Skip to content

Commit

Permalink
cli: add config rm command
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Jutteau <[email protected]>
  • Loading branch information
jerome-jutteau committed Mar 16, 2022
1 parent 8a0d322 commit b0d16c9
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion cmd/frieza/cli_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
func cliConfig() cli.Command {
return cli.NewCommand("config", "configure frieza options").
WithCommand(cliConfigLs()).
WithCommand(cliConfigSet())
WithCommand(cliConfigSet()).
WithCommand(cliConfigRm())
}

func cliConfigLs() cli.Command {
Expand All @@ -35,6 +36,17 @@ func cliConfigSet() cli.Command {
})
}

func cliConfigRm() cli.Command {
return cli.NewCommand("remove", "delete a specific option (reset to default value)").
WithShortcut("rm").
WithOption(cliConfigPath()).
WithArg(cli.NewArg("option_name", "option's name to remove/unset")).
WithAction(func(args []string, options map[string]string) int {
configRm(options["config"], &args[0])
return 0
})
}

func configLs(customConfigPath string) {
var configPath *string
if len(customConfigPath) > 0 {
Expand Down Expand Up @@ -71,3 +83,23 @@ func configSet(customConfigPath string, optionName *string, optionValue *string)
log.Fatalf("Cannot save configuration file: %s", err.Error())
}
}

func configRm(customConfigPath string, optionName *string) {
var configPath *string
if len(customConfigPath) > 0 {
configPath = &customConfigPath
}
config, err := ConfigLoadWithDefault(configPath)
if err != nil {
log.Fatal("Cannot load configuration: " + err.Error())
}
switch *optionName {
case "snapshot_folder_path":
config.SnapshotFolderPath = ""
default:
log.Fatalf("Unknow option name")
}
if err = config.Write(configPath); err != nil {
log.Fatalf("Cannot save configuration file: %s", err.Error())
}
}

0 comments on commit b0d16c9

Please sign in to comment.