Skip to content

Commit 4dd063b

Browse files
author
Zachary Scott
authored
Merge pull request #244 from CircleCI-Public/context-free
Remove 'context' dependency from API
2 parents 7e18181 + 2a97807 commit 4dd063b

File tree

8 files changed

+113
-162
lines changed

8 files changed

+113
-162
lines changed

api/api.go

Lines changed: 56 additions & 63 deletions
Large diffs are not rendered by default.

client/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ func prepareRequest(ctx context.Context, address string, request *Request) (*htt
167167
// Run sends an HTTP request to the GraphQL server and deserializes the response or returns an error.
168168
// TODO(zzak): This function is fairly complex, we should refactor it
169169
// nolint: gocyclo
170-
func (cl *Client) Run(ctx context.Context, request *Request, resp interface{}) error {
170+
func (cl *Client) Run(request *Request, resp interface{}) error {
171171
l := log.New(os.Stderr, "", 0)
172+
ctx := context.Background()
172173

173174
select {
174175
case <-ctx.Done():

client/client_test.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package client
22

33
import (
4-
"context"
54
"io"
65
"io/ioutil"
76
"net/http"
87
"net/http/httptest"
98
"regexp"
109
"testing"
11-
"time"
1210
)
1311

1412
func TestServerAddress(t *testing.T) {
@@ -72,15 +70,12 @@ func TestDoJSON(t *testing.T) {
7270
}))
7371
defer srv.Close()
7472

75-
ctx := context.Background()
7673
client := NewClient(srv.URL, "/", "token", false)
7774

78-
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
79-
defer cancel()
8075
var resp struct {
8176
Something string
8277
}
83-
err := client.Run(ctx, &Request{Query: "query {}"}, &resp)
78+
err := client.Run(&Request{Query: "query {}"}, &resp)
8479
if err != nil {
8580
t.Errorf(err.Error())
8681
}
@@ -111,8 +106,6 @@ func TestQueryJSON(t *testing.T) {
111106
}
112107
}))
113108
defer srv.Close()
114-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
115-
defer cancel()
116109

117110
client := NewClient(srv.URL, "/", "token", false)
118111

@@ -131,7 +124,7 @@ func TestQueryJSON(t *testing.T) {
131124
var resp struct {
132125
Value string
133126
}
134-
err := client.Run(ctx, req, &resp)
127+
err := client.Run(req, &resp)
135128
if err != nil {
136129
t.Errorf(err.Error())
137130
}
@@ -174,15 +167,11 @@ func TestDoJSONErr(t *testing.T) {
174167

175168
defer server.Close()
176169

177-
ctx := context.Background()
178170
client := NewClient(server.URL, "/", "token", false)
179171

180-
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
181-
defer cancel()
182-
183172
var responseData map[string]interface{}
184173

185-
err := client.Run(ctx, &Request{Query: "query {}"}, &responseData)
174+
err := client.Run(&Request{Query: "query {}"}, &responseData)
186175
if err.Error() != "Something went wrong\nSomething else went wrong" {
187176
t.Errorf(err.Error())
188177
}
@@ -202,8 +191,6 @@ func TestHeader(t *testing.T) {
202191
}
203192
}))
204193
defer srv.Close()
205-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
206-
defer cancel()
207194

208195
client := NewClient(srv.URL, "/", "token", false)
209196

@@ -213,7 +200,7 @@ func TestHeader(t *testing.T) {
213200
var resp struct {
214201
Value string
215202
}
216-
err := client.Run(ctx, req, &resp)
203+
err := client.Run(req, &resp)
217204
if err != nil {
218205
t.Errorf(err.Error())
219206
}
@@ -240,16 +227,14 @@ func TestStatusCode200(t *testing.T) {
240227
}
241228
}))
242229
defer srv.Close()
243-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
244-
defer cancel()
245230

246231
client := NewClient(srv.URL, "/", "token", false)
247232

248233
req := NewUnauthorizedRequest("query {}")
249234

250235
var resp interface{}
251236

252-
err := client.Run(ctx, req, &resp)
237+
err := client.Run(req, &resp)
253238
if err != nil {
254239
t.Errorf(err.Error())
255240
}
@@ -272,16 +257,14 @@ func TestStatusCode500(t *testing.T) {
272257
}
273258
}))
274259
defer srv.Close()
275-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
276-
defer cancel()
277260

278261
client := NewClient(srv.URL, "/", "token", false)
279262

280263
req := NewUnauthorizedRequest("query {}")
281264

282265
var resp interface{}
283266

284-
err := client.Run(ctx, req, &resp)
267+
err := client.Run(req, &resp)
285268
if err == nil {
286269
t.Error("expected error")
287270
}
@@ -308,16 +291,14 @@ func TestStatusCode413(t *testing.T) {
308291
}
309292
}))
310293
defer srv.Close()
311-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
312-
defer cancel()
313294

314295
client := NewClient(srv.URL, "/", "token", false)
315296

316297
req := NewUnauthorizedRequest("query {}")
317298

318299
var resp interface{}
319300

320-
err := client.Run(ctx, req, &resp)
301+
err := client.Run(req, &resp)
321302
if err == nil {
322303
t.Error("expected error")
323304
}

cmd/config.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65

76
"github.com/CircleCI-Public/circleci-cli/api"
@@ -17,9 +16,9 @@ import (
1716
const defaultConfigPath = ".circleci/config.yml"
1817

1918
type configOptions struct {
20-
cfg *settings.Config
21-
apiOpts api.Options
22-
args []string
19+
cfg *settings.Config
20+
cl *client.Client
21+
args []string
2322
}
2423

2524
// Path to the config.yml file to operate on.
@@ -32,8 +31,7 @@ var configAnnotations = map[string]string{
3231

3332
func newConfigCommand(config *settings.Config) *cobra.Command {
3433
opts := configOptions{
35-
apiOpts: api.Options{},
36-
cfg: config,
34+
cfg: config,
3735
}
3836

3937
configCmd := &cobra.Command{
@@ -46,8 +44,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command {
4644
Short: "Pack up your CircleCI configuration into a single file.",
4745
PreRun: func(cmd *cobra.Command, args []string) {
4846
opts.args = args
49-
opts.apiOpts.Context = context.Background()
50-
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
47+
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
5148
},
5249
RunE: func(_ *cobra.Command, _ []string) error {
5350
return packConfig(opts)
@@ -63,8 +60,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command {
6360
Short: "Check that the config file is well formed.",
6461
PreRun: func(cmd *cobra.Command, args []string) {
6562
opts.args = args
66-
opts.apiOpts.Context = context.Background()
67-
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
63+
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
6864
},
6965
RunE: func(_ *cobra.Command, _ []string) error {
7066
return validateConfig(opts)
@@ -83,8 +79,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command {
8379
Short: "Process the config.",
8480
PreRun: func(cmd *cobra.Command, args []string) {
8581
opts.args = args
86-
opts.apiOpts.Context = context.Background()
87-
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
82+
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
8883
},
8984
RunE: func(_ *cobra.Command, _ []string) error {
9085
return processConfig(opts)
@@ -99,8 +94,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command {
9994
Short: "Migrate a pre-release 2.0 config to the official release version",
10095
PreRun: func(cmd *cobra.Command, args []string) {
10196
opts.args = args
102-
opts.apiOpts.Context = context.Background()
103-
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
97+
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
10498
},
10599
RunE: func(_ *cobra.Command, _ []string) error {
106100
return migrateConfig(opts)
@@ -133,7 +127,7 @@ func validateConfig(opts configOptions) error {
133127
path = opts.args[0]
134128
}
135129

136-
_, err := api.ConfigQuery(opts.apiOpts, path)
130+
_, err := api.ConfigQuery(opts.cl, path)
137131

138132
if err != nil {
139133
return err
@@ -149,7 +143,7 @@ func validateConfig(opts configOptions) error {
149143
}
150144

151145
func processConfig(opts configOptions) error {
152-
response, err := api.ConfigQuery(opts.apiOpts, opts.args[0])
146+
response, err := api.ConfigQuery(opts.cl, opts.args[0])
153147

154148
if err != nil {
155149
return err

cmd/diagnostic.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76

@@ -13,24 +12,22 @@ import (
1312
)
1413

1514
type diagnosticOptions struct {
16-
cfg *settings.Config
17-
apiOpts api.Options
18-
args []string
15+
cfg *settings.Config
16+
cl *client.Client
17+
args []string
1918
}
2019

2120
func newDiagnosticCommand(config *settings.Config) *cobra.Command {
2221
opts := diagnosticOptions{
23-
apiOpts: api.Options{},
24-
cfg: config,
22+
cfg: config,
2523
}
2624

2725
diagnosticCommand := &cobra.Command{
2826
Use: "diagnostic",
2927
Short: "Check the status of your CircleCI CLI.",
3028
PreRun: func(cmd *cobra.Command, args []string) {
3129
opts.args = args
32-
opts.apiOpts.Context = context.Background()
33-
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
30+
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
3431
},
3532
RunE: func(_ *cobra.Command, _ []string) error {
3633
return diagnostic(opts)
@@ -56,7 +53,7 @@ https://circleci.com/account/api`)
5653

5754
fmt.Println("Trying an introspection query on API... ")
5855

59-
responseIntro, err := api.IntrospectionQuery(opts.apiOpts)
56+
responseIntro, err := api.IntrospectionQuery(opts.cl)
6057
if responseIntro.Schema.QueryType.Name == "" {
6158
fmt.Println("Unable to make a query against the GraphQL API, please check your settings")
6259
if err != nil {
@@ -73,7 +70,7 @@ https://circleci.com/account/api`)
7370
}
7471
}
7572

76-
responseWho, err := api.WhoamiQuery(opts.apiOpts)
73+
responseWho, err := api.WhoamiQuery(opts.cl)
7774

7875
if err != nil {
7976
return err

cmd/namespace.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"strings"
76

@@ -12,15 +11,14 @@ import (
1211
)
1312

1413
type namespaceOptions struct {
15-
apiOpts api.Options
16-
cfg *settings.Config
17-
args []string
14+
cfg *settings.Config
15+
cl *client.Client
16+
args []string
1817
}
1918

2019
func newNamespaceCommand(config *settings.Config) *cobra.Command {
2120
opts := namespaceOptions{
22-
apiOpts: api.Options{},
23-
cfg: config,
21+
cfg: config,
2422
}
2523

2624
namespaceCmd := &cobra.Command{
@@ -35,8 +33,7 @@ func newNamespaceCommand(config *settings.Config) *cobra.Command {
3533
Please note that at this time all namespaces created in the registry are world-readable.`,
3634
PreRun: func(cmd *cobra.Command, args []string) {
3735
opts.args = args
38-
opts.apiOpts.Context = context.Background()
39-
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
36+
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug)
4037
},
4138
RunE: func(_ *cobra.Command, _ []string) error {
4239
return createNamespace(opts)
@@ -57,7 +54,7 @@ Please note that at this time all namespaces created in the registry are world-r
5754
func createNamespace(opts namespaceOptions) error {
5855
namespaceName := opts.args[0]
5956

60-
_, err := api.CreateNamespace(opts.apiOpts, namespaceName, opts.args[2], strings.ToUpper(opts.args[1]))
57+
_, err := api.CreateNamespace(opts.cl, namespaceName, opts.args[2], strings.ToUpper(opts.args[1]))
6158

6259
if err != nil {
6360
return err

0 commit comments

Comments
 (0)