diff --git a/cmd/baremetal/baremetal.go b/cmd/baremetal/baremetal.go index 5cdc86a2f..1ee178384 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/environment" - "opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/remote" ) @@ -41,7 +40,9 @@ func NewBaremetalCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Co Use: "baremetal", Short: "Perform actions on baremetal hosts", PersistentPreRun: func(cmd *cobra.Command, args []string) { - log.Init(rootSettings.Debug, cmd.OutOrStderr()) + if parentPreRun := cmd.Root().PersistentPreRun; parentPreRun != nil { + parentPreRun(cmd.Root(), args) + } // Load or Initialize airship Config rootSettings.InitConfig() diff --git a/cmd/cluster/cluster.go b/cmd/cluster/cluster.go index e49c1b8a4..91e503ec5 100644 --- a/cmd/cluster/cluster.go +++ b/cmd/cluster/cluster.go @@ -19,7 +19,6 @@ import ( "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/k8s/client" - "opendev.org/airship/airshipctl/pkg/log" ) const ( @@ -37,7 +36,9 @@ func NewClusterCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comm Short: "Manage Kubernetes clusters", Long: clusterLong[1:], PersistentPreRun: func(cmd *cobra.Command, args []string) { - log.Init(rootSettings.Debug, cmd.OutOrStderr()) + if parentPreRun := cmd.Root().PersistentPreRun; parentPreRun != nil { + parentPreRun(cmd.Root(), args) + } // Load or Initialize airship Config rootSettings.InitConfig() diff --git a/cmd/config/config.go b/cmd/config/config.go index 3fb5566c7..2ac81bb7f 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -18,7 +18,6 @@ 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. @@ -28,7 +27,10 @@ func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma DisableFlagsInUseLine: true, Short: "Manage the airshipctl config file", PersistentPreRun: func(cmd *cobra.Command, args []string) { - log.Init(rootSettings.Debug, cmd.OutOrStderr()) + if parentPreRun := cmd.Root().PersistentPreRun; parentPreRun != nil { + parentPreRun(cmd.Root(), args) + } + if cmd.Use == "init" { rootSettings.Create = true } diff --git a/cmd/document/document.go b/cmd/document/document.go index 234ec00e0..288d145c3 100644 --- a/cmd/document/document.go +++ b/cmd/document/document.go @@ -18,7 +18,6 @@ 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 @@ -26,11 +25,6 @@ func NewDocumentCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Com documentRootCmd := &cobra.Command{ Use: "document", Short: "Manage deployment documents", - PersistentPreRun: func(cmd *cobra.Command, args []string) { - // Note: config is not loaded here; the kustomize plugin command doesn't - // require it, and multiple use cases fail if we assume the file is there. - log.Init(rootSettings.Debug, cmd.OutOrStderr()) - }, } documentRootCmd.AddCommand(NewPullCommand(rootSettings)) diff --git a/cmd/image/image.go b/cmd/image/image.go index aec253b9b..ab964074a 100644 --- a/cmd/image/image.go +++ b/cmd/image/image.go @@ -18,7 +18,6 @@ import ( "github.com/spf13/cobra" "opendev.org/airship/airshipctl/pkg/environment" - "opendev.org/airship/airshipctl/pkg/log" ) // NewImageCommand creates a new command for managing ISO images using airshipctl. @@ -27,7 +26,9 @@ func NewImageCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comman Use: "image", Short: "Manage ISO image creation", PersistentPreRun: func(cmd *cobra.Command, args []string) { - log.Init(rootSettings.Debug, cmd.OutOrStderr()) + if parentPreRun := cmd.Root().PersistentPreRun; parentPreRun != nil { + parentPreRun(cmd.Root(), args) + } // Load or Initialize airship Config rootSettings.InitConfig() diff --git a/cmd/phase/phase.go b/cmd/phase/phase.go index 4e47410cd..98d099917 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/environment" - "opendev.org/airship/airshipctl/pkg/log" ) const ( @@ -35,7 +34,9 @@ func NewPhaseCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comman Short: "Manage phases", Long: clusterLong[1:], PersistentPreRun: func(cmd *cobra.Command, args []string) { - log.Init(rootSettings.Debug, cmd.OutOrStderr()) + if parentPreRun := cmd.Root().PersistentPreRun; parentPreRun != nil { + parentPreRun(cmd.Root(), args) + } // Load or Initialize airship Config rootSettings.InitConfig() }, diff --git a/cmd/root.go b/cmd/root.go index 32818faa7..e23325e72 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -31,6 +31,7 @@ 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 @@ -42,13 +43,18 @@ func NewAirshipCTLCommand(out io.Writer) *cobra.Command { // 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 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()) + }, } rootCmd.SetOut(out) + rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable verbose output") return rootCmd, makeRootSettings(rootCmd) } diff --git a/pkg/bootstrap/isogen/command_test.go b/pkg/bootstrap/isogen/command_test.go index 76ed46bcf..30c9b209e 100644 --- a/pkg/bootstrap/isogen/command_test.go +++ b/pkg/bootstrap/isogen/command_test.go @@ -234,7 +234,6 @@ func TestVerifyInputs(t *testing.T) { func TestGenerateBootstrapIso(t *testing.T) { t.Run("EnsureCompleteError", func(t *testing.T) { settings := &environment.AirshipCTLSettings{ - Debug: false, AirshipConfigPath: "testdata/config/config", KubeConfigPath: "testdata/config/kubeconfig", Config: &config.Config{}, @@ -248,7 +247,6 @@ func TestGenerateBootstrapIso(t *testing.T) { t.Run("ContextEntryPointError", func(t *testing.T) { settings := &environment.AirshipCTLSettings{ - Debug: false, AirshipConfigPath: "testdata/config/config", KubeConfigPath: "testdata/config/kubeconfig", Config: &config.Config{}, @@ -262,7 +260,6 @@ func TestGenerateBootstrapIso(t *testing.T) { t.Run("NewBundleByPathError", func(t *testing.T) { settings := &environment.AirshipCTLSettings{ - Debug: false, AirshipConfigPath: "testdata/config/config", KubeConfigPath: "testdata/config/kubeconfig", Config: &config.Config{}, @@ -276,7 +273,6 @@ func TestGenerateBootstrapIso(t *testing.T) { t.Run("SelectOneError", func(t *testing.T) { settings := &environment.AirshipCTLSettings{ - Debug: false, AirshipConfigPath: "testdata/config/config", KubeConfigPath: "testdata/config/kubeconfig", Config: &config.Config{}, @@ -291,7 +287,6 @@ func TestGenerateBootstrapIso(t *testing.T) { t.Run("ToObjectError", func(t *testing.T) { settings := &environment.AirshipCTLSettings{ - Debug: false, AirshipConfigPath: "testdata/config/config", KubeConfigPath: "testdata/config/kubeconfig", Config: &config.Config{}, @@ -305,7 +300,6 @@ func TestGenerateBootstrapIso(t *testing.T) { t.Run("verifyInputsError", func(t *testing.T) { settings := &environment.AirshipCTLSettings{ - Debug: false, AirshipConfigPath: "testdata/config/config", KubeConfigPath: "testdata/config/kubeconfig", Config: &config.Config{}, diff --git a/pkg/environment/settings.go b/pkg/environment/settings.go index ab51e8aad..c93038bac 100644 --- a/pkg/environment/settings.go +++ b/pkg/environment/settings.go @@ -29,8 +29,6 @@ import ( // AirshipCTLSettings is a container for all of the settings needed by airshipctl type AirshipCTLSettings struct { - // Debug is used for verbose output - Debug bool AirshipConfigPath string KubeConfigPath string Create bool @@ -40,11 +38,6 @@ type AirshipCTLSettings struct { // InitFlags adds the default settings flags to cmd func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) { flags := cmd.PersistentFlags() - flags.BoolVar( - &a.Debug, - "debug", - false, - "enable verbose output") defaultAirshipConfigDir := filepath.Join(HomeEnvVar, config.AirshipConfigDir) diff --git a/pkg/k8s/poller/poller_test.go b/pkg/k8s/poller/poller_test.go index 7a4929a4c..44bb797bc 100755 --- a/pkg/k8s/poller/poller_test.go +++ b/pkg/k8s/poller/poller_test.go @@ -31,7 +31,6 @@ import ( func TestNewStatusPoller(t *testing.T) { settings := &environment.AirshipCTLSettings{ - Debug: true, Config: config.NewConfig(), KubeConfigPath: "testdata/kubeconfig.yaml", }