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 <raliev@mirantis.com> Relates-To: #327
This commit is contained in:
parent
160117af3a
commit
abd1317566
22
cmd/root.go
22
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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
|
8
main.go
8
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user