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 ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"github.com/coreos/clair/api/v1" "github.com/coreos/clair/api/v1"
@ -17,22 +16,17 @@ func Analyze(id string) (v1.LayerEnvelope, error) {
if err != nil { if err != nil {
return v1.LayerEnvelope{}, fmt.Errorf("analysing layer %v: %v", id, err) return v1.LayerEnvelope{}, fmt.Errorf("analysing layer %v: %v", id, err)
} }
defer response.Body.Close() defer response.Body.Close()
var analysis v1.LayerEnvelope
body, err := ioutil.ReadAll(response.Body) err = json.NewDecoder(response.Body).Decode(&analysis)
if err != nil { if err != nil {
return v1.LayerEnvelope{}, fmt.Errorf("reading layer analysis: %v", err) return v1.LayerEnvelope{}, fmt.Errorf("reading layer analysis: %v", err)
} }
if response.StatusCode != 200 { 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 return analysis, nil
} }

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"github.com/coreos/clair/api/v1" "github.com/coreos/clair/api/v1"
@ -38,18 +37,7 @@ func Push(layer v1.LayerEnvelope) error {
if response.StatusCode == 422 { if response.StatusCode == 422 {
return OSNotSupported return OSNotSupported
} }
return fmt.Errorf("receiving http error: %d", response.StatusCode)
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 nil return nil

View File

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

View File

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

View File

@ -3,7 +3,6 @@ package docker
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"github.com/Sirupsen/logrus" "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 { if response.StatusCode != 200 {
switch response.StatusCode { switch response.StatusCode {
case http.StatusUnauthorized: case http.StatusUnauthorized:
@ -52,22 +47,22 @@ func Pull(imageName string) (Image, error) {
case http.StatusNotFound: case http.StatusNotFound:
return Image{}, config.ErrLoginNotFound return Image{}, config.ErrLoginNotFound
default: 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{}, fmt.Errorf("parsing manifest: %v", err)
} }
return image, nil 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 { if err != nil {
return fmt.Errorf("unmarshalling manifest body: %v", err) return fmt.Errorf("reading manifest body: %v", err)
} }
image.uniqueLayers() image.uniqueLayers()