Fix defer

This commit is contained in:
jgsqware 2016-07-01 10:22:33 +02:00
parent da0ed1af1b
commit 3921352734
6 changed files with 16 additions and 10 deletions

View File

@ -47,11 +47,12 @@ func analyzeLayer(id string) (v1.LayerEnvelope, error) {
lURI := fmt.Sprintf("%v/layers/%v?vulnerabilities", uri, id) lURI := fmt.Sprintf("%v/layers/%v?vulnerabilities", uri, id)
response, err := http.Get(lURI) response, err := http.Get(lURI)
defer response.Body.Close()
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()
var analysis v1.LayerEnvelope var analysis v1.LayerEnvelope
err = json.NewDecoder(response.Body).Decode(&analysis) err = json.NewDecoder(response.Body).Decode(&analysis)
if err != nil { if err != nil {

View File

@ -10,11 +10,12 @@ import (
func IsHealthy() bool { func IsHealthy() bool {
logrus.Debugln("requesting health on: " + healthURI) logrus.Debugln("requesting health on: " + healthURI)
response, err := http.Get(healthURI) response, err := http.Get(healthURI)
defer response.Body.Close()
if err != nil { if err != nil {
logrus.Errorf("requesting Clair health: %v", err) logrus.Errorf("requesting Clair health: %v", err)
return false return false
} }
defer response.Body.Close()
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
return false return false

View File

@ -90,10 +90,11 @@ func pushLayer(layer v1.LayerEnvelope) error {
request.Header.Set("Content-Type", "application/json") request.Header.Set("Content-Type", "application/json")
response, err := (&http.Client{}).Do(request) response, err := (&http.Client{}).Do(request)
defer response.Body.Close()
if err != nil { if err != nil {
return fmt.Errorf("pushing layer to clair: %v", err) return fmt.Errorf("pushing layer to clair: %v", err)
} }
defer response.Body.Close()
if response.StatusCode != 201 { if response.StatusCode != 201 {
if response.StatusCode == 422 { if response.StatusCode == 422 {

View File

@ -9,11 +9,11 @@ import (
func Versions() (interface{}, error) { func Versions() (interface{}, error) {
Config() Config()
response, err := http.Get(uri + "/versions") response, err := http.Get(uri + "/versions")
defer response.Body.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf("requesting Clair version: %v", err) return nil, fmt.Errorf("requesting Clair version: %v", err)
} }
defer response.Body.Close()
var versionBody interface{} var versionBody interface{}
err = json.NewDecoder(response.Body).Decode(&versionBody) err = json.NewDecoder(response.Body).Decode(&versionBody)
if err != nil { if err != nil {

View File

@ -65,15 +65,16 @@ func save(imageName string) (schema1.SignedManifest, error) {
// open output file // open output file
fo, err := os.Create(path + "/output.tar") fo, err := os.Create(path + "/output.tar")
if err != nil {
return schema1.SignedManifest{}, err
}
// close fo on exit and check for its returned error // close fo on exit and check for its returned error
defer func() { defer func() {
if err := fo.Close(); err != nil { if err := fo.Close(); err != nil {
panic(err) panic(err)
} }
}() }()
if err != nil {
return schema1.SignedManifest{}, err
}
// make a write buffer // make a write buffer
w := bufio.NewWriter(fo) w := bufio.NewWriter(fo)
@ -99,10 +100,11 @@ func save(imageName string) (schema1.SignedManifest, error) {
func historyFromManifest(path string) (schema1.SignedManifest, error) { func historyFromManifest(path string) (schema1.SignedManifest, error) {
mf, err := os.Open(path + "/manifest.json") mf, err := os.Open(path + "/manifest.json")
defer mf.Close()
if err != nil { if err != nil {
return schema1.SignedManifest{}, err return schema1.SignedManifest{}, err
} }
defer mf.Close()
// https://github.com/docker/docker/blob/master/image/tarexport/tarexport.go#L17 // https://github.com/docker/docker/blob/master/image/tarexport/tarexport.go#L17
type manifestItem struct { type manifestItem struct {
@ -160,10 +162,11 @@ func historyFromCommand(imageName string) (schema1.SignedManifest, error) {
func openAndUntar(name, dst string) error { func openAndUntar(name, dst string) error {
var rd io.Reader var rd io.Reader
f, err := os.Open(name) f, err := os.Open(name)
defer f.Close()
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
rd = f rd = f
if strings.HasSuffix(name, ".gz") || strings.HasSuffix(name, ".tgz") { if strings.HasSuffix(name, ".gz") || strings.HasSuffix(name, ".tgz") {
gr, err := gzip.NewReader(f) gr, err := gzip.NewReader(f)

View File

@ -52,6 +52,7 @@ func AuthenticateResponse(client *http.Client, dockerResponse *http.Response, re
req.SetBasicAuth(authConfig.Username, authConfig.Password) req.SetBasicAuth(authConfig.Username, authConfig.Password)
response, err := client.Do(req) response, err := client.Do(req)
defer response.Body.Close()
if err != nil { if err != nil {
return err return err
@ -65,7 +66,6 @@ func AuthenticateResponse(client *http.Client, dockerResponse *http.Response, re
return fmt.Errorf("authentication server response: %v - %v", response.StatusCode, response.Status) return fmt.Errorf("authentication server response: %v - %v", response.StatusCode, response.Status)
} }
defer response.Body.Close()
type token struct { type token struct {
Value string `json:"token"` Value string `json:"token"`
} }