use json.NewDecoder instead of ioutil.ReadAll

This commit is contained in:
jgsqware 2016-05-17 18:55:06 +02:00
parent dd970f8b06
commit d78cb4356d
5 changed files with 15 additions and 53 deletions

View File

@ -3,7 +3,6 @@ package clair
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/coreos/clair/api/v1"
@ -17,22 +16,17 @@ func Analyze(id string) (v1.LayerEnvelope, error) {
if err != nil {
return v1.LayerEnvelope{}, fmt.Errorf("analysing layer %v: %v", id, err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
var analysis v1.LayerEnvelope
err = json.NewDecoder(response.Body).Decode(&analysis)
if err != nil {
return v1.LayerEnvelope{}, fmt.Errorf("reading layer analysis: %v", err)
}
if response.StatusCode != 200 {
return v1.LayerEnvelope{}, fmt.Errorf("%d - %s", response.StatusCode, string(body))
//TODO: should I show reponse body in case of error?
return v1.LayerEnvelope{}, fmt.Errorf("receiving http error: %d", response.StatusCode)
}
var analysis v1.LayerEnvelope
err = json.Unmarshal(body, &analysis)
if err != nil {
return v1.LayerEnvelope{}, fmt.Errorf("unmarshalling layer analysis: %v", err)
}
return analysis, nil
}

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/coreos/clair/api/v1"
@ -38,18 +37,7 @@ func Push(layer v1.LayerEnvelope) error {
if response.StatusCode == 422 {
return OSNotSupported
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("reading 'add layer' response : %v", err)
}
var lErr LayerError
err = json.Unmarshal(body, &lErr)
if err != nil {
return fmt.Errorf("unmarshalling 'add layer' error message: %v", err)
}
return fmt.Errorf("%d - %s", response.StatusCode, string(body))
return fmt.Errorf("receiving http error: %d", response.StatusCode)
}
return nil

View File

@ -3,7 +3,6 @@ package clair
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
@ -15,18 +14,10 @@ func Versions() (interface{}, error) {
return nil, fmt.Errorf("requesting Clair version: %v", err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
var versionBody interface{}
err = json.NewDecoder(response.Body).Decode(&versionBody)
if err != nil {
return nil, fmt.Errorf("reading Clair version body: %v", err)
}
var versionBody interface{}
err = json.Unmarshal(body, &versionBody)
if err != nil {
return nil, fmt.Errorf("unmarshalling Clair version body: %v", err)
}
return versionBody, nil
}

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
@ -73,18 +72,13 @@ func AuthenticateResponse(dockerResponse *http.Response, request *http.Request)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
var tok token
err = json.Unmarshal(body, &tok)
err = json.NewDecoder(response.Body).Decode(&tok)
if err != nil {
return err
}
request.Header.Set("Authorization", "Bearer "+tok.String())
return nil

View File

@ -3,7 +3,6 @@ package docker
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/Sirupsen/logrus"
@ -41,10 +40,6 @@ func Pull(imageName string) (Image, error) {
}
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return Image{}, fmt.Errorf("reading manifest body: %v", err)
}
if response.StatusCode != 200 {
switch response.StatusCode {
case http.StatusUnauthorized:
@ -52,22 +47,22 @@ func Pull(imageName string) (Image, error) {
case http.StatusNotFound:
return Image{}, config.ErrLoginNotFound
default:
return Image{}, fmt.Errorf("%d - %s", response.StatusCode, string(body))
return Image{}, fmt.Errorf("receiving http error: %d", response.StatusCode)
}
}
if err := image.parseManifest(body); err != nil {
if err := image.parseManifest(response); err != nil {
return Image{}, fmt.Errorf("parsing manifest: %v", err)
}
return image, nil
}
func (image *Image) parseManifest(body []byte) error {
func (image *Image) parseManifest(response *http.Response) error {
err := json.Unmarshal(body, &image)
err := json.NewDecoder(response.Body).Decode(&image)
if err != nil {
return fmt.Errorf("unmarshalling manifest body: %v", err)
return fmt.Errorf("reading manifest body: %v", err)
}
image.uniqueLayers()