From d78cb4356dc9af2be51af69d87c0b9cdda379221 Mon Sep 17 00:00:00 2001 From: jgsqware Date: Tue, 17 May 2016 18:55:06 +0200 Subject: [PATCH] use json.NewDecoder instead of ioutil.ReadAll --- cmd/clairctl/clair/analyze.go | 16 +++++----------- cmd/clairctl/clair/push.go | 14 +------------- cmd/clairctl/clair/versions.go | 13 ++----------- cmd/clairctl/docker/auth.go | 10 ++-------- cmd/clairctl/docker/pull.go | 15 +++++---------- 5 files changed, 15 insertions(+), 53 deletions(-) diff --git a/cmd/clairctl/clair/analyze.go b/cmd/clairctl/clair/analyze.go index 972f668a..2ac20d2c 100644 --- a/cmd/clairctl/clair/analyze.go +++ b/cmd/clairctl/clair/analyze.go @@ -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 } diff --git a/cmd/clairctl/clair/push.go b/cmd/clairctl/clair/push.go index a9f81893..af792fef 100644 --- a/cmd/clairctl/clair/push.go +++ b/cmd/clairctl/clair/push.go @@ -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 diff --git a/cmd/clairctl/clair/versions.go b/cmd/clairctl/clair/versions.go index 180cecdd..0942663e 100644 --- a/cmd/clairctl/clair/versions.go +++ b/cmd/clairctl/clair/versions.go @@ -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 } diff --git a/cmd/clairctl/docker/auth.go b/cmd/clairctl/docker/auth.go index f26f46f5..aa6a0866 100644 --- a/cmd/clairctl/docker/auth.go +++ b/cmd/clairctl/docker/auth.go @@ -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 diff --git a/cmd/clairctl/docker/pull.go b/cmd/clairctl/docker/pull.go index bf914ac3..85e88ea4 100644 --- a/cmd/clairctl/docker/pull.go +++ b/cmd/clairctl/docker/pull.go @@ -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()