diff --git a/cmd/baremetal/baremetal.go b/cmd/baremetal/baremetal.go index 3d0de2099..e4695f270 100644 --- a/cmd/baremetal/baremetal.go +++ b/cmd/baremetal/baremetal.go @@ -18,7 +18,6 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/config" - "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/remote" ) @@ -36,14 +35,12 @@ const ( ) // NewBaremetalCommand creates a new command for interacting with baremetal using airshipctl. -func NewBaremetalCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { +func NewBaremetalCommand(cfgFactory config.Factory) *cobra.Command { baremetalRootCmd := &cobra.Command{ Use: "baremetal", Short: "Perform actions on baremetal hosts", } - cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath) - baremetalRootCmd.AddCommand(NewEjectMediaCommand(cfgFactory)) baremetalRootCmd.AddCommand(NewPowerOffCommand(cfgFactory)) baremetalRootCmd.AddCommand(NewPowerOnCommand(cfgFactory)) diff --git a/cmd/baremetal/baremetal_test.go b/cmd/baremetal/baremetal_test.go index 3a49b1b16..ec6d3221f 100644 --- a/cmd/baremetal/baremetal_test.go +++ b/cmd/baremetal/baremetal_test.go @@ -20,7 +20,6 @@ import ( "github.com/stretchr/testify/assert" "opendev.org/airship/airshipctl/cmd/baremetal" - "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" ) @@ -29,7 +28,7 @@ func TestBaremetal(t *testing.T) { { Name: "baremetal-with-help", CmdLine: "-h", - Cmd: baremetal.NewBaremetalCommand(&environment.AirshipCTLSettings{}), + Cmd: baremetal.NewBaremetalCommand(nil), }, { Name: "baremetal-ejectmedia-with-help", diff --git a/cmd/cluster/cluster.go b/cmd/cluster/cluster.go index a37ee1903..9ae19eb9a 100644 --- a/cmd/cluster/cluster.go +++ b/cmd/cluster/cluster.go @@ -18,7 +18,6 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/config" - "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/k8s/client" ) @@ -31,15 +30,13 @@ such as getting status and deploying initial infrastructure. ) // NewClusterCommand creates a command for interacting with a Kubernetes cluster. -func NewClusterCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { +func NewClusterCommand(cfgFactory config.Factory) *cobra.Command { clusterRootCmd := &cobra.Command{ Use: "cluster", Short: "Manage Kubernetes clusters", Long: clusterLong[1:], } - cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath) - clusterRootCmd.AddCommand(NewInitCommand(cfgFactory)) clusterRootCmd.AddCommand(NewMoveCommand(cfgFactory)) clusterRootCmd.AddCommand(NewStatusCommand(cfgFactory, client.DefaultClient)) diff --git a/cmd/cluster/cluster_test.go b/cmd/cluster/cluster_test.go index ccb522ad5..d1a19168d 100644 --- a/cmd/cluster/cluster_test.go +++ b/cmd/cluster/cluster_test.go @@ -18,22 +18,15 @@ import ( "testing" "opendev.org/airship/airshipctl/cmd/cluster" - "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" ) func TestNewClusterCommand(t *testing.T) { - fakeRootSettings := &environment.AirshipCTLSettings{ - AirshipConfigPath: "../../testdata/k8s/config.yaml", - KubeConfigPath: "../../testdata/k8s/kubeconfig.yaml", - } - fakeRootSettings.InitConfig() - tests := []*testutil.CmdTest{ { Name: "cluster-cmd-with-help", CmdLine: "--help", - Cmd: cluster.NewClusterCommand(fakeRootSettings), + Cmd: cluster.NewClusterCommand(nil), }, } for _, testcase := range tests { diff --git a/cmd/config/config.go b/cmd/config/config.go index 0c92a5526..cde0dd6b0 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -18,30 +18,16 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/config" - "opendev.org/airship/airshipctl/pkg/environment" ) // NewConfigCommand creates a command for interacting with the airshipctl configuration. -func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { +func NewConfigCommand(cfgFactory config.Factory) *cobra.Command { configRootCmd := &cobra.Command{ Use: "config", DisableFlagsInUseLine: true, Short: "Manage the airshipctl config file", - PersistentPreRun: func(cmd *cobra.Command, args []string) { - if parentPreRun := cmd.Root().PersistentPreRun; parentPreRun != nil { - parentPreRun(cmd.Root(), args) - } - - if cmd.Use == "init" { - rootSettings.Create = true - } - // Load or Initialize airship Config - rootSettings.InitConfig() - }, } - cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath) - configRootCmd.AddCommand(NewGetContextCommand(cfgFactory)) configRootCmd.AddCommand(NewSetContextCommand(cfgFactory)) diff --git a/cmd/config/config_test.go b/cmd/config/config_test.go index c091c9018..3980b1819 100644 --- a/cmd/config/config_test.go +++ b/cmd/config/config_test.go @@ -20,7 +20,6 @@ import ( "testing" cmd "opendev.org/airship/airshipctl/cmd/config" - "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" ) @@ -29,7 +28,7 @@ func TestConfig(t *testing.T) { { Name: "config-cmd-with-help", CmdLine: "--help", - Cmd: cmd.NewConfigCommand(&environment.AirshipCTLSettings{}), + Cmd: cmd.NewConfigCommand(nil), }, } diff --git a/cmd/document/document.go b/cmd/document/document.go index bda9d93ea..e9318d85e 100644 --- a/cmd/document/document.go +++ b/cmd/document/document.go @@ -18,18 +18,15 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/config" - "opendev.org/airship/airshipctl/pkg/environment" ) // NewDocumentCommand creates a new command for managing airshipctl documents -func NewDocumentCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { +func NewDocumentCommand(cfgFactory config.Factory) *cobra.Command { documentRootCmd := &cobra.Command{ Use: "document", Short: "Manage deployment documents", } - cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath) - documentRootCmd.AddCommand(NewPullCommand(cfgFactory)) documentRootCmd.AddCommand(NewPluginCommand()) diff --git a/cmd/document/document_test.go b/cmd/document/document_test.go index 187f0cdb4..db910d00e 100644 --- a/cmd/document/document_test.go +++ b/cmd/document/document_test.go @@ -18,17 +18,15 @@ import ( "testing" "opendev.org/airship/airshipctl/cmd/document" - "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" ) func TestDocument(t *testing.T) { - rootSettings := &environment.AirshipCTLSettings{} tests := []*testutil.CmdTest{ { Name: "document-with-help", CmdLine: "-h", - Cmd: document.NewDocumentCommand(rootSettings), + Cmd: document.NewDocumentCommand(nil), }, { Name: "document-plugin-with-help", diff --git a/cmd/image/image.go b/cmd/image/image.go index 10960e710..ab13cfe0f 100644 --- a/cmd/image/image.go +++ b/cmd/image/image.go @@ -18,18 +18,15 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/config" - "opendev.org/airship/airshipctl/pkg/environment" ) // NewImageCommand creates a new command for managing ISO images using airshipctl. -func NewImageCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { +func NewImageCommand(cfgFactory config.Factory) *cobra.Command { imageRootCmd := &cobra.Command{ Use: "image", Short: "Manage ISO image creation", } - cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath) - imageRootCmd.AddCommand(NewImageBuildCommand(cfgFactory)) return imageRootCmd diff --git a/cmd/image/image_test.go b/cmd/image/image_test.go index 2582a3f28..5d919d878 100644 --- a/cmd/image/image_test.go +++ b/cmd/image/image_test.go @@ -18,7 +18,6 @@ import ( "testing" "opendev.org/airship/airshipctl/cmd/image" - "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" ) @@ -27,7 +26,7 @@ func TestImage(t *testing.T) { { Name: "image-with-help", CmdLine: "-h", - Cmd: image.NewImageCommand(&environment.AirshipCTLSettings{}), + Cmd: image.NewImageCommand(nil), }, } diff --git a/cmd/phase/phase.go b/cmd/phase/phase.go index 66e5e003b..325fa0772 100644 --- a/cmd/phase/phase.go +++ b/cmd/phase/phase.go @@ -18,7 +18,6 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/config" - "opendev.org/airship/airshipctl/pkg/environment" ) const ( @@ -29,15 +28,13 @@ such as getting list and applying specific one. ) // NewPhaseCommand creates a command for interacting with phases -func NewPhaseCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { +func NewPhaseCommand(cfgFactory config.Factory) *cobra.Command { phaseRootCmd := &cobra.Command{ Use: "phase", Short: "Manage phases", Long: clusterLong[1:], } - cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath) - phaseRootCmd.AddCommand(NewApplyCommand(cfgFactory)) phaseRootCmd.AddCommand(NewRenderCommand(cfgFactory)) phaseRootCmd.AddCommand(NewPlanCommand(cfgFactory)) diff --git a/cmd/phase/phase_test.go b/cmd/phase/phase_test.go index 9ae4b07fc..49b490ea8 100644 --- a/cmd/phase/phase_test.go +++ b/cmd/phase/phase_test.go @@ -18,22 +18,15 @@ import ( "testing" "opendev.org/airship/airshipctl/cmd/phase" - "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" ) func TestNewPhaseCommand(t *testing.T) { - fakeRootSettings := &environment.AirshipCTLSettings{ - AirshipConfigPath: "../../testdata/k8s/config.yaml", - KubeConfigPath: "../../testdata/k8s/kubeconfig.yaml", - } - fakeRootSettings.InitConfig() - tests := []*testutil.CmdTest{ { Name: "phase-cmd-with-help", CmdLine: "--help", - Cmd: phase.NewPhaseCommand(fakeRootSettings), + Cmd: phase.NewPhaseCommand(nil), }, } for _, testcase := range tests { diff --git a/cmd/root.go b/cmd/root.go index e23325e72..50d7be255 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,8 +16,10 @@ package cmd import ( "io" + "path/filepath" "github.com/spf13/cobra" + "k8s.io/client-go/tools/clientcmd" // Import to initialize client auth plugins. _ "k8s.io/client-go/plugin/pkg/client/auth" @@ -30,54 +32,76 @@ import ( "opendev.org/airship/airshipctl/cmd/image" "opendev.org/airship/airshipctl/cmd/phase" "opendev.org/airship/airshipctl/cmd/secret" - "opendev.org/airship/airshipctl/pkg/environment" + cfg "opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/log" ) +// RootOptions stores global flags values +type RootOptions struct { + Debug bool + AirshipConfigPath string + KubeConfigPath string +} + // NewAirshipCTLCommand creates a root `airshipctl` command with the default commands attached func NewAirshipCTLCommand(out io.Writer) *cobra.Command { rootCmd, settings := NewRootCommand(out) - return AddDefaultAirshipCTLCommands(rootCmd, settings) + return AddDefaultAirshipCTLCommands(rootCmd, + cfg.CreateFactory(&settings.AirshipConfigPath, &settings.KubeConfigPath)) } // NewRootCommand creates the root `airshipctl` command. All other commands are // subcommands branching from this one -func NewRootCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings) { - var debug bool +func NewRootCommand(out io.Writer) (*cobra.Command, *RootOptions) { + options := &RootOptions{} rootCmd := &cobra.Command{ Use: "airshipctl", Short: "A unified entrypoint to various airship components", SilenceErrors: true, SilenceUsage: true, PersistentPreRun: func(cmd *cobra.Command, args []string) { - log.Init(debug, cmd.OutOrStdout()) + log.Init(options.Debug, cmd.OutOrStdout()) }, } rootCmd.SetOut(out) - rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable verbose output") + initFlags(options, rootCmd) - return rootCmd, makeRootSettings(rootCmd) + return rootCmd, options } // AddDefaultAirshipCTLCommands is a convenience function for adding all of the // default commands to airshipctl -func AddDefaultAirshipCTLCommands(cmd *cobra.Command, settings *environment.AirshipCTLSettings) *cobra.Command { - cmd.AddCommand(baremetal.NewBaremetalCommand(settings)) - cmd.AddCommand(cluster.NewClusterCommand(settings)) +func AddDefaultAirshipCTLCommands(cmd *cobra.Command, factory cfg.Factory) *cobra.Command { + cmd.AddCommand(baremetal.NewBaremetalCommand(factory)) + cmd.AddCommand(cluster.NewClusterCommand(factory)) cmd.AddCommand(completion.NewCompletionCommand()) - cmd.AddCommand(document.NewDocumentCommand(settings)) - cmd.AddCommand(config.NewConfigCommand(settings)) - cmd.AddCommand(image.NewImageCommand(settings)) + cmd.AddCommand(document.NewDocumentCommand(factory)) + cmd.AddCommand(config.NewConfigCommand(factory)) + cmd.AddCommand(image.NewImageCommand(factory)) cmd.AddCommand(secret.NewSecretCommand()) - cmd.AddCommand(phase.NewPhaseCommand(settings)) + cmd.AddCommand(phase.NewPhaseCommand(factory)) 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 +func initFlags(options *RootOptions, cmd *cobra.Command) { + flags := cmd.PersistentFlags() + flags.BoolVar(&options.Debug, "debug", false, "enable verbose output") + + defaultAirshipConfigDir := filepath.Join(cfg.HomeEnvVar, cfg.AirshipConfigDir) + + defaultAirshipConfigPath := filepath.Join(defaultAirshipConfigDir, cfg.AirshipConfig) + flags.StringVar( + &options.AirshipConfigPath, + "airshipconf", + "", + `Path to file for airshipctl configuration. (default "`+defaultAirshipConfigPath+`")`) + + defaultKubeConfigPath := filepath.Join(defaultAirshipConfigDir, cfg.AirshipKubeConfig) + flags.StringVar( + &options.KubeConfigPath, + clientcmd.RecommendedConfigPathFlag, + "", + `Path to kubeconfig associated with airshipctl configuration. (default "`+defaultKubeConfigPath+`")`) } diff --git a/cmd/root_test.go b/cmd/root_test.go index 143e33413..d5e0c67b8 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -24,7 +24,6 @@ import ( "opendev.org/airship/airshipctl/cmd" "opendev.org/airship/airshipctl/cmd/baremetal" - "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" ) @@ -101,6 +100,6 @@ func getDefaultRootCommand(t *testing.T) *cobra.Command { func getSpecializedRootCommand(t *testing.T) *cobra.Command { t.Helper() rootCmd := getVanillaRootCommand(t) - rootCmd.AddCommand(baremetal.NewBaremetalCommand(&environment.AirshipCTLSettings{})) + rootCmd.AddCommand(baremetal.NewBaremetalCommand(nil)) return rootCmd } diff --git a/pkg/config/constants.go b/pkg/config/constants.go index b91e7829d..b296b0dd8 100644 --- a/pkg/config/constants.go +++ b/pkg/config/constants.go @@ -57,6 +57,9 @@ const ( // Modules AirshipDefaultManagementType = redfish.ClientType + + //HomeEnvVar holds value of HOME directory from env + HomeEnvVar = "$HOME" ) // Default values for remote operations