Skip to content

Commit e94e214

Browse files
Support pagination for tags
1 parent efad427 commit e94e214

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

registry/tags.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
11
package registry
22

3-
import "context"
3+
import (
4+
"context"
5+
"net/url"
6+
7+
"github.com/peterhellberg/link"
8+
)
49

510
type tagsResponse struct {
611
Tags []string `json:"tags"`
712
}
813

9-
// Tags returns the tags for a specific repository.
10-
func (r *Registry) Tags(ctx context.Context, repository string) ([]string, error) {
11-
url := r.url("/v2/%s/tags/list", repository)
12-
r.Logf("registry.tags url=%s repository=%s", url, repository)
14+
func (r *Registry) tags(ctx context.Context, u string, repository string) ([]string, error) {
15+
var uri string
16+
if u == "" {
17+
r.Logf("registry.tags url=%s repository=%s", u, repository)
18+
uri = r.url("/v2/%s/tags/list", repository)
19+
} else {
20+
uri = r.url(u)
21+
}
1322

1423
var response tagsResponse
15-
if _, err := r.getJSON(ctx, url, &response); err != nil {
24+
h, err := r.getJSON(ctx, uri, &response)
25+
if err != nil {
1626
return nil, err
1727
}
1828

29+
for _, l := range link.ParseHeader(h) {
30+
if l.Rel == "next" {
31+
unescaped, _ := url.QueryUnescape(l.URI)
32+
tags, err := r.tags(ctx, unescaped, repository)
33+
if err != nil {
34+
return nil, err
35+
}
36+
response.Tags = append(response.Tags, tags...)
37+
}
38+
}
39+
1940
return response.Tags, nil
2041
}
42+
43+
// Tags returns the tags for a specific repository.
44+
func (r *Registry) Tags(ctx context.Context, repository string) ([]string, error) {
45+
return r.tags(ctx, "", repository)
46+
}

0 commit comments

Comments
 (0)