From 2453ee0b44b4bbbf830ed7cc53db5beed53f4e11 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 9 Oct 2022 18:03:04 +0200 Subject: [PATCH] Fix binding options and workaround overwriting configured log level (#4772) --- cmd/charger.go | 2 ++ cmd/charger_ramp.go | 2 ++ cmd/discuss.go | 2 ++ cmd/dump.go | 2 ++ cmd/flags.go | 10 +++++----- cmd/helper.go | 12 ++++++++++++ cmd/meter.go | 2 ++ cmd/root.go | 8 ++++---- cmd/token.go | 2 ++ cmd/vehicle.go | 2 ++ 10 files changed, 35 insertions(+), 9 deletions(-) diff --git a/cmd/charger.go b/cmd/charger.go index 6d4a66104..9df2cbc00 100644 --- a/cmd/charger.go +++ b/cmd/charger.go @@ -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) diff --git a/cmd/charger_ramp.go b/cmd/charger_ramp.go index 6a51ab04d..bf9022290 100644 --- a/cmd/charger_ramp.go +++ b/cmd/charger_ramp.go @@ -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) diff --git a/cmd/discuss.go b/cmd/discuss.go index 473ea7d31..c6f36ee45 100644 --- a/cmd/discuss.go +++ b/cmd/discuss.go @@ -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 diff --git a/cmd/dump.go b/cmd/dump.go index 1a4eae537..eda9f9e31 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -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) diff --git a/cmd/flags.go b/cmd/flags.go index 7adaea163..b543e1479 100644 --- a/cmd/flags.go +++ b/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 } diff --git a/cmd/helper.go b/cmd/helper.go index ee35bb7af..7cf2d4386 100644 --- a/cmd/helper.go +++ b/cmd/helper.go @@ -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 { diff --git a/cmd/meter.go b/cmd/meter.go index 0953b2935..486197cf0 100644 --- a/cmd/meter.go +++ b/cmd/meter.go @@ -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) diff --git a/cmd/root.go b/cmd/root.go index 3023f3ebf..6dbf4df6d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") != "" { diff --git a/cmd/token.go b/cmd/token.go index 9346f8373..36f4b10a4 100644 --- a/cmd/token.go +++ b/cmd/token.go @@ -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] diff --git a/cmd/vehicle.go b/cmd/vehicle.go index 52cac5624..b9dfa8fd0 100644 --- a/cmd/vehicle.go +++ b/cmd/vehicle.go @@ -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)