Fix an issue causing command line args to be parsed too late

This commit is contained in:
Ian Howell 2019-05-30 09:54:26 -05:00
parent 3b31b47158
commit 42ace4fd46
5 changed files with 28 additions and 13 deletions

View File

@ -1,26 +1,35 @@
package cmd package cmd
import ( import (
"fmt"
"io" "io"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/ian-howell/airshipctl/cmd/workflow"
"github.com/ian-howell/airshipctl/cmd/bootstrap" "github.com/ian-howell/airshipctl/cmd/bootstrap"
"github.com/ian-howell/airshipctl/cmd/workflow"
"github.com/ian-howell/airshipctl/pkg/environment" "github.com/ian-howell/airshipctl/pkg/environment"
) )
// NewRootCmd creates the root `airshipctl` command. All other commands are // NewRootCmd creates the root `airshipctl` command. All other commands are
// subcommands branching from this one // subcommands branching from this one
func NewRootCmd(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings, error) { func NewRootCmd(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings, error) {
settings := &environment.AirshipCTLSettings{}
rootCmd := &cobra.Command{ rootCmd := &cobra.Command{
Use: "airshipctl", Use: "airshipctl",
Short: "airshipctl is a unified entrypoint to various airship components", 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.SetOutput(out)
rootCmd.AddCommand(NewVersionCommand()) rootCmd.AddCommand(NewVersionCommand())
settings := &environment.AirshipCTLSettings{}
settings.InitFlags(rootCmd) settings.InitFlags(rootCmd)
return rootCmd, settings, nil return rootCmd, settings, nil

View File

@ -61,9 +61,10 @@ func TestWorkflowInit(t *testing.T) {
for _, tt := range cmdTests { for _, tt := range cmdTests {
settings.PluginSettings[workflow.PluginSettingsID] = &wfenv.Settings{ settings.PluginSettings[workflow.PluginSettingsID] = &wfenv.Settings{
KubeClient: kubefake.NewSimpleClientset(tt.CmdTest.Objs...), Initialized: true,
ArgoClient: argofake.NewSimpleClientset(tt.ArgoObjs...), KubeClient: kubefake.NewSimpleClientset(tt.CmdTest.Objs...),
CRDClient: apixv1beta1fake.NewSimpleClientset(tt.CRDObjs...), ArgoClient: argofake.NewSimpleClientset(tt.ArgoObjs...),
CRDClient: apixv1beta1fake.NewSimpleClientset(tt.CRDObjs...),
} }
test.RunTest(t, tt.CmdTest, rootCmd) test.RunTest(t, tt.CmdTest, rootCmd)
} }

View File

@ -52,7 +52,8 @@ func TestWorkflowList(t *testing.T) {
for _, tt := range cmdTests { for _, tt := range cmdTests {
settings.PluginSettings[workflow.PluginSettingsID] = &wfenv.Settings{ settings.PluginSettings[workflow.PluginSettingsID] = &wfenv.Settings{
ArgoClient: argofake.NewSimpleClientset(tt.ArgoObjs...), Initialized: true,
ArgoClient: argofake.NewSimpleClientset(tt.ArgoObjs...),
} }
test.RunTest(t, tt.CmdTest, rootCmd) test.RunTest(t, tt.CmdTest, rootCmd)
} }

View File

@ -17,11 +17,6 @@ func main() {
cmd.AddDefaultAirshipCTLCommands(rootCmd, settings) 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) log.Init(settings, os.Stdout)
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {

View File

@ -27,6 +27,9 @@ type Settings struct {
// CRDClient is an instrument for interacting with CRDs // CRDClient is an instrument for interacting with CRDs
CRDClient apixv1beta1.Interface 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 // InitFlags adds the default settings flags to cmd
@ -38,6 +41,10 @@ func (s *Settings) InitFlags(cmd *cobra.Command) {
// Init assigns default values // Init assigns default values
func (s *Settings) Init() error { func (s *Settings) Init() error {
if s.Initialized {
return nil
}
if s.KubeConfigFilePath == "" { if s.KubeConfigFilePath == "" {
s.KubeConfigFilePath = clientcmd.RecommendedHomeFile s.KubeConfigFilePath = clientcmd.RecommendedHomeFile
} }
@ -62,5 +69,7 @@ func (s *Settings) Init() error {
if err != nil { if err != nil {
return err return err
} }
s.Initialized = true
return nil return nil
} }