Skip to content

Commit a3a06bb

Browse files
authored
Straighten out remote.List{WithContext} (#1090)
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.
1 parent eca1cd8 commit a3a06bb

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

Diff for: pkg/v1/remote/list.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ type tags struct {
3131
Tags []string `json:"tags"`
3232
}
3333

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

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

61-
// This is lazy, but I want to make sure List(..., WithContext(ctx)) works
62-
// without calling makeOptions() twice (which can have side effects).
63-
// This means ListWithContext(ctx, ..., WithContext(ctx2)) prefers ctx2.
64-
if o.context != context.Background() {
65-
ctx = o.context
66-
}
67-
6863
client := http.Client{Transport: tr}
6964
tagList := []string{}
7065
parsed := tags{}
7166

7267
// get responses until there is no next page
7368
for {
7469
select {
75-
case <-ctx.Done():
76-
return nil, ctx.Err()
70+
case <-o.context.Done():
71+
return nil, o.context.Err()
7772
default:
7873
}
7974

80-
req, err := http.NewRequest("GET", uri.String(), nil)
75+
req, err := http.NewRequestWithContext(o.context, "GET", uri.String(), nil)
8176
if err != nil {
8277
return nil, err
8378
}
84-
req = req.WithContext(ctx)
8579

8680
resp, err := client.Do(req)
8781
if err != nil {

Diff for: pkg/v1/remote/list_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/http"
2121
"net/http/httptest"
2222
"net/url"
23+
"strings"
2324
"testing"
2425

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

112113
_, err = ListWithContext(ctx, repo)
113-
if want, got := context.Canceled, err; got != want {
114-
t.Errorf("wanted %v got %v", want, got)
114+
if err == nil || !strings.Contains(err.Error(), "context canceled") {
115+
t.Errorf(`unexpected error; want "context canceled", got %v`, err)
115116
}
116117
}
117118

0 commit comments

Comments
 (0)