Skip to content

Commit

Permalink
force flag (#431)
Browse files Browse the repository at this point in the history
Signed-off-by: Future Outlier <[email protected]>
Co-authored-by: Future Outlier <[email protected]>
  • Loading branch information
Future-Outlier and Future Outlier authored Oct 12, 2023
1 parent 49714a3 commit 7f80f02
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions flytectl/cmd/config/subcommand/config/config_flags.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions flytectl/cmd/config/subcommand/config/config_flags_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions flytectl/cmd/config/subcommand/config/init_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package config
var (
DefaultConfig = &Config{
Insecure: false,
Force: false,
}
)

Expand All @@ -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"`
}
4 changes: 3 additions & 1 deletion flytectl/cmd/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 14 additions & 1 deletion flytectl/cmd/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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)
}

0 comments on commit 7f80f02

Please sign in to comment.