Fix an issue causing command line args to be parsed too late
This commit is contained in:
parent
3b31b47158
commit
42ace4fd46
13
cmd/root.go
13
cmd/root.go
@ -1,26 +1,35 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/ian-howell/airshipctl/cmd/workflow"
|
||||
"github.com/ian-howell/airshipctl/cmd/bootstrap"
|
||||
"github.com/ian-howell/airshipctl/cmd/workflow"
|
||||
"github.com/ian-howell/airshipctl/pkg/environment"
|
||||
)
|
||||
|
||||
// NewRootCmd creates the root `airshipctl` command. All other commands are
|
||||
// subcommands branching from this one
|
||||
func NewRootCmd(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings, error) {
|
||||
settings := &environment.AirshipCTLSettings{}
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "airshipctl",
|
||||
Short: "airshipctl is a unified entrypoint to various airship components",
|
||||
SilenceErrors: true,
|
||||
SilenceUsage: true,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := settings.Init(); err != nil {
|
||||
return fmt.Errorf("error while initializing settings: %s", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
rootCmd.SetOutput(out)
|
||||
rootCmd.AddCommand(NewVersionCommand())
|
||||
|
||||
settings := &environment.AirshipCTLSettings{}
|
||||
settings.InitFlags(rootCmd)
|
||||
|
||||
return rootCmd, settings, nil
|
||||
|
@ -61,6 +61,7 @@ func TestWorkflowInit(t *testing.T) {
|
||||
|
||||
for _, tt := range cmdTests {
|
||||
settings.PluginSettings[workflow.PluginSettingsID] = &wfenv.Settings{
|
||||
Initialized: true,
|
||||
KubeClient: kubefake.NewSimpleClientset(tt.CmdTest.Objs...),
|
||||
ArgoClient: argofake.NewSimpleClientset(tt.ArgoObjs...),
|
||||
CRDClient: apixv1beta1fake.NewSimpleClientset(tt.CRDObjs...),
|
||||
|
@ -52,6 +52,7 @@ func TestWorkflowList(t *testing.T) {
|
||||
|
||||
for _, tt := range cmdTests {
|
||||
settings.PluginSettings[workflow.PluginSettingsID] = &wfenv.Settings{
|
||||
Initialized: true,
|
||||
ArgoClient: argofake.NewSimpleClientset(tt.ArgoObjs...),
|
||||
}
|
||||
test.RunTest(t, tt.CmdTest, rootCmd)
|
||||
|
5
main.go
5
main.go
@ -17,11 +17,6 @@ func main() {
|
||||
|
||||
cmd.AddDefaultAirshipCTLCommands(rootCmd, settings)
|
||||
|
||||
// Flags may not be parsed until all subcommands have been added
|
||||
rootCmd.PersistentFlags().Parse(os.Args[1:])
|
||||
|
||||
settings.Init()
|
||||
|
||||
log.Init(settings, os.Stdout)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
|
@ -27,6 +27,9 @@ type Settings struct {
|
||||
|
||||
// CRDClient is an instrument for interacting with CRDs
|
||||
CRDClient apixv1beta1.Interface
|
||||
|
||||
// Initialized denotes whether the settings have been initialized or not. It is useful for unit-testing
|
||||
Initialized bool
|
||||
}
|
||||
|
||||
// InitFlags adds the default settings flags to cmd
|
||||
@ -38,6 +41,10 @@ func (s *Settings) InitFlags(cmd *cobra.Command) {
|
||||
|
||||
// Init assigns default values
|
||||
func (s *Settings) Init() error {
|
||||
if s.Initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
if s.KubeConfigFilePath == "" {
|
||||
s.KubeConfigFilePath = clientcmd.RecommendedHomeFile
|
||||
}
|
||||
@ -62,5 +69,7 @@ func (s *Settings) Init() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.Initialized = true
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user