cmd/cli add settings set command (#12152)

This commit is contained in:
andig 2024-02-08 20:23:46 +01:00 • committed by GitHub
parent 19e90380b8
commit fcfceb4937
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 58 additions and 12 deletions

View file

@ -40,6 +40,7 @@ const (
flagDigits = "digits"
flagDelay = "delay"
flagForce = "force"
)
func bind(cmd *cobra.Command, key string, flagName ...string) {

View file

@ -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)
}()
}

57
cmd/settings-set.go Normal file
View file

@ -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()
}