Skip to content

Commit 8909954

Browse files
Merge pull request #46 from ctrlplanedev/log-level-env
chore(log-levels): Refactor log level env flag
2 parents c05d204 + 4c37687 commit 8909954

2 files changed

Lines changed: 10 additions & 15 deletions

File tree

cmd/ctrlc/ctrlc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func init() {
3232
viper.BindPFlag("workspace", cmd.PersistentFlags().Lookup("workspace"))
3333
viper.BindEnv("workspace", "CTRLPLANE_WORKSPACE")
3434

35+
viper.BindPFlag("log-level", cmd.PersistentFlags().Lookup("log-level"))
36+
viper.BindEnv("log-level", "CTRLPLANE_LOG_LEVEL")
37+
3538
viper.BindEnv("cluster-identifier", "CTRLPLANE_CLUSTER_IDENTIFIER")
3639
}
3740

cmd/ctrlc/root/root.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package root
22

33
import (
4-
"os"
5-
64
"github.com/MakeNowJust/heredoc/v2"
75

86
"github.com/charmbracelet/log"
@@ -14,6 +12,7 @@ import (
1412
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync"
1513
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/version"
1614
"github.com/spf13/cobra"
15+
"github.com/spf13/viper"
1716
)
1817

1918
func NewRootCmd() *cobra.Command {
@@ -28,7 +27,11 @@ func NewRootCmd() *cobra.Command {
2827
$ ctrlc connect <agent-name>
2928
`),
3029
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
31-
switch logLevel {
30+
level := viper.GetString("log-level")
31+
if level == "" {
32+
level = logLevel
33+
}
34+
switch level {
3235
case "debug":
3336
log.SetLevel(log.DebugLevel)
3437
case "info":
@@ -45,7 +48,7 @@ func NewRootCmd() *cobra.Command {
4548
},
4649
}
4750

48-
cmd.PersistentFlags().StringVar(&logLevel, "log-level", defaultOrEnv("info", "CTRLC_LOG_LEVEL"), "Set the logging level (debug, info, warn, error)")
51+
cmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set the logging level (debug, info, warn, error)")
4952

5053
cmd.AddCommand(agent.NewAgentCmd())
5154
cmd.AddCommand(api.NewAPICmd())
@@ -58,14 +61,3 @@ func NewRootCmd() *cobra.Command {
5861

5962
return cmd
6063
}
61-
62-
func defaultOrEnv(defaultValue string, envVarName string) string {
63-
if envVarName == "" {
64-
return defaultValue
65-
}
66-
value, set := os.LookupEnv(envVarName)
67-
if !set {
68-
value = defaultValue
69-
}
70-
return value
71-
}

0 commit comments

Comments
 (0)