diff --git a/cmd/scw/main.go b/cmd/scw/main.go index b6903dd8ae..a6add77b85 100644 --- a/cmd/scw/main.go +++ b/cmd/scw/main.go @@ -25,6 +25,19 @@ var ( GoArch = runtime.GOARCH ) +func getCommands() *core.Commands { + // Import all commands available in CLI from various packages. + // NB: Merge order impacts scw usage sort. + commands := core.NewCommands() + commands.Merge(instance.GetCommands()) + commands.Merge(marketplace.GetCommands()) + commands.Merge(initNamespace.GetCommands()) + commands.Merge(configNamespace.GetCommands()) + commands.Merge(autocompleteNamespace.GetCommands()) + commands.Merge(versionNamespace.GetCommands()) + return commands +} + func main() { buildInfo := &core.BuildInfo{ Version: version.Must(version.NewSemver(Version)), // panic when version does not respect semantic versionning @@ -39,18 +52,9 @@ func main() { // Catch every panic after this line. This will send an anonymous report on Scaleway's sentry. defer sentry.RecoverPanicAndSendReport(buildInfo) - // Import all commands available in CLI from various packages. - commands := core.NewCommands() - commands.Merge(instance.GetCommands()) - commands.Merge(initNamespace.GetCommands()) - commands.Merge(configNamespace.GetCommands()) - commands.Merge(marketplace.GetCommands()) - commands.Merge(autocompleteNamespace.GetCommands()) - commands.Merge(versionNamespace.GetCommands()) - exitCode, _, _ := core.Bootstrap(&core.BootstrapConfig{ Args: os.Args, - Commands: commands, + Commands: getCommands(), BuildInfo: buildInfo, Stdout: os.Stdout, Stderr: os.Stderr, diff --git a/cmd/scw/main_test.go b/cmd/scw/main_test.go new file mode 100644 index 0000000000..cfd14d2f21 --- /dev/null +++ b/cmd/scw/main_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "testing" + + "github.com/scaleway/scaleway-cli/internal/core" +) + +func Test_MainUsage(t *testing.T) { + t.Run("usage", core.Test(&core.TestConfig{ + Commands: getCommands(), + Cmd: "scw -h", + Check: core.TestCheckCombine( + core.TestCheckExitCode(0), + core.TestCheckGolden(), + ), + })) +} diff --git a/cmd/scw/testdata/test-main-usage-usage.stderr.golden b/cmd/scw/testdata/test-main-usage-usage.stderr.golden new file mode 100644 index 0000000000..ba21132655 --- /dev/null +++ b/cmd/scw/testdata/test-main-usage-usage.stderr.golden @@ -0,0 +1,19 @@ +USAGE: + scw [global-flags] [flags] + +AVAILABLE COMMANDS: + instance Instance API + marketplace Marketplace API + init Initialize the config + config Config file management + autocomplete Install autocompletion script + version Display cli version + help Help about any command + +FLAGS: + -D, --debug Enable debug mode + -h, --help help for scw + -o, --output string Output format: json or human + -p, --profile string The config profile to use + +Use "scw [command] --help" for more information about a command. diff --git a/internal/core/cobra_builder.go b/internal/core/cobra_builder.go index 0c11cab25d..15dc13c9ed 100644 --- a/internal/core/cobra_builder.go +++ b/internal/core/cobra_builder.go @@ -76,6 +76,7 @@ func (b *cobraBuilder) build() *cobra.Command { b.hydrateCobra(index[k], commandsIndex[k]) } + b.hydrateCobra(rootCmd, &Command{}) return rootCmd }