Skip to content

Commit

Permalink
root: use a flag for enabling debug logs
Browse files Browse the repository at this point in the history
Signed-off-by: Gunwant Jain <[email protected]>
  • Loading branch information
wantguns committed Oct 15, 2023
1 parent 14b84b8 commit 43a980a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"runtime"

"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
cfgFile string
enableDebug bool

rootCmd = &cobra.Command{
Use: "unfold",
Expand All @@ -23,10 +25,19 @@ func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.config/unfold/config.yaml)")
rootCmd.PersistentFlags().BoolVarP(&enableDebug, "debug", "v", os.Getenv("DEBUG") == "true", "Enable debug mode")
rootCmd.AddCommand(LoginCmd, RefreshCmd, UserCmd, AvailabilityCmd, TransactionsCmd)
}

func initConfig() {

if enableDebug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

if cfgFile != "" {
viper.SetConfigFile(cfgFile)
err := viper.ReadInConfig()
Expand Down
5 changes: 0 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"
"os/signal"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"

Expand All @@ -24,9 +23,5 @@ func main() {

defer viper.WriteConfig()

// TODO: Use a flag for this
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

cmd.Execute()
}

0 comments on commit 43a980a

Please sign in to comment.