Add dump --cfg command and cleanup flags (#4668)

This commit is contained in:
andig 2022-10-01 21:25:02 +02:00 • committed by GitHub
parent 940f6f100f
commit 06bdb8349e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 99 additions and 48 deletions

View file

@ -54,9 +54,9 @@ func runDiscuss(cmd *cobra.Command, args []string) {
redacted = redact(string(src))
}
out := new(bytes.Buffer)
tmpl := template.Must(template.New("discuss").Parse(discussTmpl))
out := new(bytes.Buffer)
_ = tmpl.Execute(out, map[string]any{
"CfgFile": file,
"CfgError": errorString(cfgErr),

View file

@ -1,8 +1,14 @@
package cmd
import (
"bytes"
_ "embed"
"fmt"
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/evcc-io/evcc/core"
"github.com/evcc-io/evcc/server"
"github.com/evcc-io/evcc/util"
@ -17,8 +23,17 @@ var dumpCmd = &cobra.Command{
Run: runDump,
}
var (
//go:embed dump.tpl
dumpTmpl string
dumpConfig *bool
)
func init() {
rootCmd.AddCommand(dumpCmd)
dumpConfig = dumpCmd.Flags().Bool("cfg", false, "Dump config file")
}
func handle(device any, err error) any {
@ -33,16 +48,47 @@ func runDump(cmd *cobra.Command, args []string) {
log.INFO.Printf("evcc %s", server.FormattedVersion())
// load config
if err := loadConfigFile(&conf); err != nil {
log.FATAL.Fatal(err)
}
err := loadConfigFile(&conf)
// setup environment
if err := configureEnvironment(conf); err != nil {
log.FATAL.Fatal(err)
if err == nil {
err = configureEnvironment(conf)
}
var site *core.Site
if err == nil {
site, err = configureSiteAndLoadpoints(conf)
}
if *dumpConfig {
file, pathErr := filepath.Abs(cfgFile)
if pathErr != nil {
file = cfgFile
}
var redacted string
if src, err := os.ReadFile(cfgFile); err == nil {
redacted = redact(string(src))
}
tmpl := template.Must(
template.New("dump").
Funcs(template.FuncMap(sprig.FuncMap())).
Parse(dumpTmpl))
out := new(bytes.Buffer)
_ = tmpl.Execute(out, map[string]any{
"CfgFile": file,
"CfgError": errorString(err),
"CfgContent": redacted,
"Version": server.FormattedVersion(),
})
fmt.Println(out.String())
os.Exit(0)
}
site, err := configureSiteAndLoadpoints(conf)
if err != nil {
log.FATAL.Fatal(err)
}

18
cmd/dump.tpl Normal file
View file

@ -0,0 +1,18 @@
{{ if .CfgError -}}
Fehlermeldung:
{{ .CfgError | indent 4 }}
{{ end -}}
{{ if .CfgContent -}}
Konfiguration{{ if .CfgFile }} ({{ .CfgFile }}){{ end }}:
{{ .CfgContent }}
{{ end -}}
{{ if .Version -}}
Version: `{{ .Version }}`
{{ end -}}

View file

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
@ -32,6 +33,18 @@ const (
flagDelay = "delay"
)
func bind(cmd *cobra.Command, flag, vpr string) {
if err := viper.BindPFlag(vpr, cmd.Flags().Lookup(flag)); err != nil {
panic(err)
}
}
func bindP(cmd *cobra.Command, flag, vpr string) {
if err := viper.BindPFlag(vpr, cmd.PersistentFlags().Lookup(flag)); err != nil {
panic(err)
}
}
func selectByName(cmd *cobra.Command, conf *[]qualifiedConfig) error {
flag := cmd.PersistentFlags().Lookup(flagName)
if !flag.Changed {

View file

@ -51,54 +51,28 @@ var rootCmd = &cobra.Command{
Use: "evcc",
Short: "EV Charge Controller",
Version: server.FormattedVersion(),
Run: run,
}
func bind(cmd *cobra.Command, flag string) {
if err := viper.BindPFlag(flag, cmd.PersistentFlags().Lookup(flag)); err != nil {
panic(err)
}
}
func configureCommand(cmd *cobra.Command) {
cmd.PersistentFlags().StringP(
"log", "l",
"info",
"Log level (fatal, error, warn, info, debug, trace)",
)
bind(cmd, "log")
cmd.PersistentFlags().StringVarP(&cfgFile,
"config", "c",
"",
"Config file (default \"~/evcc.yaml\" or \"/etc/evcc.yaml\")",
)
cmd.PersistentFlags().BoolP(
"help", "h",
false,
"Help for "+cmd.Name(),
)
Run: runRoot,
}
func init() {
cobra.OnInitialize(initConfig)
configureCommand(rootCmd)
rootCmd.PersistentFlags().IntP("port", "p", 7070, "Listen port")
if err := viper.BindPFlag("network.port", rootCmd.PersistentFlags().Lookup("port")); err != nil {
panic(err)
}
// global options
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file (default \"~/evcc.yaml\" or \"/etc/evcc.yaml\")")
rootCmd.PersistentFlags().DurationP("interval", "i", 10*time.Second, "Update interval")
bind(rootCmd, "interval")
rootCmd.PersistentFlags().Bool("metrics", false, "Expose metrics")
bind(rootCmd, "metrics")
rootCmd.PersistentFlags().Bool("profile", false, "Expose pprof profiles")
bind(rootCmd, "profile")
rootCmd.PersistentFlags().BoolP("help", "h", false, "Help")
rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
// config file options
rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)")
bindP(rootCmd, "log", "log")
rootCmd.Flags().Bool("metrics", false, "Expose metrics")
bind(rootCmd, "metrics", "metrics")
rootCmd.Flags().Bool("profile", false, "Expose pprof profiles")
bind(rootCmd, "profile", "profile")
}
// initConfig reads in config file and ENV variables if set
@ -189,7 +163,7 @@ func publishErrorInfo(cfgFile string, err error) {
publish("fatal", unwrap(err))
}
func run(cmd *cobra.Command, args []string) {
func runRoot(cmd *cobra.Command, args []string) {
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))
log.INFO.Printf("evcc %s", server.FormattedVersion())