From abd1317566cada0fa83b95b40f5e4db5762399a7 Mon Sep 17 00:00:00 2001 From: Ruslan Aliev Date: Tue, 25 Aug 2020 17:37:19 -0500 Subject: [PATCH] Refactor airshipctl root command As part of config refactoring process, we have to prepare root level command to future changes, optimize function signatures and its logic. Change-Id: I563a7556ce1cca795a74cf2c6a26380467a15a5d Signed-off-by: Ruslan Aliev Relates-To: #327 --- cmd/root.go | 22 +++++++++++-------- cmd/root_test.go | 11 ++++------ .../rootCmd-with-no-subcommands.golden | 14 ------------ .../specialized-rootCmd-with-bootstrap.golden | 1 - docs/tools/generate_cli_docs.go | 6 +---- main.go | 8 +------ 6 files changed, 19 insertions(+), 43 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 82e1157f1..32818faa7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -34,15 +34,14 @@ import ( ) // NewAirshipCTLCommand creates a root `airshipctl` command with the default commands attached -func NewAirshipCTLCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings, error) { - rootCmd, settings, err := NewRootCommand(out) - return AddDefaultAirshipCTLCommands(rootCmd, settings), settings, err +func NewAirshipCTLCommand(out io.Writer) *cobra.Command { + rootCmd, settings := NewRootCommand(out) + return AddDefaultAirshipCTLCommands(rootCmd, settings) } // NewRootCommand creates the root `airshipctl` command. All other commands are // subcommands branching from this one -func NewRootCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings, error) { - settings := &environment.AirshipCTLSettings{} +func NewRootCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings) { rootCmd := &cobra.Command{ Use: "airshipctl", Short: "A unified entrypoint to various airship components", @@ -50,11 +49,8 @@ func NewRootCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSetti SilenceUsage: true, } rootCmd.SetOut(out) - rootCmd.AddCommand(NewVersionCommand()) - settings.InitFlags(rootCmd) - - return rootCmd, settings, nil + return rootCmd, makeRootSettings(rootCmd) } // AddDefaultAirshipCTLCommands is a convenience function for adding all of the @@ -68,6 +64,14 @@ func AddDefaultAirshipCTLCommands(cmd *cobra.Command, settings *environment.Airs cmd.AddCommand(image.NewImageCommand(settings)) cmd.AddCommand(secret.NewSecretCommand()) cmd.AddCommand(phase.NewPhaseCommand(settings)) + cmd.AddCommand(NewVersionCommand()) return cmd } + +// makeRootSettings holds all actions about environment.AirshipCTLSettings +func makeRootSettings(cmd *cobra.Command) *environment.AirshipCTLSettings { + settings := &environment.AirshipCTLSettings{} + settings.InitFlags(cmd) + return settings +} diff --git a/cmd/root_test.go b/cmd/root_test.go index cead57f36..143e33413 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -75,11 +75,10 @@ func TestFlagLoading(t *testing.T) { t.Run(tt.name, func(subTest *testing.T) { // We don't care about the output of this test, so toss // it into a throwaway &bytes.buffer{} - rootCmd, settings, err := cmd.NewRootCommand(&bytes.Buffer{}) - require.NoError(t, err) + rootCmd, settings := cmd.NewRootCommand(&bytes.Buffer{}) rootCmd.SetArgs(tt.args) - err = rootCmd.Execute() + err := rootCmd.Execute() require.NoError(t, err) assert.Equal(t, settings.AirshipConfigPath, tt.expected) @@ -89,15 +88,13 @@ func TestFlagLoading(t *testing.T) { func getVanillaRootCommand(t *testing.T) *cobra.Command { t.Helper() - rootCmd, _, err := cmd.NewRootCommand(nil) - require.NoError(t, err, "Could not create root commands") + rootCmd, _ := cmd.NewRootCommand(nil) return rootCmd } func getDefaultRootCommand(t *testing.T) *cobra.Command { t.Helper() - rootCmd, _, err := cmd.NewAirshipCTLCommand(nil) - require.NoError(t, err, "Could not create root commands") + rootCmd := cmd.NewAirshipCTLCommand(nil) return rootCmd } diff --git a/cmd/testdata/TestRootGoldenOutput/rootCmd-with-no-subcommands.golden b/cmd/testdata/TestRootGoldenOutput/rootCmd-with-no-subcommands.golden index 1fe882fd7..48d5ef65a 100644 --- a/cmd/testdata/TestRootGoldenOutput/rootCmd-with-no-subcommands.golden +++ b/cmd/testdata/TestRootGoldenOutput/rootCmd-with-no-subcommands.golden @@ -1,16 +1,2 @@ A unified entrypoint to various airship components -Usage: - airshipctl [command] - -Available Commands: - help Help about any command - version Show the version number of airshipctl - -Flags: - --airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config") - --debug enable verbose output - -h, --help help for airshipctl - --kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig") - -Use "airshipctl [command] --help" for more information about a command. diff --git a/cmd/testdata/TestRootGoldenOutput/specialized-rootCmd-with-bootstrap.golden b/cmd/testdata/TestRootGoldenOutput/specialized-rootCmd-with-bootstrap.golden index 39244a20d..8100b62d6 100644 --- a/cmd/testdata/TestRootGoldenOutput/specialized-rootCmd-with-bootstrap.golden +++ b/cmd/testdata/TestRootGoldenOutput/specialized-rootCmd-with-bootstrap.golden @@ -6,7 +6,6 @@ Usage: Available Commands: baremetal Perform actions on baremetal hosts help Help about any command - version Show the version number of airshipctl Flags: --airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config") diff --git a/docs/tools/generate_cli_docs.go b/docs/tools/generate_cli_docs.go index a40cfc7ca..9e119630d 100644 --- a/docs/tools/generate_cli_docs.go +++ b/docs/tools/generate_cli_docs.go @@ -24,11 +24,7 @@ import ( ) func main() { - rootCmd, _, err := cmd.NewAirshipCTLCommand(os.Stdout) - if err != nil { - fmt.Fprintln(os.Stdout, err) - os.Exit(1) - } + rootCmd := cmd.NewAirshipCTLCommand(os.Stdout) // Remote auto-generated notice rootCmd.DisableAutoGenTag = true diff --git a/main.go b/main.go index 6cbf6f900..9af4dcf88 100644 --- a/main.go +++ b/main.go @@ -22,13 +22,7 @@ import ( ) func main() { - rootCmd, _, err := cmd.NewAirshipCTLCommand(os.Stdout) - if err != nil { - fmt.Fprintln(os.Stdout, err) - os.Exit(1) - } - - if err := rootCmd.Execute(); err != nil { + if err := cmd.NewAirshipCTLCommand(os.Stdout).Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) }