diff --git a/utils/registry/registry.go b/utils/registry/registry.go index 845c8e876..baaf70e91 100644 --- a/utils/registry/registry.go +++ b/utils/registry/registry.go @@ -89,7 +89,7 @@ func (r *Registry) Catalog() ([]string, error) { resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { return repos, e } diff --git a/utils/registry/repository.go b/utils/registry/repository.go index 479d44408..ac49e04f8 100644 --- a/utils/registry/repository.go +++ b/utils/registry/repository.go @@ -111,14 +111,15 @@ func NewRepositoryWithUsername(name, endpoint, username string) (*Repository, er return repository, nil } -func isUnauthorizedError(err error) (error, bool) { +// try to convert err to errors.Error if it is +func isUnauthorizedError(err error) (bool, error) { if strings.Contains(err.Error(), http.StatusText(http.StatusUnauthorized)) { - return errors.Error{ + return true, errors.Error{ StatusCode: http.StatusUnauthorized, StatusText: http.StatusText(http.StatusUnauthorized), - }, true + } } - return err, false + return false, err } // ListTag ... @@ -131,7 +132,7 @@ func (r *Repository) ListTag() ([]string, error) { resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { return tags, e } @@ -178,7 +179,7 @@ func (r *Repository) ManifestExist(reference string) (digest string, exist bool, resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { err = e return @@ -224,7 +225,7 @@ func (r *Repository) PullManifest(reference string, acceptMediaTypes []string) ( resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { err = e return @@ -265,7 +266,7 @@ func (r *Repository) PushManifest(reference, mediaType string, payload []byte) ( resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { err = e return @@ -303,7 +304,7 @@ func (r *Repository) DeleteManifest(digest string) error { resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { return e } @@ -354,7 +355,7 @@ func (r *Repository) BlobExist(digest string) (bool, error) { resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { return false, e } @@ -392,7 +393,7 @@ func (r *Repository) PullBlob(digest string) (size int64, data []byte, err error resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { err = e return @@ -431,7 +432,7 @@ func (r *Repository) initiateBlobUpload(name string) (location, uploadUUID strin resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { err = e return @@ -469,7 +470,7 @@ func (r *Repository) monolithicBlobUpload(location, digest string, size int64, d resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { return e } @@ -523,7 +524,7 @@ func (r *Repository) DeleteBlob(digest string) error { resp, err := r.client.Do(req) if err != nil { - e, ok := isUnauthorizedError(err) + ok, e := isUnauthorizedError(err) if ok { return e } diff --git a/utils/registry/repository_test.go b/utils/registry/repository_test.go index 5bc8a7d53..07b46237b 100644 --- a/utils/registry/repository_test.go +++ b/utils/registry/repository_test.go @@ -31,12 +31,12 @@ import ( ) var ( - username string = "user" - password string = "P@ssw0rd" - repo string = "samalba/my-app" - tags tagResp = tagResp{Tags: []string{"1.0", "2.0", "3.0"}} - validToken string = "valid_token" - invalidToken string = "invalid_token" + username = "user" + password = "P@ssw0rd" + repo = "samalba/my-app" + tags = tagResp{Tags: []string{"1.0", "2.0", "3.0"}} + validToken = "valid_token" + invalidToken = "invalid_token" credential auth.Credential registryServer *httptest.Server tokenServer *httptest.Server