Skip to content

Commit

Permalink
Straighten out remote.List{WithContext} (#1090)
Browse files Browse the repository at this point in the history
remote.ListWithContext now just wraps remote.List, instead of the other
wawy around. This also has the effect that ping() uses the given
context.

remote.ListWithContext is marked as deprecated, to be removed in a
future update.
  • Loading branch information
imjasonh authored Jul 30, 2021
1 parent eca1cd8 commit a3a06bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
26 changes: 10 additions & 16 deletions pkg/v1/remote/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ type tags struct {
Tags []string `json:"tags"`
}

// List wraps ListWithContext using the background context.
func List(repo name.Repository, options ...Option) ([]string, error) {
return ListWithContext(context.Background(), repo, options...)
// ListWithContext calls List with the given context.
//
// Deprecated: Use List and WithContext. This will be removed in a future release.
func ListWithContext(ctx context.Context, repo name.Repository, options ...Option) ([]string, error) {
return List(repo, append(options, WithContext(ctx))...)
}

// ListWithContext calls /tags/list for the given repository, returning the list of tags
// List calls /tags/list for the given repository, returning the list of tags
// in the "tags" property.
func ListWithContext(ctx context.Context, repo name.Repository, options ...Option) ([]string, error) {
func List(repo name.Repository, options ...Option) ([]string, error) {
o, err := makeOptions(repo, options...)
if err != nil {
return nil, err
Expand All @@ -58,30 +60,22 @@ func ListWithContext(ctx context.Context, repo name.Repository, options ...Optio
RawQuery: "n=1000",
}

// This is lazy, but I want to make sure List(..., WithContext(ctx)) works
// without calling makeOptions() twice (which can have side effects).
// This means ListWithContext(ctx, ..., WithContext(ctx2)) prefers ctx2.
if o.context != context.Background() {
ctx = o.context
}

client := http.Client{Transport: tr}
tagList := []string{}
parsed := tags{}

// get responses until there is no next page
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-o.context.Done():
return nil, o.context.Err()
default:
}

req, err := http.NewRequest("GET", uri.String(), nil)
req, err := http.NewRequestWithContext(o.context, "GET", uri.String(), nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)

resp, err := client.Do(req)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/v1/remote/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -110,8 +111,8 @@ func TestCancelledList(t *testing.T) {
}

_, err = ListWithContext(ctx, repo)
if want, got := context.Canceled, err; got != want {
t.Errorf("wanted %v got %v", want, got)
if err == nil || !strings.Contains(err.Error(), "context canceled") {
t.Errorf(`unexpected error; want "context canceled", got %v`, err)
}
}

Expand Down

0 comments on commit a3a06bb

Please sign in to comment.