Skip to content

Commit

Permalink
Fix binding options and workaround overwriting configured log level (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Oct 9, 2022
1 parent 416d620 commit 2453ee0
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmd/charger_ramp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmd/discuss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions cmd/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") != "" {
Expand Down
2 changes: 2 additions & 0 deletions cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions cmd/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2453ee0

Please sign in to comment.