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)
response, err := http.Get(lURI)
defer response.Body.Close()
if err != nil {
return v1.LayerEnvelope{}, fmt.Errorf("analysing layer %v: %v", id, err)
}
defer response.Body.Close()
var analysis v1.LayerEnvelope
err = json.NewDecoder(response.Body).Decode(&analysis)
if err != nil {

View File

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

View File

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

View File

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

View File

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