From bc90c4f39e1ba6374c40226cc9227a54becab2c3 Mon Sep 17 00:00:00 2001 From: Ruslan Aliev Date: Wed, 6 May 2020 05:26:42 -0500 Subject: [PATCH] Move config init and load from root cmd level There are several commands which do not require to load config: completion, help, secret, version. Hovewer, PersistentPreRun call at root level forces airshipctl to load config anyway. Therefore, we can observe unnecessary config load errors instead of actual result. This patch moves config load to subcommand level where it is necessary. Change-Id: I774a7d038c408e71688f7bf04f1cb87a2132a342 --- cmd/baremetal/baremetal.go | 25 ++++++++++++++++--------- cmd/cluster/cluster.go | 7 +++++++ cmd/config/config.go | 7 +++++++ cmd/document/document.go | 7 +++++++ cmd/phase/phase.go | 7 +++++++ cmd/root.go | 7 ------- 6 files changed, 44 insertions(+), 16 deletions(-) diff --git a/cmd/baremetal/baremetal.go b/cmd/baremetal/baremetal.go index 1b0b19539..4929269e5 100644 --- a/cmd/baremetal/baremetal.go +++ b/cmd/baremetal/baremetal.go @@ -18,6 +18,7 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/environment" + "opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/remote" ) @@ -36,33 +37,39 @@ const ( // NewBaremetalCommand creates a new command for interacting with baremetal using airshipctl. func NewBaremetalCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { - cmd := &cobra.Command{ + baremetalRootCmd := &cobra.Command{ Use: "baremetal", Short: "Perform actions on baremetal hosts", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + log.Init(rootSettings.Debug, cmd.OutOrStderr()) + + // Load or Initialize airship Config + rootSettings.InitConfig() + }, } ejectMediaCmd := NewEjectMediaCommand(rootSettings) - cmd.AddCommand(ejectMediaCmd) + baremetalRootCmd.AddCommand(ejectMediaCmd) isoGenCmd := NewISOGenCommand(rootSettings) - cmd.AddCommand(isoGenCmd) + baremetalRootCmd.AddCommand(isoGenCmd) powerOffCmd := NewPowerOffCommand(rootSettings) - cmd.AddCommand(powerOffCmd) + baremetalRootCmd.AddCommand(powerOffCmd) powerOnCmd := NewPowerOnCommand(rootSettings) - cmd.AddCommand(powerOnCmd) + baremetalRootCmd.AddCommand(powerOnCmd) powerStatusCmd := NewPowerStatusCommand(rootSettings) - cmd.AddCommand(powerStatusCmd) + baremetalRootCmd.AddCommand(powerStatusCmd) rebootCmd := NewRebootCommand(rootSettings) - cmd.AddCommand(rebootCmd) + baremetalRootCmd.AddCommand(rebootCmd) remoteDirectCmd := NewRemoteDirectCommand(rootSettings) - cmd.AddCommand(remoteDirectCmd) + baremetalRootCmd.AddCommand(remoteDirectCmd) - return cmd + return baremetalRootCmd } // getHostSelections builds a list of selectors that can be passed to a manager using the name and label flags passed to diff --git a/cmd/cluster/cluster.go b/cmd/cluster/cluster.go index fd51bd6c1..23e6146d3 100644 --- a/cmd/cluster/cluster.go +++ b/cmd/cluster/cluster.go @@ -18,6 +18,7 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/environment" + "opendev.org/airship/airshipctl/pkg/log" ) const ( @@ -34,6 +35,12 @@ func NewClusterCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comm Use: "cluster", Short: "Manage Kubernetes clusters", Long: clusterLong[1:], + PersistentPreRun: func(cmd *cobra.Command, args []string) { + log.Init(rootSettings.Debug, cmd.OutOrStderr()) + + // Load or Initialize airship Config + rootSettings.InitConfig() + }, } clusterRootCmd.AddCommand(NewInitInfraCommand(rootSettings)) diff --git a/cmd/config/config.go b/cmd/config/config.go index 60b28885f..234e1039d 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -18,6 +18,7 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/environment" + "opendev.org/airship/airshipctl/pkg/log" ) // NewConfigCommand creates a command for interacting with the airshipctl configuration. @@ -26,6 +27,12 @@ func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma Use: "config", DisableFlagsInUseLine: true, Short: "Manage the airshipctl config file", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + log.Init(rootSettings.Debug, cmd.OutOrStderr()) + + // Load or Initialize airship Config + rootSettings.InitConfig() + }, } configRootCmd.AddCommand(NewSetClusterCommand(rootSettings)) configRootCmd.AddCommand(NewGetClusterCommand(rootSettings)) diff --git a/cmd/document/document.go b/cmd/document/document.go index c151b0631..b355dd205 100644 --- a/cmd/document/document.go +++ b/cmd/document/document.go @@ -18,6 +18,7 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/environment" + "opendev.org/airship/airshipctl/pkg/log" ) // NewDocumentCommand creates a new command for managing airshipctl documents @@ -25,6 +26,12 @@ func NewDocumentCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Com documentRootCmd := &cobra.Command{ Use: "document", Short: "Manage deployment documents", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + log.Init(rootSettings.Debug, cmd.OutOrStderr()) + + // Load or Initialize airship Config + rootSettings.InitConfig() + }, } documentRootCmd.AddCommand(NewPullCommand(rootSettings)) diff --git a/cmd/phase/phase.go b/cmd/phase/phase.go index c74d5f069..8d7673528 100644 --- a/cmd/phase/phase.go +++ b/cmd/phase/phase.go @@ -19,6 +19,7 @@ import ( "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/k8s/client" + "opendev.org/airship/airshipctl/pkg/log" ) const ( @@ -34,6 +35,12 @@ func NewPhaseCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comman Use: "phase", Short: "Manage phases", Long: clusterLong[1:], + PersistentPreRun: func(cmd *cobra.Command, args []string) { + log.Init(rootSettings.Debug, cmd.OutOrStderr()) + + // Load or Initialize airship Config + rootSettings.InitConfig() + }, } phaseRootCmd.AddCommand(NewApplyCommand(rootSettings, client.DefaultClient)) diff --git a/cmd/root.go b/cmd/root.go index 96c9958c8..370816fee 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,7 +30,6 @@ import ( "opendev.org/airship/airshipctl/cmd/phase" "opendev.org/airship/airshipctl/cmd/secret" "opendev.org/airship/airshipctl/pkg/environment" - "opendev.org/airship/airshipctl/pkg/log" ) // NewAirshipCTLCommand creates a root `airshipctl` command with the default commands attached @@ -48,12 +47,6 @@ func NewRootCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSetti Short: "A unified entrypoint to various airship components", SilenceErrors: true, SilenceUsage: true, - PersistentPreRun: func(cmd *cobra.Command, args []string) { - log.Init(settings.Debug, cmd.OutOrStderr()) - - // Load or Initialize airship Config - settings.InitConfig() - }, } rootCmd.SetOut(out) rootCmd.AddCommand(NewVersionCommand())