Skip to content

Commit

Permalink
cmd/cli add settings set command (evcc-io#12152)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Feb 8, 2024
1 parent 19e9038 commit fcfceb4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
1 change: 1 addition & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (

flagDigits = "digits"
flagDelay = "delay"
flagForce = "force"
)

func bind(cmd *cobra.Command, key string, flagName ...string) {
Expand Down
12 changes: 0 additions & 12 deletions cmd/settings-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package cmd
import (
"fmt"
"os"
"os/signal"
"regexp"
"syscall"
"text/tabwriter"

"github.com/evcc-io/evcc/server/db/settings"
Expand Down Expand Up @@ -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 changes: 57 additions & 0 deletions cmd/settings-set.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit fcfceb4

Please sign in to comment.