Skip to content

Commit

Permalink
fix: correct log level handling in cmd (#17)
Browse files Browse the repository at this point in the history
* default to empty space instead of error
  as we already set the default in config
* only set new log level if log level flag
  is set/not equal empty space
* no need to set global log level in RunCmd()
  • Loading branch information
xx4h authored Oct 9, 2024
1 parent 89fc886 commit c9f3bde
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ func newRootCmd(h *pkg.Hctl, out io.Writer, _ []string) *cobra.Command {
Short: "A command line tool to control your home automation",
Long: fmt.Sprintf("%s\nHctl is a CLI tool to control your home automation", banner),
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
lvl, err := zerolog.ParseLevel(logLevel)
if err != nil {
return err
if logLevel != "" {
lvl, err := zerolog.ParseLevel(logLevel)
if err != nil {
return err
}
zerolog.SetGlobalLevel(lvl)
}
zerolog.SetGlobalLevel(lvl)
return nil
},
}
Expand All @@ -69,13 +71,12 @@ func newRootCmd(h *pkg.Hctl, out io.Writer, _ []string) *cobra.Command {
newVolumeCmd(h, out),
)

cmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", zerolog.ErrorLevel.String(), "Set the log level")
cmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "", "Set the log level")

return cmd
}

func RunCmd() {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
h, err := pkg.NewHctl()
if err != nil {
Expand Down

0 comments on commit c9f3bde

Please sign in to comment.