From fcfceb493706a2910776c05ec3166231db8fa8a7 Mon Sep 17 00:00:00 2001 From: andig Date: Thu, 8 Feb 2024 20:23:46 +0100 Subject: [PATCH] cmd/cli add settings set command (#12152) --- cmd/flags.go | 1 + cmd/settings-get.go | 12 ---------- cmd/settings-set.go | 57 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 cmd/settings-set.go diff --git a/cmd/flags.go b/cmd/flags.go index 5e019e335..0436d4be9 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -40,6 +40,7 @@ const ( flagDigits = "digits" flagDelay = "delay" + flagForce = "force" ) func bind(cmd *cobra.Command, key string, flagName ...string) { diff --git a/cmd/settings-get.go b/cmd/settings-get.go index 325db4a07..12f4ecc88 100644 --- a/cmd/settings-get.go +++ b/cmd/settings-get.go @@ -3,9 +3,7 @@ package cmd import ( "fmt" "os" - "os/signal" "regexp" - "syscall" "text/tabwriter" "github.com/evcc-io/evcc/server/db/settings" @@ -49,14 +47,4 @@ func runSettingsGet(cmd *cobra.Command, args []string) { fmt.Fprintf(w, "%s:\t%s\n", s.Key, s.Value) } w.Flush() - - // catch signals - go func() { - signalC := make(chan os.Signal, 1) - signal.Notify(signalC, os.Interrupt, syscall.SIGTERM) - - <-signalC // wait for signal - - os.Exit(1) - }() } diff --git a/cmd/settings-set.go b/cmd/settings-set.go new file mode 100644 index 000000000..cfdbabe2c --- /dev/null +++ b/cmd/settings-set.go @@ -0,0 +1,57 @@ +package cmd + +import ( + "fmt" + + "github.com/AlecAivazis/survey/v2" + "github.com/evcc-io/evcc/server/db/settings" + "github.com/spf13/cobra" +) + +// settingsSetCmd represents the configure command +var settingsSetCmd = &cobra.Command{ + Use: "set", + Short: "Set configuration setting", + Run: runSettingsSet, + Args: cobra.ExactArgs(2), +} + +func init() { + settingsCmd.AddCommand(settingsSetCmd) + settingsSetCmd.Flags().BoolP(flagForce, "f", false, "Force (no confirmation)") +} + +func runSettingsSet(cmd *cobra.Command, args []string) { + // load config + if err := loadConfigFile(&conf); err != nil { + log.FATAL.Fatal(err) + } + + // setup environment + if err := configureEnvironment(cmd, conf); err != nil { + log.FATAL.Fatal(err) + } + + confirmation, _ := cmd.Flags().GetBool(flagForce) + if !confirmation { + msg := fmt.Sprintf("Set %s", args[0]) + if val, _ := settings.String(args[0]); val != "" { + msg = fmt.Sprintf("Override %s (current value: %s)", args[0], val) + } + + prompt := &survey.Confirm{ + Message: msg, + } + + if err := survey.AskOne(prompt, &confirmation); err != nil { + log.FATAL.Fatal(err) + } + } + + if confirmation { + settings.SetString(args[0], args[1]) + } + + // wait for shutdown + <-shutdownDoneC() +}