Skip to content

Commit

Permalink
Merge pull request #181 from CircleCI-Public/orb-info
Browse files Browse the repository at this point in the history
[CIRCLE-14052]: Add `orb info` which reports meta-data about an orb
  • Loading branch information
Zachary Scott authored Oct 30, 2018
2 parents 1dca9bc + a7e08ce commit 7652a4f
Show file tree
Hide file tree
Showing 10 changed files with 668 additions and 238 deletions.
302 changes: 182 additions & 120 deletions api/api.go

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ var _ = BeforeSuite(func() {
Ω(err).ShouldNot(HaveOccurred())
})

func withTempHomeConfig() (string, *os.File) {
var (
tempHome string
err error
config *os.File
)

tempHome, err = ioutil.TempDir("", "circleci-cli-test-")
Expect(err).ToNot(HaveOccurred())

const (
configDir = ".circleci"
configFile = "cli.yml"
)

Expect(os.Mkdir(filepath.Join(tempHome, configDir), 0700)).To(Succeed())

config, err = os.OpenFile(
filepath.Join(tempHome, configDir, configFile),
os.O_RDWR|os.O_CREATE,
0600,
)
Expect(err).ToNot(HaveOccurred())

return tempHome, config
}

var _ = AfterSuite(func() {
gexec.CleanupBuildArtifacts()
})
Expand Down
44 changes: 23 additions & 21 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import (
const defaultConfigPath = ".circleci/config.yml"

type configOptions struct {
cfg *settings.Config
cl *client.Client
log *logger.Logger
args []string
cfg *settings.Config
apiOpts api.Options
args []string
}

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

func newConfigCommand(config *settings.Config) *cobra.Command {
opts := configOptions{
cfg: config,
apiOpts: api.Options{},
cfg: config,
}

configCmd := &cobra.Command{
Expand All @@ -46,8 +46,9 @@ 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.log = logger.NewLogger(config.Debug)
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token)
opts.apiOpts.Context = context.Background()
opts.apiOpts.Log = logger.NewLogger(config.Debug)
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token)
},
RunE: func(_ *cobra.Command, _ []string) error {
return packConfig(opts)
Expand All @@ -63,8 +64,9 @@ 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.log = logger.NewLogger(config.Debug)
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token)
opts.apiOpts.Context = context.Background()
opts.apiOpts.Log = logger.NewLogger(config.Debug)
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token)
},
RunE: func(_ *cobra.Command, _ []string) error {
return validateConfig(opts)
Expand All @@ -83,8 +85,9 @@ func newConfigCommand(config *settings.Config) *cobra.Command {
Short: "Process the config.",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
opts.log = logger.NewLogger(config.Debug)
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token)
opts.apiOpts.Context = context.Background()
opts.apiOpts.Log = logger.NewLogger(config.Debug)
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token)
},
RunE: func(_ *cobra.Command, _ []string) error {
return processConfig(opts)
Expand All @@ -99,8 +102,9 @@ 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.log = logger.NewLogger(config.Debug)
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token)
opts.apiOpts.Context = context.Background()
opts.apiOpts.Log = logger.NewLogger(config.Debug)
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token)
},
RunE: func(_ *cobra.Command, _ []string) error {
return migrateConfig(opts)
Expand Down Expand Up @@ -133,31 +137,29 @@ func validateConfig(opts configOptions) error {
path = opts.args[0]
}

ctx := context.Background()
_, err := api.ConfigQuery(ctx, opts.log, opts.cl, path)
_, err := api.ConfigQuery(opts.apiOpts, path)

if err != nil {
return err
}

if path == "-" {
opts.log.Infof("Config input is valid.")
opts.apiOpts.Log.Infof("Config input is valid.")
} else {
opts.log.Infof("Config file at %s is valid.", path)
opts.apiOpts.Log.Infof("Config file at %s is valid.", path)
}

return nil
}

func processConfig(opts configOptions) error {
ctx := context.Background()
response, err := api.ConfigQuery(ctx, opts.log, opts.cl, opts.args[0])
response, err := api.ConfigQuery(opts.apiOpts, opts.args[0])

if err != nil {
return err
}

opts.log.Info(response.OutputYaml)
opts.apiOpts.Log.Info(response.OutputYaml)
return nil
}

Expand All @@ -171,7 +173,7 @@ func packConfig(opts configOptions) error {
if err != nil {
return errors.Wrap(err, "Failed trying to marshal the tree to YAML ")
}
opts.log.Infof("%s\n", string(y))
opts.apiOpts.Log.Infof("%s\n", string(y))
return nil
}

Expand Down
43 changes: 23 additions & 20 deletions cmd/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ import (
)

type diagnosticOptions struct {
cfg *settings.Config
cl *client.Client
log *logger.Logger
args []string
cfg *settings.Config
apiOpts api.Options
args []string
}

func newDiagnosticCommand(config *settings.Config) *cobra.Command {
opts := diagnosticOptions{
cfg: config,
apiOpts: api.Options{},
cfg: config,
}

diagnosticCommand := &cobra.Command{
Use: "diagnostic",
Short: "Check the status of your CircleCI CLI.",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
opts.log = logger.NewLogger(config.Debug)
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token)
opts.apiOpts.Context = context.Background()
opts.apiOpts.Log = logger.NewLogger(config.Debug)
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token)
},
RunE: func(_ *cobra.Command, _ []string) error {
return diagnostic(opts)
Expand All @@ -40,38 +41,40 @@ func newDiagnosticCommand(config *settings.Config) *cobra.Command {
}

func diagnostic(opts diagnosticOptions) error {
opts.log.Infoln("\n---\nCircleCI CLI Diagnostics\n---")
opts.log.Infof("Debugger mode: %v\n", opts.cfg.Debug)
opts.log.Infof("Config found: %v\n", opts.cfg.FileUsed)
opts.log.Infof("API host: %s\n", opts.cfg.Host)
opts.log.Infof("API endpoint: %s\n", opts.cfg.Endpoint)
log := opts.apiOpts.Log
log.Infoln("\n---\nCircleCI CLI Diagnostics\n---")
log.Infof("Debugger mode: %v\n", opts.cfg.Debug)
log.Infof("Config found: %v\n", opts.cfg.FileUsed)
log.Infof("API host: %s\n", opts.cfg.Host)
log.Infof("API endpoint: %s\n", opts.cfg.Endpoint)

if opts.cfg.Token == "token" || opts.cfg.Token == "" {
return errors.New("please set a token with 'circleci setup'")
}
opts.log.Infoln("OK, got a token.")
log.Infoln("OK, got a token.")

opts.log.Infoln("Trying an introspection query on API... ")
responseIntro, err := api.IntrospectionQuery(context.Background(), opts.log, opts.cl)
log.Infoln("Trying an introspection query on API... ")

responseIntro, err := api.IntrospectionQuery(opts.apiOpts)
if responseIntro.Schema.QueryType.Name == "" {
opts.log.Infoln("Unable to make a query against the GraphQL API, please check your settings")
log.Infoln("Unable to make a query against the GraphQL API, please check your settings")
if err != nil {
return err
}
}

opts.log.Infoln("Ok.")
log.Infoln("Ok.")

opts.log.Debug("Introspection query result with Schema.QueryType of %s", responseIntro.Schema.QueryType.Name)
log.Debug("Introspection query result with Schema.QueryType of %s", responseIntro.Schema.QueryType.Name)

responseWho, err := api.WhoamiQuery(context.Background(), opts.log, opts.cl)
responseWho, err := api.WhoamiQuery(opts.apiOpts)

if err != nil {
return err
}

if responseWho.Me.Name != "" {
opts.log.Infof("Hello, %s.\n", responseWho.Me.Name)
log.Infof("Hello, %s.\n", responseWho.Me.Name)
}

return nil
Expand Down
22 changes: 11 additions & 11 deletions cmd/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
)

type namespaceOptions struct {
cfg *settings.Config
cl *client.Client
log *logger.Logger
args []string
apiOpts api.Options
cfg *settings.Config
args []string
}

func newNamespaceCommand(config *settings.Config) *cobra.Command {
opts := namespaceOptions{
cfg: config,
apiOpts: api.Options{},
cfg: config,
}

namespaceCmd := &cobra.Command{
Expand All @@ -35,8 +35,9 @@ 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.log = logger.NewLogger(config.Debug)
opts.cl = client.NewClient(config.Host, config.Endpoint, config.Token)
opts.apiOpts.Context = context.Background()
opts.apiOpts.Log = logger.NewLogger(config.Debug)
opts.apiOpts.Client = client.NewClient(config.Host, config.Endpoint, config.Token)
},
RunE: func(_ *cobra.Command, _ []string) error {
return createNamespace(opts)
Expand All @@ -55,16 +56,15 @@ Please note that at this time all namespaces created in the registry are world-r
}

func createNamespace(opts namespaceOptions) error {
ctx := context.Background()
namespaceName := opts.args[0]

_, err := api.CreateNamespace(ctx, opts.log, opts.cl, namespaceName, opts.args[2], strings.ToUpper(opts.args[1]))
_, err := api.CreateNamespace(opts.apiOpts, namespaceName, opts.args[2], strings.ToUpper(opts.args[1]))

if err != nil {
return err
}

opts.log.Infof("Namespace `%s` created.", namespaceName)
opts.log.Info("Please note that any orbs you publish in this namespace are open orbs and are world-readable.")
opts.apiOpts.Log.Infof("Namespace `%s` created.", namespaceName)
opts.apiOpts.Log.Info("Please note that any orbs you publish in this namespace are open orbs and are world-readable.")
return nil
}
Loading

0 comments on commit 7652a4f

Please sign in to comment.