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
|
// NewAirshipCTLCommand creates a root `airshipctl` command with the default commands attached
|
||||||
func NewAirshipCTLCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings, error) {
|
func NewAirshipCTLCommand(out io.Writer) *cobra.Command {
|
||||||
rootCmd, settings, err := NewRootCommand(out)
|
rootCmd, settings := NewRootCommand(out)
|
||||||
return AddDefaultAirshipCTLCommands(rootCmd, settings), settings, err
|
return AddDefaultAirshipCTLCommands(rootCmd, settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRootCommand creates the root `airshipctl` command. All other commands are
|
// NewRootCommand creates the root `airshipctl` command. All other commands are
|
||||||
// subcommands branching from this one
|
// subcommands branching from this one
|
||||||
func NewRootCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings, error) {
|
func NewRootCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings) {
|
||||||
settings := &environment.AirshipCTLSettings{}
|
|
||||||
rootCmd := &cobra.Command{
|
rootCmd := &cobra.Command{
|
||||||
Use: "airshipctl",
|
Use: "airshipctl",
|
||||||
Short: "A unified entrypoint to various airship components",
|
Short: "A unified entrypoint to various airship components",
|
||||||
@ -50,11 +49,8 @@ func NewRootCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSetti
|
|||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
}
|
}
|
||||||
rootCmd.SetOut(out)
|
rootCmd.SetOut(out)
|
||||||
rootCmd.AddCommand(NewVersionCommand())
|
|
||||||
|
|
||||||
settings.InitFlags(rootCmd)
|
return rootCmd, makeRootSettings(rootCmd)
|
||||||
|
|
||||||
return rootCmd, settings, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddDefaultAirshipCTLCommands is a convenience function for adding all of the
|
// 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(image.NewImageCommand(settings))
|
||||||
cmd.AddCommand(secret.NewSecretCommand())
|
cmd.AddCommand(secret.NewSecretCommand())
|
||||||
cmd.AddCommand(phase.NewPhaseCommand(settings))
|
cmd.AddCommand(phase.NewPhaseCommand(settings))
|
||||||
|
cmd.AddCommand(NewVersionCommand())
|
||||||
|
|
||||||
return cmd
|
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) {
|
t.Run(tt.name, func(subTest *testing.T) {
|
||||||
// We don't care about the output of this test, so toss
|
// We don't care about the output of this test, so toss
|
||||||
// it into a throwaway &bytes.buffer{}
|
// it into a throwaway &bytes.buffer{}
|
||||||
rootCmd, settings, err := cmd.NewRootCommand(&bytes.Buffer{})
|
rootCmd, settings := cmd.NewRootCommand(&bytes.Buffer{})
|
||||||
require.NoError(t, err)
|
|
||||||
rootCmd.SetArgs(tt.args)
|
rootCmd.SetArgs(tt.args)
|
||||||
|
|
||||||
err = rootCmd.Execute()
|
err := rootCmd.Execute()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
assert.Equal(t, settings.AirshipConfigPath, tt.expected)
|
assert.Equal(t, settings.AirshipConfigPath, tt.expected)
|
||||||
@ -89,15 +88,13 @@ func TestFlagLoading(t *testing.T) {
|
|||||||
|
|
||||||
func getVanillaRootCommand(t *testing.T) *cobra.Command {
|
func getVanillaRootCommand(t *testing.T) *cobra.Command {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
rootCmd, _, err := cmd.NewRootCommand(nil)
|
rootCmd, _ := cmd.NewRootCommand(nil)
|
||||||
require.NoError(t, err, "Could not create root commands")
|
|
||||||
return rootCmd
|
return rootCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDefaultRootCommand(t *testing.T) *cobra.Command {
|
func getDefaultRootCommand(t *testing.T) *cobra.Command {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
rootCmd, _, err := cmd.NewAirshipCTLCommand(nil)
|
rootCmd := cmd.NewAirshipCTLCommand(nil)
|
||||||
require.NoError(t, err, "Could not create root commands")
|
|
||||||
return rootCmd
|
return rootCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +1,2 @@
|
|||||||
A unified entrypoint to various airship components
|
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:
|
Available Commands:
|
||||||
baremetal Perform actions on baremetal hosts
|
baremetal Perform actions on baremetal hosts
|
||||||
help Help about any command
|
help Help about any command
|
||||||
version Show the version number of airshipctl
|
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
|
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
|
||||||
|
@ -24,11 +24,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rootCmd, _, err := cmd.NewAirshipCTLCommand(os.Stdout)
|
rootCmd := cmd.NewAirshipCTLCommand(os.Stdout)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stdout, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remote auto-generated notice
|
// Remote auto-generated notice
|
||||||
rootCmd.DisableAutoGenTag = true
|
rootCmd.DisableAutoGenTag = true
|
||||||
|
8
main.go
8
main.go
@ -22,13 +22,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rootCmd, _, err := cmd.NewAirshipCTLCommand(os.Stdout)
|
if err := cmd.NewAirshipCTLCommand(os.Stdout).Execute(); err != nil {
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stdout, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rootCmd.Execute(); err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user