From b0d16c90711308fe084e22f9612928f397d6d0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Jutteau?= Date: Tue, 15 Mar 2022 16:51:49 +0100 Subject: [PATCH] cli: add config rm command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Jutteau --- cmd/frieza/cli_config.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/cmd/frieza/cli_config.go b/cmd/frieza/cli_config.go index a560a9b..6beecc2 100644 --- a/cmd/frieza/cli_config.go +++ b/cmd/frieza/cli_config.go @@ -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 { @@ -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 { @@ -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()) + } +}