diff --git a/api/api.go b/api/api.go index d8772dfad..f1f60ae48 100644 --- a/api/api.go +++ b/api/api.go @@ -1,7 +1,6 @@ package api import ( - "context" "encoding/json" "io/ioutil" "log" @@ -43,12 +42,6 @@ type GQLResponseError struct { Type string } -// Options wraps common requirements for API functions into a single struct. -type Options struct { - Context context.Context - Client *client.Client -} - // IntrospectionResponse matches the result from making an introspection query type IntrospectionResponse struct { Schema struct { @@ -361,15 +354,15 @@ func loadYaml(path string) (string, error) { } // WhoamiQuery returns the result of querying the `/me` endpoint of the API -func WhoamiQuery(opts Options) (*WhoamiResponse, error) { +func WhoamiQuery(cl *client.Client) (*WhoamiResponse, error) { response := WhoamiResponse{} query := `query { me { name } }` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return nil, err } - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) if err != nil { return nil, err @@ -379,7 +372,7 @@ func WhoamiQuery(opts Options) (*WhoamiResponse, error) { } // ConfigQuery calls the GQL API to validate and process config -func ConfigQuery(opts Options, configPath string) (*ConfigResponse, error) { +func ConfigQuery(cl *client.Client, configPath string) (*ConfigResponse, error) { var response BuildConfigResponse config, err := loadYaml(configPath) @@ -399,9 +392,9 @@ func ConfigQuery(opts Options, configPath string) (*ConfigResponse, error) { request := client.NewUnauthorizedRequest(query) request.Var("config", config) - request.Header.Set("Authorization", opts.Client.Token) + request.Header.Set("Authorization", cl.Token) - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) if err != nil { return nil, errors.Wrap(err, "Unable to validate config") @@ -415,7 +408,7 @@ func ConfigQuery(opts Options, configPath string) (*ConfigResponse, error) { } // OrbQuery validated and processes an orb. -func OrbQuery(opts Options, configPath string) (*ConfigResponse, error) { +func OrbQuery(cl *client.Client, configPath string) (*ConfigResponse, error) { var response OrbConfigResponse config, err := loadYaml(configPath) @@ -435,9 +428,9 @@ func OrbQuery(opts Options, configPath string) (*ConfigResponse, error) { request := client.NewUnauthorizedRequest(query) request.Var("config", config) - request.Header.Set("Authorization", opts.Client.Token) + request.Header.Set("Authorization", cl.Token) - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) if err != nil { return nil, errors.Wrap(err, "Unable to validate config") @@ -451,7 +444,7 @@ func OrbQuery(opts Options, configPath string) (*ConfigResponse, error) { } // OrbPublishByID publishes a new version of an orb by id -func OrbPublishByID(opts Options, configPath string, orbID string, orbVersion string) (*Orb, error) { +func OrbPublishByID(cl *client.Client, configPath string, orbID string, orbVersion string) (*Orb, error) { var response OrbPublishResponse config, err := loadYaml(configPath) @@ -474,7 +467,7 @@ func OrbPublishByID(opts Options, configPath string, orbID string, orbVersion st } ` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return nil, err } @@ -482,7 +475,7 @@ func OrbPublishByID(opts Options, configPath string, orbID string, orbVersion st request.Var("orbId", orbID) request.Var("version", orbVersion) - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) if err != nil { return nil, errors.Wrap(err, "Unable to publish orb") @@ -496,7 +489,7 @@ func OrbPublishByID(opts Options, configPath string, orbID string, orbVersion st } // OrbID fetches an orb returning the ID -func OrbID(opts Options, namespace string, orb string) (*OrbIDResponse, error) { +func OrbID(cl *client.Client, namespace string, orb string) (*OrbIDResponse, error) { name := namespace + "/" + orb var response OrbIDResponse @@ -512,14 +505,14 @@ func OrbID(opts Options, namespace string, orb string) (*OrbIDResponse, error) { } ` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return nil, err } request.Var("name", name) request.Var("namespace", namespace) - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) // If there is an error, or the request was successful, return now. if err != nil || response.Orb.ID != "" { @@ -535,7 +528,7 @@ func OrbID(opts Options, namespace string, orb string) (*OrbIDResponse, error) { return nil, fmt.Errorf("the '%s' orb does not exist in the '%s' namespace. Did you misspell the namespace or the orb name?", orb, namespace) } -func createNamespaceWithOwnerID(opts Options, name string, ownerID string) (*CreateNamespaceResponse, error) { +func createNamespaceWithOwnerID(cl *client.Client, name string, ownerID string) (*CreateNamespaceResponse, error) { var response CreateNamespaceResponse query := ` @@ -554,14 +547,14 @@ func createNamespaceWithOwnerID(opts Options, name string, ownerID string) (*Cre } }` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return nil, err } request.Var("name", name) request.Var("organizationId", ownerID) - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) if len(response.CreateNamespace.Errors) > 0 { return nil, response.CreateNamespace.Errors @@ -574,7 +567,7 @@ func createNamespaceWithOwnerID(opts Options, name string, ownerID string) (*Cre return &response, nil } -func getOrganization(opts Options, organizationName string, organizationVcs string) (*GetOrganizationResponse, error) { +func getOrganization(cl *client.Client, organizationName string, organizationVcs string) (*GetOrganizationResponse, error) { var response GetOrganizationResponse query := `query($organizationName: String!, $organizationVcs: VCSType!) { @@ -586,14 +579,14 @@ func getOrganization(opts Options, organizationName string, organizationVcs stri } }` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return nil, err } request.Var("organizationName", organizationName) request.Var("organizationVcs", organizationVcs) - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) if err != nil { return nil, errors.Wrapf(err, "Unable to find organization %s of vcs-type %s", organizationName, organizationVcs) @@ -611,14 +604,14 @@ func organizationNotFound(name string, vcs string) error { } // CreateNamespace creates (reserves) a namespace for an organization -func CreateNamespace(opts Options, name string, organizationName string, organizationVcs string) (*CreateNamespaceResponse, error) { - getOrgResponse, getOrgError := getOrganization(opts, organizationName, organizationVcs) +func CreateNamespace(cl *client.Client, name string, organizationName string, organizationVcs string) (*CreateNamespaceResponse, error) { + getOrgResponse, getOrgError := getOrganization(cl, organizationName, organizationVcs) if getOrgError != nil { return nil, errors.Wrap(organizationNotFound(organizationName, organizationVcs), getOrgError.Error()) } - createNSResponse, createNSError := createNamespaceWithOwnerID(opts, name, getOrgResponse.Organization.ID) + createNSResponse, createNSError := createNamespaceWithOwnerID(cl, name, getOrgResponse.Organization.ID) if createNSError != nil { return nil, createNSError @@ -627,7 +620,7 @@ func CreateNamespace(opts Options, name string, organizationName string, organiz return createNSResponse, nil } -func getNamespace(opts Options, name string) (*GetNamespaceResponse, error) { +func getNamespace(cl *client.Client, name string) (*GetNamespaceResponse, error) { var response GetNamespaceResponse query := ` @@ -639,13 +632,13 @@ func getNamespace(opts Options, name string) (*GetNamespaceResponse, error) { } }` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return nil, err } request.Var("name", name) - if err = opts.Client.Run(opts.Context, request, &response); err != nil { + if err = cl.Run(request, &response); err != nil { return nil, errors.Wrapf(err, "failed to load namespace '%s'", err) } @@ -656,7 +649,7 @@ func getNamespace(opts Options, name string) (*GetNamespaceResponse, error) { return &response, nil } -func createOrbWithNsID(opts Options, name string, namespaceID string) (*CreateOrbResponse, error) { +func createOrbWithNsID(cl *client.Client, name string, namespaceID string) (*CreateOrbResponse, error) { var response CreateOrbResponse query := `mutation($name: String!, $registryNamespaceId: UUID!){ @@ -674,14 +667,14 @@ func createOrbWithNsID(opts Options, name string, namespaceID string) (*CreateOr } }` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return nil, err } request.Var("name", name) request.Var("registryNamespaceId", namespaceID) - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) if len(response.CreateOrb.Errors) > 0 { return nil, response.CreateOrb.Errors @@ -695,13 +688,13 @@ func createOrbWithNsID(opts Options, name string, namespaceID string) (*CreateOr } // CreateOrb creates (reserves) an orb within a namespace -func CreateOrb(opts Options, namespace string, name string) (*CreateOrbResponse, error) { - response, err := getNamespace(opts, namespace) +func CreateOrb(cl *client.Client, namespace string, name string) (*CreateOrbResponse, error) { + response, err := getNamespace(cl, namespace) if err != nil { return nil, err } - return createOrbWithNsID(opts, name, response.RegistryNamespace.ID) + return createOrbWithNsID(cl, name, response.RegistryNamespace.ID) } // TODO(zzak): this function is not really related to the API. Move it to another package? @@ -725,14 +718,14 @@ func incrementVersion(version string, segment string) (string, error) { } // OrbIncrementVersion accepts an orb and segment to increment the orb. -func OrbIncrementVersion(opts Options, configPath string, namespace string, orb string, segment string) (*Orb, error) { +func OrbIncrementVersion(cl *client.Client, configPath string, namespace string, orb string, segment string) (*Orb, error) { // TODO(zzak): We can squash OrbID and OrbLatestVersion to a single query - id, err := OrbID(opts, namespace, orb) + id, err := OrbID(cl, namespace, orb) if err != nil { return nil, err } - v, err := OrbLatestVersion(opts, namespace, orb) + v, err := OrbLatestVersion(cl, namespace, orb) if err != nil { return nil, err } @@ -742,7 +735,7 @@ func OrbIncrementVersion(opts Options, configPath string, namespace string, orb return nil, err } - response, err := OrbPublishByID(opts, configPath, id.Orb.ID, v2) + response, err := OrbPublishByID(cl, configPath, id.Orb.ID, v2) if err != nil { return nil, err } @@ -752,7 +745,7 @@ func OrbIncrementVersion(opts Options, configPath string, namespace string, orb // OrbLatestVersion finds the latest published version of an orb and returns it. // If it doesn't find a version, it will return 0.0.0 for the orb's version -func OrbLatestVersion(opts Options, namespace string, orb string) (string, error) { +func OrbLatestVersion(cl *client.Client, namespace string, orb string) (string, error) { name := namespace + "/" + orb var response OrbLatestVersionResponse @@ -766,13 +759,13 @@ func OrbLatestVersion(opts Options, namespace string, orb string) (string, error } }` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return "", err } request.Var("name", name) - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) if err != nil { return "", err } @@ -785,15 +778,15 @@ func OrbLatestVersion(opts Options, namespace string, orb string) (string, error } // OrbPromote takes an orb and a development version and increments a semantic release with the given segment. -func OrbPromote(opts Options, namespace string, orb string, label string, segment string) (*Orb, error) { +func OrbPromote(cl *client.Client, namespace string, orb string, label string, segment string) (*Orb, error) { // TODO(zzak): We can squash OrbID and OrbLatestVersion to a single query - id, err := OrbID(opts, namespace, orb) + id, err := OrbID(cl, namespace, orb) if err != nil { return nil, err } - v, err := OrbLatestVersion(opts, namespace, orb) + v, err := OrbLatestVersion(cl, namespace, orb) if err != nil { return nil, err } @@ -821,7 +814,7 @@ func OrbPromote(opts Options, namespace string, orb string, label string, segmen } ` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return nil, err } @@ -829,7 +822,7 @@ func OrbPromote(opts Options, namespace string, orb string, label string, segmen request.Var("devVersion", label) request.Var("semanticVersion", v2) - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) if len(response.PromoteOrb.Errors) > 0 { return nil, response.PromoteOrb.Errors @@ -856,7 +849,7 @@ func orbVersionRef(orb string) string { } // OrbSource gets the source of an orb -func OrbSource(opts Options, orbRef string) (string, error) { +func OrbSource(cl *client.Client, orbRef string) (string, error) { if err := references.IsOrbRefWithOptionalVersion(orbRef); err != nil { return "", err } @@ -879,7 +872,7 @@ func OrbSource(opts Options, orbRef string) (string, error) { request := client.NewUnauthorizedRequest(query) request.Var("orbVersionRef", ref) - err := opts.Client.Run(opts.Context, request, &response) + err := cl.Run(request, &response) if err != nil { return "", err } @@ -892,7 +885,7 @@ func OrbSource(opts Options, orbRef string) (string, error) { } // OrbInfo gets the meta-data of an orb -func OrbInfo(opts Options, orbRef string) (*OrbVersion, error) { +func OrbInfo(cl *client.Client, orbRef string) (*OrbVersion, error) { if err := references.IsOrbRefWithOptionalVersion(orbRef); err != nil { return nil, err } @@ -929,7 +922,7 @@ func OrbInfo(opts Options, orbRef string) (*OrbVersion, error) { request := client.NewUnauthorizedRequest(query) request.Var("orbVersionRef", ref) - err := opts.Client.Run(opts.Context, request, &response) + err := cl.Run(request, &response) if err != nil { return nil, err } @@ -957,7 +950,7 @@ func OrbInfo(opts Options, orbRef string) (*OrbVersion, error) { // ListOrbs queries the API to find all orbs. // Returns a collection of Orb objects containing their relevant data. -func ListOrbs(opts Options, uncertified bool) (*OrbsForListing, error) { +func ListOrbs(cl *client.Client, uncertified bool) (*OrbsForListing, error) { l := log.New(os.Stderr, "", 0) query := ` @@ -996,7 +989,7 @@ query ListOrbs ($after: String!, $certifiedOnly: Boolean!) { request.Var("after", currentCursor) request.Var("certifiedOnly", !uncertified) - err := opts.Client.Run(opts.Context, request, &result) + err := cl.Run(request, &result) if err != nil { return nil, errors.Wrap(err, "GraphQL query failed") } @@ -1033,7 +1026,7 @@ query ListOrbs ($after: String!, $certifiedOnly: Boolean!) { // ListNamespaceOrbs queries the API to find all orbs belonging to the given // namespace. // Returns a collection of Orb objects containing their relevant data. -func ListNamespaceOrbs(opts Options, namespace string) (*OrbsForListing, error) { +func ListNamespaceOrbs(cl *client.Client, namespace string) (*OrbsForListing, error) { l := log.New(os.Stderr, "", 0) query := ` @@ -1075,7 +1068,7 @@ query namespaceOrbs ($namespace: String, $after: String!) { request.Var("namespace", namespace) orbs.Namespace = namespace - err := opts.Client.Run(opts.Context, request, &result) + err := cl.Run(request, &result) if err != nil { return nil, errors.Wrap(err, "GraphQL query failed") } @@ -1116,7 +1109,7 @@ query namespaceOrbs ($namespace: String, $after: String!) { // IntrospectionQuery makes a query on the API asking for bits of the schema // This query isn't intended to get the entire schema, there are better tools for that. -func IntrospectionQuery(opts Options) (*IntrospectionResponse, error) { +func IntrospectionQuery(cl *client.Client) (*IntrospectionResponse, error) { var response IntrospectionResponse query := `query IntrospectionQuery { @@ -1138,12 +1131,12 @@ func IntrospectionQuery(opts Options) (*IntrospectionResponse, error) { } }` - request, err := client.NewAuthorizedRequest(query, opts.Client.Token) + request, err := client.NewAuthorizedRequest(query, cl.Token) if err != nil { return nil, err } - err = opts.Client.Run(opts.Context, request, &response) + err = cl.Run(request, &response) return &response, err } diff --git a/client/client.go b/client/client.go index af14c590a..95a9fa78b 100644 --- a/client/client.go +++ b/client/client.go @@ -167,8 +167,9 @@ func prepareRequest(ctx context.Context, address string, request *Request) (*htt // Run sends an HTTP request to the GraphQL server and deserializes the response or returns an error. // TODO(zzak): This function is fairly complex, we should refactor it // nolint: gocyclo -func (cl *Client) Run(ctx context.Context, request *Request, resp interface{}) error { +func (cl *Client) Run(request *Request, resp interface{}) error { l := log.New(os.Stderr, "", 0) + ctx := context.Background() select { case <-ctx.Done(): diff --git a/client/client_test.go b/client/client_test.go index c1b7c2161..aa4fc5c1f 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1,14 +1,12 @@ package client import ( - "context" "io" "io/ioutil" "net/http" "net/http/httptest" "regexp" "testing" - "time" ) func TestServerAddress(t *testing.T) { @@ -72,15 +70,12 @@ func TestDoJSON(t *testing.T) { })) defer srv.Close() - ctx := context.Background() client := NewClient(srv.URL, "/", "token", false) - ctx, cancel := context.WithTimeout(ctx, 1*time.Second) - defer cancel() var resp struct { Something string } - err := client.Run(ctx, &Request{Query: "query {}"}, &resp) + err := client.Run(&Request{Query: "query {}"}, &resp) if err != nil { t.Errorf(err.Error()) } @@ -111,8 +106,6 @@ func TestQueryJSON(t *testing.T) { } })) defer srv.Close() - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() client := NewClient(srv.URL, "/", "token", false) @@ -131,7 +124,7 @@ func TestQueryJSON(t *testing.T) { var resp struct { Value string } - err := client.Run(ctx, req, &resp) + err := client.Run(req, &resp) if err != nil { t.Errorf(err.Error()) } @@ -174,15 +167,11 @@ func TestDoJSONErr(t *testing.T) { defer server.Close() - ctx := context.Background() client := NewClient(server.URL, "/", "token", false) - ctx, cancel := context.WithTimeout(ctx, 1*time.Second) - defer cancel() - var responseData map[string]interface{} - err := client.Run(ctx, &Request{Query: "query {}"}, &responseData) + err := client.Run(&Request{Query: "query {}"}, &responseData) if err.Error() != "Something went wrong\nSomething else went wrong" { t.Errorf(err.Error()) } @@ -202,8 +191,6 @@ func TestHeader(t *testing.T) { } })) defer srv.Close() - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() client := NewClient(srv.URL, "/", "token", false) @@ -213,7 +200,7 @@ func TestHeader(t *testing.T) { var resp struct { Value string } - err := client.Run(ctx, req, &resp) + err := client.Run(req, &resp) if err != nil { t.Errorf(err.Error()) } @@ -240,8 +227,6 @@ func TestStatusCode200(t *testing.T) { } })) defer srv.Close() - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() client := NewClient(srv.URL, "/", "token", false) @@ -249,7 +234,7 @@ func TestStatusCode200(t *testing.T) { var resp interface{} - err := client.Run(ctx, req, &resp) + err := client.Run(req, &resp) if err != nil { t.Errorf(err.Error()) } @@ -272,8 +257,6 @@ func TestStatusCode500(t *testing.T) { } })) defer srv.Close() - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() client := NewClient(srv.URL, "/", "token", false) @@ -281,7 +264,7 @@ func TestStatusCode500(t *testing.T) { var resp interface{} - err := client.Run(ctx, req, &resp) + err := client.Run(req, &resp) if err == nil { t.Error("expected error") } @@ -308,8 +291,6 @@ func TestStatusCode413(t *testing.T) { } })) defer srv.Close() - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() client := NewClient(srv.URL, "/", "token", false) @@ -317,7 +298,7 @@ func TestStatusCode413(t *testing.T) { var resp interface{} - err := client.Run(ctx, req, &resp) + err := client.Run(req, &resp) if err == nil { t.Error("expected error") } diff --git a/cmd/config.go b/cmd/config.go index 8e9adb774..05d214459 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,7 +1,6 @@ package cmd import ( - "context" "fmt" "github.com/CircleCI-Public/circleci-cli/api" @@ -17,9 +16,9 @@ import ( const defaultConfigPath = ".circleci/config.yml" type configOptions struct { - cfg *settings.Config - apiOpts api.Options - args []string + cfg *settings.Config + cl *client.Client + args []string } // Path to the config.yml file to operate on. @@ -32,8 +31,7 @@ var configAnnotations = map[string]string{ func newConfigCommand(config *settings.Config) *cobra.Command { opts := configOptions{ - apiOpts: api.Options{}, - cfg: config, + cfg: config, } configCmd := &cobra.Command{ @@ -46,8 +44,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command { Short: "Pack up your CircleCI configuration into a single file.", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return packConfig(opts) @@ -63,8 +60,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command { Short: "Check that the config file is well formed.", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return validateConfig(opts) @@ -83,8 +79,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command { Short: "Process the config.", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return processConfig(opts) @@ -99,8 +94,7 @@ func newConfigCommand(config *settings.Config) *cobra.Command { Short: "Migrate a pre-release 2.0 config to the official release version", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return migrateConfig(opts) @@ -133,7 +127,7 @@ func validateConfig(opts configOptions) error { path = opts.args[0] } - _, err := api.ConfigQuery(opts.apiOpts, path) + _, err := api.ConfigQuery(opts.cl, path) if err != nil { return err @@ -149,7 +143,7 @@ func validateConfig(opts configOptions) error { } func processConfig(opts configOptions) error { - response, err := api.ConfigQuery(opts.apiOpts, opts.args[0]) + response, err := api.ConfigQuery(opts.cl, opts.args[0]) if err != nil { return err diff --git a/cmd/diagnostic.go b/cmd/diagnostic.go index 394ae07ff..c41493545 100644 --- a/cmd/diagnostic.go +++ b/cmd/diagnostic.go @@ -1,7 +1,6 @@ package cmd import ( - "context" "fmt" "os" @@ -13,15 +12,14 @@ import ( ) type diagnosticOptions struct { - cfg *settings.Config - apiOpts api.Options - args []string + cfg *settings.Config + cl *client.Client + args []string } func newDiagnosticCommand(config *settings.Config) *cobra.Command { opts := diagnosticOptions{ - apiOpts: api.Options{}, - cfg: config, + cfg: config, } diagnosticCommand := &cobra.Command{ @@ -29,8 +27,7 @@ func newDiagnosticCommand(config *settings.Config) *cobra.Command { Short: "Check the status of your CircleCI CLI.", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return diagnostic(opts) @@ -56,7 +53,7 @@ https://circleci.com/account/api`) fmt.Println("Trying an introspection query on API... ") - responseIntro, err := api.IntrospectionQuery(opts.apiOpts) + responseIntro, err := api.IntrospectionQuery(opts.cl) if responseIntro.Schema.QueryType.Name == "" { fmt.Println("Unable to make a query against the GraphQL API, please check your settings") if err != nil { @@ -73,7 +70,7 @@ https://circleci.com/account/api`) } } - responseWho, err := api.WhoamiQuery(opts.apiOpts) + responseWho, err := api.WhoamiQuery(opts.cl) if err != nil { return err diff --git a/cmd/namespace.go b/cmd/namespace.go index 79d026b1f..8c2edd6d4 100644 --- a/cmd/namespace.go +++ b/cmd/namespace.go @@ -1,7 +1,6 @@ package cmd import ( - "context" "fmt" "strings" @@ -12,15 +11,14 @@ import ( ) type namespaceOptions struct { - apiOpts api.Options - cfg *settings.Config - args []string + cfg *settings.Config + cl *client.Client + args []string } func newNamespaceCommand(config *settings.Config) *cobra.Command { opts := namespaceOptions{ - apiOpts: api.Options{}, - cfg: config, + cfg: config, } namespaceCmd := &cobra.Command{ @@ -35,8 +33,7 @@ func newNamespaceCommand(config *settings.Config) *cobra.Command { Please note that at this time all namespaces created in the registry are world-readable.`, PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return createNamespace(opts) @@ -57,7 +54,7 @@ Please note that at this time all namespaces created in the registry are world-r func createNamespace(opts namespaceOptions) error { namespaceName := opts.args[0] - _, err := api.CreateNamespace(opts.apiOpts, namespaceName, opts.args[2], strings.ToUpper(opts.args[1])) + _, err := api.CreateNamespace(opts.cl, namespaceName, opts.args[2], strings.ToUpper(opts.args[1])) if err != nil { return err diff --git a/cmd/orb.go b/cmd/orb.go index 3b3b8b563..35c1cfc19 100644 --- a/cmd/orb.go +++ b/cmd/orb.go @@ -2,7 +2,6 @@ package cmd import ( "bytes" - "context" "encoding/json" "fmt" "sort" @@ -17,9 +16,9 @@ import ( ) type orbOptions struct { - apiOpts api.Options - cfg *settings.Config - args []string + cfg *settings.Config + cl *client.Client + args []string listUncertified bool listJSON bool @@ -35,8 +34,7 @@ var orbAnnotations = map[string]string{ func newOrbCommand(config *settings.Config) *cobra.Command { opts := orbOptions{ - apiOpts: api.Options{}, - cfg: config, + cfg: config, } listCommand := &cobra.Command{ @@ -45,8 +43,7 @@ func newOrbCommand(config *settings.Config) *cobra.Command { Args: cobra.MaximumNArgs(1), PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return listOrbs(opts) @@ -68,8 +65,7 @@ func newOrbCommand(config *settings.Config) *cobra.Command { Short: "Validate an orb.yml", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return validateOrb(opts) @@ -84,8 +80,7 @@ func newOrbCommand(config *settings.Config) *cobra.Command { Short: "Validate an orb and print its form after all pre-registration processing", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return processOrb(opts) @@ -102,8 +97,7 @@ func newOrbCommand(config *settings.Config) *cobra.Command { Please note that at this time all orbs published to the registry are world-readable.`, PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return publishOrb(opts) @@ -123,8 +117,7 @@ Please note that at this time all orbs promoted within the registry are world-re Example: 'circleci orb publish promote foo/bar@dev:master major' => foo/bar@1.0.0`, PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return promoteOrb(opts) @@ -144,8 +137,7 @@ Please note that at this time all orbs incremented within the registry are world Example: 'circleci orb publish increment foo/orb.yml foo/bar minor' => foo/bar@1.1.0`, PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return incrementOrb(opts) @@ -165,8 +157,7 @@ Example: 'circleci orb publish increment foo/orb.yml foo/bar minor' => foo/bar@1 Short: "Show the source of an orb", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return showSource(opts) @@ -183,8 +174,7 @@ Example: 'circleci orb publish increment foo/orb.yml foo/bar minor' => foo/bar@1 Short: "Show the meta-data of an orb", PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return orbInfo(opts) @@ -203,8 +193,7 @@ Example: 'circleci orb publish increment foo/orb.yml foo/bar minor' => foo/bar@1 Please note that at this time all orbs created in the registry are world-readable.`, PreRun: func(cmd *cobra.Command, args []string) { opts.args = args - opts.apiOpts.Context = context.Background() - opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) + opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token, config.Debug) }, RunE: func(_ *cobra.Command, _ []string) error { return createOrb(opts) @@ -413,7 +402,7 @@ func listOrbs(opts orbOptions) error { return listNamespaceOrbs(opts) } - orbs, err := api.ListOrbs(opts.apiOpts, opts.listUncertified) + orbs, err := api.ListOrbs(opts.cl, opts.listUncertified) if err != nil { return errors.Wrapf(err, "Failed to list orbs") } @@ -428,7 +417,7 @@ func listOrbs(opts orbOptions) error { func listNamespaceOrbs(opts orbOptions) error { namespace := opts.args[0] - orbs, err := api.ListNamespaceOrbs(opts.apiOpts, namespace) + orbs, err := api.ListNamespaceOrbs(opts.cl, namespace) if err != nil { return errors.Wrapf(err, "Failed to list orbs in namespace `%s`", namespace) } @@ -441,7 +430,7 @@ func listNamespaceOrbs(opts orbOptions) error { } func validateOrb(opts orbOptions) error { - _, err := api.OrbQuery(opts.apiOpts, opts.args[0]) + _, err := api.OrbQuery(opts.cl, opts.args[0]) if err != nil { return err @@ -457,7 +446,7 @@ func validateOrb(opts orbOptions) error { } func processOrb(opts orbOptions) error { - response, err := api.OrbQuery(opts.apiOpts, opts.args[0]) + response, err := api.OrbQuery(opts.cl, opts.args[0]) if err != nil { return err @@ -476,12 +465,12 @@ func publishOrb(opts orbOptions) error { return err } - id, err := api.OrbID(opts.apiOpts, namespace, orb) + id, err := api.OrbID(opts.cl, namespace, orb) if err != nil { return err } - _, err = api.OrbPublishByID(opts.apiOpts, path, id.Orb.ID, version) + _, err = api.OrbPublishByID(opts.cl, path, id.Orb.ID, version) if err != nil { return err } @@ -521,7 +510,7 @@ func incrementOrb(opts orbOptions) error { return err } - response, err := api.OrbIncrementVersion(opts.apiOpts, opts.args[0], namespace, orb, segment) + response, err := api.OrbIncrementVersion(opts.cl, opts.args[0], namespace, orb, segment) if err != nil { return err @@ -549,7 +538,7 @@ func promoteOrb(opts orbOptions) error { return fmt.Errorf("The version '%s' must be a dev version (the string should begin `dev:`)", version) } - response, err := api.OrbPromote(opts.apiOpts, namespace, orb, version, segment) + response, err := api.OrbPromote(opts.cl, namespace, orb, version, segment) if err != nil { return err } @@ -568,7 +557,7 @@ func createOrb(opts orbOptions) error { return err } - _, err = api.CreateOrb(opts.apiOpts, namespace, orb) + _, err = api.CreateOrb(opts.cl, namespace, orb) if err != nil { return err @@ -583,7 +572,7 @@ func createOrb(opts orbOptions) error { func showSource(opts orbOptions) error { ref := opts.args[0] - source, err := api.OrbSource(opts.apiOpts, ref) + source, err := api.OrbSource(opts.cl, ref) if err != nil { return errors.Wrapf(err, "Failed to get source for '%s'", ref) } @@ -594,7 +583,7 @@ func showSource(opts orbOptions) error { func orbInfo(opts orbOptions) error { ref := opts.args[0] - info, err := api.OrbInfo(opts.apiOpts, ref) + info, err := api.OrbInfo(opts.cl, ref) if err != nil { return errors.Wrapf(err, "Failed to get info for '%s'", ref) } diff --git a/cmd/query.go b/cmd/query.go index d39c138bd..3721d9e45 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -1,7 +1,6 @@ package cmd import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -62,7 +61,7 @@ func query(opts queryOptions) error { if err != nil { return err } - err = opts.cl.Run(context.Background(), req, &resp) + err = opts.cl.Run(req, &resp) if err != nil { return errors.Wrap(err, "Error occurred when running query") }