Skip to content

Commit

Permalink
cmd: use SetIndent(2) to dump yaml subcommands
Browse files Browse the repository at this point in the history
Signed-off-by: Koichi Shiraishi <[email protected]>
  • Loading branch information
zchee committed Mar 18, 2022
1 parent 263902a commit 03eb272
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 75 deletions.
16 changes: 8 additions & 8 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func newAdminCommand(config *settings.Config) *cobra.Command {
Args: cobra.MinimumNArgs(1),
}
importOrbCommand.Flags().BoolVar(&orbOpts.integrationTesting, "integration-testing", false, "Enable test mode to bypass interactive UI.")
if err := importOrbCommand.Flags().MarkHidden("integration-testing"); err != nil {
panic(err)
}
// if err := importOrbCommand.Flags().MarkHidden("integration-testing"); err != nil {
// panic(err)
// }
importOrbCommand.Flags().BoolVar(&orbOpts.noPrompt, "no-prompt", false, "Disable prompt to bypass interactive UI.")

renameCommand := &cobra.Command{
Expand Down Expand Up @@ -79,9 +79,9 @@ Example:
deleteAliasCommand.Annotations["<name>"] = "The name of the alias to delete"
deleteAliasCommand.Flags().BoolVar(&nsOpts.noPrompt, "no-prompt", false, "Disable prompt to bypass interactive UI.")
deleteAliasCommand.Flags().BoolVar(&nsOpts.integrationTesting, "integration-testing", false, "Enable test mode to bypass interactive UI.")
if err := deleteAliasCommand.Flags().MarkHidden("integration-testing"); err != nil {
panic(err)
}
// if err := deleteAliasCommand.Flags().MarkHidden("integration-testing"); err != nil {
// panic(err)
// }

deleteNamespaceCommand := &cobra.Command{
Use: "delete-namespace <name>",
Expand Down Expand Up @@ -110,7 +110,7 @@ Example:

adminCommand := &cobra.Command{
Use: "admin",
Short: "Administrative operations for a CircleCI Server installation.",
Short: "Administrative operations for a CircleCI Server installation. (hidden)",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
orbOpts.args = args
nsOpts.args = args
Expand All @@ -122,7 +122,7 @@ Example:
// As of writing this comment, that is only for daily update checks.
return rootCmdPreRun(rootOptions)
},
Hidden: true,
Hidden: false,
}

adminCommand.AddCommand(importOrbCommand)
Expand Down
4 changes: 2 additions & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func newLocalExecuteCommand(config *settings.Config) *cobra.Command {
// hidden command for backwards compatibility
func newBuildCommand(config *settings.Config) *cobra.Command {
cmd := newLocalExecuteCommand(config)
cmd.Hidden = true
cmd.Use = "build"
cmd.Hidden = false
cmd.Use = "build (hidden)"
return cmd
}

Expand Down
15 changes: 7 additions & 8 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"io/ioutil"
"strings"

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/CircleCI-Public/circleci-cli/api/graphql"
Expand Down Expand Up @@ -73,9 +74,6 @@ func newConfigCommand(config *settings.Config) *cobra.Command {
validateCommand.Annotations["<path>"] = configAnnotations["<path>"]
validateCommand.PersistentFlags().StringVarP(&configPath, "config", "c", ".circleci/config.yml", "path to config file")
validateCommand.PersistentFlags().BoolVar(&ignoreDeprecatedImages, "ignore-deprecated-images", false, "ignores the deprecated images error")
if err := validateCommand.PersistentFlags().MarkHidden("config"); err != nil {
panic(err)
}
validateCommand.Flags().StringP("org-slug", "o", "", "organization slug (for example: github/example-org), used when a config depends on private orbs belonging to that org")

processCommand := &cobra.Command{
Expand All @@ -97,14 +95,13 @@ func newConfigCommand(config *settings.Config) *cobra.Command {

migrateCommand := &cobra.Command{
Use: "migrate",
Short: "Migrate a pre-release 2.0 config to the official release version",
Short: "Migrate a pre-release 2.0 config to the official release version (hidden)",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
},
RunE: func(_ *cobra.Command, _ []string) error {
return migrateConfig(opts)
},
Hidden: true,
DisableFlagParsing: true,
}
// These flags are for documentation and not actually parsed
Expand Down Expand Up @@ -193,11 +190,13 @@ func packConfig(opts configOptions) error {
return errors.Wrap(err, "An error occurred trying to build the tree")
}

y, err := yaml.Marshal(&tree)
if err != nil {
var s strings.Builder
enc := yaml.NewEncoder(&s)
enc.SetIndent(2)
if err := enc.Encode(&tree); err != nil {
return errors.Wrap(err, "Failed trying to marshal the tree to YAML ")
}
fmt.Printf("%s\n", string(y))
fmt.Print(s.String())
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Please note that at this time all namespaces created in the registry are world-r
createCmd.Annotations["<vcs-type>"] = `Your VCS provider, can be either "github" or "bitbucket"`
createCmd.Annotations["<org-name>"] = `The name used for your organization`

createCmd.Flags().BoolVar(&opts.integrationTesting, "integration-testing", false, "Enable test mode to bypass interactive UI.")
createCmd.Flags().BoolVar(&opts.integrationTesting, "integration-testing", false, "Enable test mode to bypass interactive UI. (hidden)")
if err := createCmd.Flags().MarkHidden("integration-testing"); err != nil {
panic(err)
}
Expand Down
38 changes: 20 additions & 18 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ func newOrbCommand(config *settings.Config) *cobra.Command {

listCommand.PersistentFlags().StringVar(&opts.sortBy, "sort", "", `one of "builds"|"projects"|"orgs"`)
listCommand.PersistentFlags().BoolVarP(&opts.listUncertified, "uncertified", "u", false, "include uncertified orbs")
listCommand.PersistentFlags().BoolVar(&opts.listJSON, "json", false, "print output as json instead of human-readable")
listCommand.PersistentFlags().BoolVar(&opts.listJSON, "json", false, "print output as json instead of human-readable (hidden)")
listCommand.PersistentFlags().BoolVarP(&opts.listDetails, "details", "d", false, "output all the commands, executors, and jobs, along with a tree of their parameters")
listCommand.PersistentFlags().BoolVarP(&opts.private, "private", "", false, "exclusively list private orbs within a namespace")
if err := listCommand.PersistentFlags().MarkHidden("json"); err != nil {
panic(err)
}
// if err := listCommand.PersistentFlags().MarkHidden("json"); err != nil {
// panic(err)
// }

validateCommand := &cobra.Command{
Use: "validate <path>",
Expand Down Expand Up @@ -285,10 +285,10 @@ Please note that at this time all orbs created in the registry are world-readabl
},
}

listCategoriesCommand.PersistentFlags().BoolVar(&opts.listJSON, "json", false, "print output as json instead of human-readable")
if err := listCategoriesCommand.PersistentFlags().MarkHidden("json"); err != nil {
panic(err)
}
listCategoriesCommand.PersistentFlags().BoolVar(&opts.listJSON, "json", false, "print output as json instead of human-readable (hidden)")
// if err := listCategoriesCommand.PersistentFlags().MarkHidden("json"); err != nil {
// panic(err)
// }

addCategorizationToOrbCommand := &cobra.Command{
Use: "add-to-category <namespace>/<orb> \"<category-name>\"",
Expand Down Expand Up @@ -319,10 +319,10 @@ Please note that at this time all orbs created in the registry are world-readabl
}
orbInit.PersistentFlags().BoolVarP(&opts.private, "private", "", false, "initialize a private orb")

orbCreate.Flags().BoolVar(&opts.integrationTesting, "integration-testing", false, "Enable test mode to bypass interactive UI.")
if err := orbCreate.Flags().MarkHidden("integration-testing"); err != nil {
panic(err)
}
orbCreate.Flags().BoolVar(&opts.integrationTesting, "integration-testing", false, "Enable test mode to bypass interactive UI. (hidden)")
// if err := orbCreate.Flags().MarkHidden("integration-testing"); err != nil {
// panic(err)
// }
orbCreate.Flags().BoolVar(&opts.noPrompt, "no-prompt", false, "Disable prompt to bypass interactive UI.")

orbCommand := &cobra.Command{
Expand Down Expand Up @@ -987,13 +987,15 @@ func packOrb(path string) (string, error) {
return "", errors.Wrap(err, "An unexpected error occurred")
}

y, err := yaml.Marshal(&tree)
if err != nil {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
if err := enc.Encode(&tree); err != nil {
return "", errors.Wrap(err, "An unexpected error occurred")
}

var orbSchema OrbSchema
err = yaml.Unmarshal(y, &orbSchema)
err = yaml.Unmarshal(buf.Bytes(), &orbSchema)
if err != nil {
return "", errors.Wrap(err, "An unexpected error occurred")
}
Expand All @@ -1011,12 +1013,12 @@ func packOrb(path string) (string, error) {
return "", err
}

final, err := yaml.Marshal(&orbSchema)
if err != nil {
buf.Reset()
if err := enc.Encode(&orbSchema); err != nil {
return "", errors.Wrap(err, "Failed trying to marshal Orb YAML")
}

return string(final), nil
return buf.String(), nil
}

// Travel down a YAML node, replacing values as we go.
Expand Down
10 changes: 5 additions & 5 deletions cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3332,11 +3332,11 @@ Windows Server 2010
Expect(err).ShouldNot(HaveOccurred())

Eventually(session.Out).Should(gbytes.Say(`commands:
orb:
steps:
- run:
command: echo Hello, world!
name: Say hello
orb:
steps:
- run:
command: echo Hello, world!
name: Say hello
`))
Eventually(session).Should(gexec.Exit(0))
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func newQueryCommand(config *settings.Config) *cobra.Command {

queryCommand := &cobra.Command{
Use: "query <path>",
Short: "Query the CircleCI GraphQL API.",
Hidden: true,
Short: "Query the CircleCI GraphQL API. (hidden)",
Hidden: false,
PreRunE: func(_ *cobra.Command, args []string) error {
opts.args = args
opts.cl = graphql.NewClient(config.HTTPClient, config.Host, config.Endpoint, config.Token, config.Debug)
Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ func MakeCommands() *cobra.Command {

flags := rootCmd.PersistentFlags()

flags.BoolVar(&rootOptions.Debug, "debug", rootOptions.Debug, "Enable debug logging.")
flags.BoolVar(&rootOptions.Debug, "debug", rootOptions.Debug, "Enable debug logging. (hidden)")
flags.StringVar(&rootTokenFromFlag, "token", "", "your token for using CircleCI, also CIRCLECI_CLI_TOKEN")
flags.StringVar(&rootOptions.Host, "host", rootOptions.Host, "URL to your CircleCI host, also CIRCLECI_CLI_HOST")
flags.StringVar(&rootOptions.Endpoint, "endpoint", rootOptions.Endpoint, "URI to your CircleCI GraphQL API endpoint")
flags.StringVar(&rootOptions.GitHubAPI, "github-api", "https://api.github.com/", "Change the default endpoint to GitHub API for retrieving updates")
flags.StringVar(&rootOptions.Endpoint, "endpoint", rootOptions.Endpoint, "URI to your CircleCI GraphQL API endpoint (hidden)")
flags.StringVar(&rootOptions.GitHubAPI, "github-api", "https://api.github.com/", "Change the default endpoint to GitHub API for retrieving updates (hidden)")
flags.BoolVar(&rootOptions.SkipUpdateCheck, "skip-update-check", skipUpdateByDefault(), "Skip the check for updates check run before every command.")

hidden := []string{"github-api", "debug", "endpoint"}
Expand Down
24 changes: 12 additions & 12 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,22 @@ func newSetupCommand(config *settings.Config) *cobra.Command {
},
}

setupCommand.Flags().BoolVar(&opts.integrationTesting, "integration-testing", false, "Enable test mode to bypass interactive UI.")
if err := setupCommand.Flags().MarkHidden("integration-testing"); err != nil {
panic(err)
}
setupCommand.Flags().BoolVar(&opts.integrationTesting, "integration-testing", false, "Enable test mode to bypass interactive UI. (hidden)")
// if err := setupCommand.Flags().MarkHidden("integration-testing"); err != nil {
// panic(err)
// }

setupCommand.Flags().BoolVar(&opts.noPrompt, "no-prompt", false, "Disable prompt to bypass interactive UI. (MUST supply --host and --token)")

setupCommand.Flags().StringVar(&opts.host, "host", "", "URL to your CircleCI host")
if err := setupCommand.Flags().MarkHidden("host"); err != nil {
panic(err)
}
setupCommand.Flags().StringVar(&opts.host, "host", "", "URL to your CircleCI host (hidden)")
// if err := setupCommand.Flags().MarkHidden("host"); err != nil {
// panic(err)
// }

setupCommand.Flags().StringVar(&opts.token, "token", "", "your token for using CircleCI")
if err := setupCommand.Flags().MarkHidden("token"); err != nil {
panic(err)
}
setupCommand.Flags().StringVar(&opts.token, "token", "", "your token for using CircleCI (hidden)")
// if err := setupCommand.Flags().MarkHidden("token"); err != nil {
// panic(err)
// }

return setupCommand
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ func newStepCommand(config *settings.Config) *cobra.Command {

stepCmd := &cobra.Command{
Use: "step",
Short: "Execute steps",
Hidden: true,
Short: "Execute steps (hidden)",
Hidden: false,
DisableFlagParsing: true,
}

haltCmd := &cobra.Command{
Use: "halt",
Short: "Halt the current job and treat it as successful",
Short: "Halt the current job and treat it as successful (hidden)",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
},
RunE: func(_ *cobra.Command, _ []string) error {
return haltRunE(opts)
},
Hidden: true,
Hidden: false,
DisableFlagParsing: true,
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func newSwitchCommand(config *settings.Config) *cobra.Command {
}

return &cobra.Command{
Use: "switch",
Use: "switch (hidden)",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
},
RunE: func(_ *cobra.Command, _ []string) error {
return runSwitch(opts)
},
Hidden: true,
Hidden: false,
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
func newTestsCommand() *cobra.Command {
testsCmd := &cobra.Command{
Use: "tests",
Short: "Collect and split tests so they may be run in parallel.",
Short: "Collect and split tests so they may be run in parallel. (hidden)",
DisableFlagParsing: true,
Hidden: true,
Hidden: false,
RunE: func(_ *cobra.Command, args []string) error {
return proxy.Exec([]string{"tests"}, args)
},
Expand Down
12 changes: 6 additions & 6 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func newUpdateCommand(config *settings.Config) *cobra.Command {

update.AddCommand(&cobra.Command{
Use: "check",
Hidden: true,
Short: "Check if there are any updates available",
Hidden: false,
Short: "Check if there are any updates available (hidden)",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
opts.cfg.SkipUpdateCheck = true
},
Expand All @@ -58,8 +58,8 @@ func newUpdateCommand(config *settings.Config) *cobra.Command {

update.AddCommand(&cobra.Command{
Use: "install",
Hidden: true,
Short: "Update the tool to the latest version",
Hidden: false,
Short: "Update the tool to the latest version (hidden)",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
opts.cfg.SkipUpdateCheck = true
},
Expand All @@ -73,8 +73,8 @@ func newUpdateCommand(config *settings.Config) *cobra.Command {

update.AddCommand(&cobra.Command{
Use: "build-agent",
Hidden: true,
Short: "Update the build agent to the latest version",
Hidden: false,
Short: "Update the build agent to the latest version (hidden)",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
opts.cfg.SkipUpdateCheck = true
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func newUsageCommand(config *settings.Config) *cobra.Command {

return &cobra.Command{
Use: "usage <path> (default is \"docs\")",
Short: "Generate usage documentation in markdown for the CLI.",
Hidden: true,
Short: "Generate usage documentation in markdown for the CLI. (hidden)",
Hidden: false,
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
},
Expand Down

0 comments on commit 03eb272

Please sign in to comment.