From 1fe5eeb755d73987e4efd7a1294232cf26eaf978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Qu=C3=A9r=C3=A9?= Date: Thu, 23 Jan 2020 12:30:15 +0100 Subject: [PATCH 1/5] chore: sort namespace in scw usage --- cmd/scw/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/scw/main.go b/cmd/scw/main.go index b6903dd8ae..2e504cef4d 100644 --- a/cmd/scw/main.go +++ b/cmd/scw/main.go @@ -42,9 +42,9 @@ func main() { // Import all commands available in CLI from various packages. commands := core.NewCommands() commands.Merge(instance.GetCommands()) + commands.Merge(marketplace.GetCommands()) commands.Merge(initNamespace.GetCommands()) commands.Merge(configNamespace.GetCommands()) - commands.Merge(marketplace.GetCommands()) commands.Merge(autocompleteNamespace.GetCommands()) commands.Merge(versionNamespace.GetCommands()) From b11906ec1fb4a0869c99270cdfd8460697805f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Qu=C3=A9r=C3=A9?= Date: Thu, 23 Jan 2020 12:33:08 +0100 Subject: [PATCH 2/5] add comment --- cmd/scw/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/scw/main.go b/cmd/scw/main.go index 2e504cef4d..147b7ec670 100644 --- a/cmd/scw/main.go +++ b/cmd/scw/main.go @@ -40,6 +40,7 @@ func main() { defer sentry.RecoverPanicAndSendReport(buildInfo) // 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()) From 8b4dcc8e48519279adfc8b349b1859dae751ff0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Qu=C3=A9r=C3=A9?= Date: Fri, 14 Feb 2020 16:18:12 +0100 Subject: [PATCH 3/5] address comments --- cmd/scw/main.go | 25 ++++++++++++++----------- cmd/scw/main_test.go | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 cmd/scw/main_test.go diff --git a/cmd/scw/main.go b/cmd/scw/main.go index 147b7ec670..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,19 +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. - // 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()) - 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(), + ), + })) +} From c0506d33dbd9f5fce0869b519f9cd4828e62427c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Qu=C3=A9r=C3=A9?= Date: Fri, 14 Feb 2020 16:20:23 +0100 Subject: [PATCH 4/5] add golden --- .../test-main-usage-usage.stderr.golden | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cmd/scw/testdata/test-main-usage-usage.stderr.golden 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..a46a3a7646 --- /dev/null +++ b/cmd/scw/testdata/test-main-usage-usage.stderr.golden @@ -0,0 +1,19 @@ +Usage: + scw [command] + +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. From 268f513cc1da8c240788831b1458056a6988d29c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Qu=C3=A9r=C3=A9?= Date: Fri, 14 Feb 2020 16:27:34 +0100 Subject: [PATCH 5/5] update golden --- cmd/scw/testdata/test-main-usage-usage.stderr.golden | 8 ++++---- internal/core/cobra_builder.go | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/scw/testdata/test-main-usage-usage.stderr.golden b/cmd/scw/testdata/test-main-usage-usage.stderr.golden index a46a3a7646..ba21132655 100644 --- a/cmd/scw/testdata/test-main-usage-usage.stderr.golden +++ b/cmd/scw/testdata/test-main-usage-usage.stderr.golden @@ -1,7 +1,7 @@ -Usage: - scw [command] +USAGE: + scw [global-flags] [flags] -Available Commands: +AVAILABLE COMMANDS: instance Instance API marketplace Marketplace API init Initialize the config @@ -10,7 +10,7 @@ Available Commands: version Display cli version help Help about any command -Flags: +FLAGS: -D, --debug Enable debug mode -h, --help help for scw -o, --output string Output format: json or human 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 }