Skip to content

Commit

Permalink
Small fixes and better error message (#145)
Browse files Browse the repository at this point in the history
* Fix typo in main.go
* Sum up 2 if clauses in one in docker.go
* Add error message if Docker Registry sends unsupported Content-Type
(this occurs for example in case a when asking for an Image with Digest
that references a docker ManifestList)
  • Loading branch information
naibaf0 authored and hashmap committed Apr 16, 2019
1 parent 970fc7e commit 1ba2d59
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ func (i *Image) Pull() error {
}

func parseImageResponse(resp *http.Response, image *Image) error {
contentType := resp.Header.Get("Content-Type")
if contentType == "application/vnd.docker.distribution.manifest.v2+json" {
switch contentType := resp.Header.Get("Content-Type"); contentType {
case "application/vnd.docker.distribution.manifest.v2+json":
var imageV2 imageV2
if err := json.NewDecoder(resp.Body).Decode(&imageV2); err != nil {
fmt.Fprintln(os.Stderr, "Image V2 decode error")
Expand All @@ -249,7 +249,7 @@ func parseImageResponse(resp *http.Response, image *Image) error {
}
image.digest = imageV2.Config.Digest
image.schemaVersion = imageV2.SchemaVersion
} else {
case "application/vnd.docker.distribution.manifest.v1+prettyjws":
var imageV1 imageV1
if err := json.NewDecoder(resp.Body).Decode(&imageV1); err != nil {
fmt.Fprintln(os.Stderr, "ImageV1 decode error")
Expand All @@ -262,6 +262,8 @@ func parseImageResponse(resp *http.Response, image *Image) error {
image.FsLayers[len(imageV1.FsLayers)-1-i].BlobSum = imageV1.FsLayers[i].BlobSum
}
image.schemaVersion = imageV1.SchemaVersion
default:
return fmt.Errorf("Docker Registry responded with unsupported Content-Type: %s", contentType)
}
return nil
}
Expand Down Expand Up @@ -319,11 +321,9 @@ func (i *Image) pullReq() (*http.Response, error) {
fmt.Fprintln(os.Stderr, "Can't create a request")
return nil, err
}
if i.Token == "" {
if i.user != "" {
req.SetBasicAuth(i.user, i.password)
i.Token = req.Header.Get("Authorization")
}
if i.Token == "" && i.user != "" {
req.SetBasicAuth(i.user, i.password)
i.Token = req.Header.Get("Authorization")
} else {
req.Header.Set("Authorization", i.Token)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {

image, err := docker.NewImage(&conf.DockerConfig)
if err != nil {
fail("Can't parse qname: %s", err)
fail("Can't parse name: %s", err)
}

err = image.Pull()
Expand Down

0 comments on commit 1ba2d59

Please sign in to comment.