Fix binding options and workaround overwriting configured log level (#4772)
This commit is contained in:
parent
416d620d6c
commit
2453ee0b44
10 changed files with 35 additions and 9 deletions
|
|
@ -42,6 +42,8 @@ func runCharger(cmd *cobra.Command, args []string) {
|
|||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
setLogLevel(cmd)
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(cmd, conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -76,6 +76,8 @@ func runChargerRamp(cmd *cobra.Command, args []string) {
|
|||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
setLogLevel(cmd)
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(cmd, conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -42,6 +42,8 @@ func runDiscuss(cmd *cobra.Command, args []string) {
|
|||
|
||||
cfgErr := loadConfigFile(&conf)
|
||||
|
||||
setLogLevel(cmd)
|
||||
|
||||
file, pathErr := filepath.Abs(cfgFile)
|
||||
if pathErr != nil {
|
||||
file = cfgFile
|
||||
|
|
|
|||
|
|
@ -50,6 +50,8 @@ func runDump(cmd *cobra.Command, args []string) {
|
|||
// load config
|
||||
err := loadConfigFile(&conf)
|
||||
|
||||
setLogLevel(cmd)
|
||||
|
||||
// setup environment
|
||||
if err == nil {
|
||||
err = configureEnvironment(cmd, conf)
|
||||
|
|
|
|||
10
cmd/flags.go
10
cmd/flags.go
|
|
@ -36,20 +36,20 @@ const (
|
|||
flagDelay = "delay"
|
||||
)
|
||||
|
||||
func bind(cmd *cobra.Command, flag, vpr string) {
|
||||
if err := viper.BindPFlag(vpr, cmd.Flags().Lookup(flag)); err != nil {
|
||||
func bind(cmd *cobra.Command, flag string) {
|
||||
if err := viper.BindPFlag(flag, 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 {
|
||||
func bindP(cmd *cobra.Command, flag string) {
|
||||
if err := viper.BindPFlag(flag, cmd.PersistentFlags().Lookup(flag)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func selectByName(cmd *cobra.Command, conf *[]qualifiedConfig) error {
|
||||
flag := cmd.PersistentFlags().Lookup(flagName)
|
||||
flag := cmd.Flags().Lookup(flagName)
|
||||
if !flag.Changed {
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,20 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/cmd/shutdown"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// setLogLevel sets log level from config overwritten by command line
|
||||
// https://github.com/spf13/viper/issues/1444
|
||||
func setLogLevel(cmd *cobra.Command) {
|
||||
if flag := cmd.Flags().Lookup("log"); viper.GetString("log") == "" || flag.Changed {
|
||||
viper.Set("log", flag.Value.String())
|
||||
}
|
||||
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))
|
||||
}
|
||||
|
||||
// unwrap converts a wrapped error into slice of strings
|
||||
func unwrap(err error) (res []string) {
|
||||
for err != nil {
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ func runMeter(cmd *cobra.Command, args []string) {
|
|||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
setLogLevel(cmd)
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(cmd, conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -53,13 +53,13 @@ func init() {
|
|||
|
||||
// config file options
|
||||
rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)")
|
||||
bindP(rootCmd, "log", "log")
|
||||
bindP(rootCmd, "log")
|
||||
|
||||
rootCmd.Flags().Bool("metrics", false, "Expose metrics")
|
||||
bind(rootCmd, "metrics", "metrics")
|
||||
bind(rootCmd, "metrics")
|
||||
|
||||
rootCmd.Flags().Bool("profile", false, "Expose pprof profiles")
|
||||
bind(rootCmd, "profile", "profile")
|
||||
bind(rootCmd, "profile")
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set
|
||||
|
|
@ -110,7 +110,7 @@ func runRoot(cmd *cobra.Command, args []string) {
|
|||
err = cfgErr
|
||||
}
|
||||
|
||||
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))
|
||||
setLogLevel(cmd)
|
||||
|
||||
// network config
|
||||
if viper.GetString("uri") != "" {
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@ func runToken(cmd *cobra.Command, args []string) {
|
|||
log.FATAL.Fatal(err)
|
||||
}
|
||||
|
||||
setLogLevel(cmd)
|
||||
|
||||
var vehicleConf qualifiedConfig
|
||||
if len(conf.Vehicles) == 1 {
|
||||
vehicleConf = conf.Vehicles[0]
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@ func runVehicle(cmd *cobra.Command, args []string) {
|
|||
fatal(err)
|
||||
}
|
||||
|
||||
setLogLevel(cmd)
|
||||
|
||||
// setup environment
|
||||
if err := configureEnvironment(cmd, conf); err != nil {
|
||||
fatal(err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue