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
This commit is contained in:
Ruslan Aliev 2020-05-06 05:26:42 -05:00
parent f334b45638
commit bc90c4f39e
6 changed files with 44 additions and 16 deletions

View File

@ -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

View File

@ -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))

View File

@ -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))

View File

@ -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))

View File

@ -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))

View File

@ -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())