Skip to content

Commit

Permalink
add debug flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-sakamoto committed Jun 16, 2024
1 parent c85c98b commit dc94f3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
11 changes: 0 additions & 11 deletions cmd/kom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ import (
"github.com/ryota-sakamoto/kubernetes-on-multipass/pkg/cmd"
)

func init() {
logLevel := slog.LevelInfo
if l, b := os.LookupEnv("LOG_LEVEL"); b {
logLevel.UnmarshalText([]byte(l))
}

slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
})))
}

func main() {
if err := cmd.Execute(); err != nil {
slog.Error(err.Error())
Expand Down
17 changes: 17 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"log/slog"
"os"

"github.com/spf13/cobra"
)

Expand All @@ -12,8 +15,22 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logLevel := slog.LevelInfo
if b, _ := cmd.Flags().GetBool("debug"); b {
logLevel = slog.LevelDebug
}

slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
})))
},
}

func Execute() error {
return rootCmd.Execute()
}

func init() {
rootCmd.PersistentFlags().BoolP("debug", "", false, "Enable debug mode")
}

0 comments on commit dc94f3b

Please sign in to comment.