From f19a880a120f3e88c268825599623bf6fbabc359 Mon Sep 17 00:00:00 2001 From: Future-Outlier Date: Fri, 13 Oct 2023 05:05:12 +0800 Subject: [PATCH] force flag (#431) Signed-off-by: Future Outlier Co-authored-by: Future Outlier --- .../cmd/config/subcommand/config/config_flags.go | 1 + .../config/subcommand/config/config_flags_test.go | 14 ++++++++++++++ .../cmd/config/subcommand/config/init_flags.go | 2 ++ flytectl/cmd/configuration/configuration.go | 4 +++- flytectl/cmd/configuration/configuration_test.go | 15 ++++++++++++++- 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/flytectl/cmd/config/subcommand/config/config_flags.go b/flytectl/cmd/config/subcommand/config/config_flags.go index 35b08563cd..c3d727a3e2 100755 --- a/flytectl/cmd/config/subcommand/config/config_flags.go +++ b/flytectl/cmd/config/subcommand/config/config_flags.go @@ -53,5 +53,6 @@ func (cfg Config) GetPFlagSet(prefix string) *pflag.FlagSet { cmdFlags.StringVar(&DefaultConfig.Console, fmt.Sprintf("%v%v", prefix, "console"), DefaultConfig.Console, "Endpoint of console, if different than flyte admin") cmdFlags.StringVar(&DefaultConfig.Host, fmt.Sprintf("%v%v", prefix, "host"), DefaultConfig.Host, "Endpoint of flyte admin") cmdFlags.BoolVar(&DefaultConfig.Insecure, fmt.Sprintf("%v%v", prefix, "insecure"), DefaultConfig.Insecure, "Enable insecure mode") + cmdFlags.BoolVar(&DefaultConfig.Force, fmt.Sprintf("%v%v", prefix, "force"), DefaultConfig.Force, "Force to overwrite the default config file without confirmation") return cmdFlags } diff --git a/flytectl/cmd/config/subcommand/config/config_flags_test.go b/flytectl/cmd/config/subcommand/config/config_flags_test.go index 142d145bc3..51f52abb86 100755 --- a/flytectl/cmd/config/subcommand/config/config_flags_test.go +++ b/flytectl/cmd/config/subcommand/config/config_flags_test.go @@ -141,4 +141,18 @@ func TestConfig_SetFlags(t *testing.T) { } }) }) + t.Run("Test_force", func(t *testing.T) { + + t.Run("Override", func(t *testing.T) { + testValue := "1" + + cmdFlags.Set("force", testValue) + if vBool, err := cmdFlags.GetBool("force"); err == nil { + testDecodeJson_Config(t, fmt.Sprintf("%v", vBool), &actual.Force) + + } else { + assert.FailNow(t, err.Error()) + } + }) + }) } diff --git a/flytectl/cmd/config/subcommand/config/init_flags.go b/flytectl/cmd/config/subcommand/config/init_flags.go index 9c31fa998d..cbb8e60be5 100755 --- a/flytectl/cmd/config/subcommand/config/init_flags.go +++ b/flytectl/cmd/config/subcommand/config/init_flags.go @@ -4,6 +4,7 @@ package config var ( DefaultConfig = &Config{ Insecure: false, + Force: false, } ) @@ -12,4 +13,5 @@ type Config struct { Console string `json:"console" pflag:",Endpoint of console, if different than flyte admin"` Host string `json:"host" pflag:",Endpoint of flyte admin"` Insecure bool `json:"insecure" pflag:",Enable insecure mode"` + Force bool `json:"force" pflag:",Force to overwrite the default config file without confirmation"` } diff --git a/flytectl/cmd/configuration/configuration.go b/flytectl/cmd/configuration/configuration.go index e759758164..e6d120b1bb 100644 --- a/flytectl/cmd/configuration/configuration.go +++ b/flytectl/cmd/configuration/configuration.go @@ -70,6 +70,8 @@ func CreateConfigCommand() *cobra.Command { Long: initCmdLong, PFlagProvider: initConfig.DefaultConfig}, } + configCmd.Flags().BoolVar(&initConfig.DefaultConfig.Force, "force", false, "Force to overwrite the default config file without confirmation") + cmdcore.AddCommands(configCmd, getResourcesFuncs) return configCmd } @@ -109,7 +111,7 @@ func initFlytectlConfig(reader io.Reader) error { if _, err := os.Stat(configutil.ConfigFile); os.IsNotExist(err) { _err = configutil.SetupConfig(configutil.ConfigFile, templateStr, templateValues) } else { - if cmdUtil.AskForConfirmation(fmt.Sprintf("This action will overwrite an existing config file at [%s]. Do you want to continue?", configutil.ConfigFile), reader) { + if initConfig.DefaultConfig.Force || cmdUtil.AskForConfirmation(fmt.Sprintf("This action will overwrite an existing config file at [%s]. Do you want to continue?", configutil.ConfigFile), reader) { if err := os.Remove(configutil.ConfigFile); err != nil { return err } diff --git a/flytectl/cmd/configuration/configuration_test.go b/flytectl/cmd/configuration/configuration_test.go index 19c90a13f4..2f48d28597 100644 --- a/flytectl/cmd/configuration/configuration_test.go +++ b/flytectl/cmd/configuration/configuration_test.go @@ -41,12 +41,12 @@ func TestCreateInitCommand(t *testing.T) { assert.Equal(t, initCmdShort, cmdNouns[2].Short) assert.Equal(t, "validate", cmdNouns[3].Use) assert.Equal(t, "Validates the loaded config.", cmdNouns[3].Short) - } func TestSetupConfigFunc(t *testing.T) { var yes = strings.NewReader("Yes") var no = strings.NewReader("No") + var empty = strings.NewReader("") mockOutStream := new(io.Writer) ctx := context.Background() _ = os.Remove(configutil.FlytectlConfig) @@ -59,8 +59,13 @@ func TestSetupConfigFunc(t *testing.T) { initConfig.DefaultConfig.Host = "" assert.Nil(t, err) + initConfig.DefaultConfig.Force = false assert.Nil(t, initFlytectlConfig(yes)) assert.Nil(t, initFlytectlConfig(no)) + + initConfig.DefaultConfig.Force = true + assert.Nil(t, initFlytectlConfig(empty)) + initConfig.DefaultConfig.Host = "flyte.org" assert.Nil(t, initFlytectlConfig(no)) initConfig.DefaultConfig.Host = "localhost:30081" @@ -86,3 +91,11 @@ func TestValidateEndpointName(t *testing.T) { assert.Equal(t, false, validateEndpointName("112.11.1.1:8080/console")) assert.Equal(t, false, validateEndpointName("flyte")) } + +func TestForceFlagInCreateConfigCommand(t *testing.T) { + cmd := CreateConfigCommand() + assert.False(t, initConfig.DefaultConfig.Force) + err := cmd.Flags().Parse([]string{"--force"}) + assert.Nil(t, err) + assert.True(t, initConfig.DefaultConfig.Force) +}