From 7bd1888b7356df0ca3daf95ea2f26f2772802cb5 Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Fri, 24 Jun 2016 00:21:14 +0800 Subject: [PATCH 1/2] handle pagination in catalog api --- utils/registry/registry.go | 81 ++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/utils/registry/registry.go b/utils/registry/registry.go index baaf70e91..08b7411b1 100644 --- a/utils/registry/registry.go +++ b/utils/registry/registry.go @@ -17,7 +17,6 @@ package registry import ( "encoding/json" - "fmt" "io/ioutil" "net/http" "net/url" @@ -81,51 +80,57 @@ func NewRegistryWithUsername(endpoint, username string) (*Registry, error) { // Catalog ... func (r *Registry) Catalog() ([]string, error) { repos := []string{} + suffix := "/v2/_catalog" + var url string - req, err := http.NewRequest("GET", buildCatalogURL(r.Endpoint.String()), nil) - if err != nil { - return repos, err - } + for len(suffix) > 0 { + url = r.Endpoint.String() + suffix - resp, err := r.client.Do(req) - if err != nil { - ok, e := isUnauthorizedError(err) - if ok { - return repos, e + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return repos, err } - return repos, err - } - - defer resp.Body.Close() - - b, err := ioutil.ReadAll(resp.Body) - if err != nil { - return repos, err - } - - if resp.StatusCode == http.StatusOK { - catalogResp := struct { - Repositories []string `json:"repositories"` - }{} - - if err := json.Unmarshal(b, &catalogResp); err != nil { + resp, err := r.client.Do(req) + if err != nil { + ok, e := isUnauthorizedError(err) + if ok { + return repos, e + } return repos, err } - repos = catalogResp.Repositories + defer resp.Body.Close() + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return repos, err + } - return repos, nil + if resp.StatusCode == http.StatusOK { + catalogResp := struct { + Repositories []string `json:"repositories"` + }{} + + if err := json.Unmarshal(b, &catalogResp); err != nil { + return repos, err + } + + repos = append(repos, catalogResp.Repositories...) + //Link: ; rel="next" + link := resp.Header.Get("Link") + if strings.HasSuffix(link, `rel="next"`) && strings.Index(link, "<") >= 0 && strings.Index(link, ">") >= 0 { + suffix = link[strings.Index(link, "<")+1 : strings.Index(link, ">")] + } else { + suffix = "" + } + } else { + return repos, errors.Error{ + StatusCode: resp.StatusCode, + StatusText: resp.Status, + Message: string(b), + } + } } - - return repos, errors.Error{ - StatusCode: resp.StatusCode, - StatusText: resp.Status, - Message: string(b), - } -} - -func buildCatalogURL(endpoint string) string { - return fmt.Sprintf("%s/v2/_catalog", endpoint) + return repos, nil } func newClient(endpoint, username string, credential auth.Credential, From 23d1acb24abd4ca5444380484e20e71280bffde0 Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Fri, 24 Jun 2016 01:37:37 +0800 Subject: [PATCH 2/2] fixes #398 --- utils/registry/registry.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/registry/registry.go b/utils/registry/registry.go index 08b7411b1..c4b7677f5 100644 --- a/utils/registry/registry.go +++ b/utils/registry/registry.go @@ -80,7 +80,7 @@ func NewRegistryWithUsername(endpoint, username string) (*Registry, error) { // Catalog ... func (r *Registry) Catalog() ([]string, error) { repos := []string{} - suffix := "/v2/_catalog" + suffix := "/v2/_catalog?n=1000" var url string for len(suffix) > 0 {