diff --git a/cmd/ctrlc/ctrlc.go b/cmd/ctrlc/ctrlc.go index a58e993..e07b5d1 100644 --- a/cmd/ctrlc/ctrlc.go +++ b/cmd/ctrlc/ctrlc.go @@ -32,6 +32,9 @@ func init() { viper.BindPFlag("workspace", cmd.PersistentFlags().Lookup("workspace")) viper.BindEnv("workspace", "CTRLPLANE_WORKSPACE") + viper.BindPFlag("log-level", cmd.PersistentFlags().Lookup("log-level")) + viper.BindEnv("log-level", "CTRLPLANE_LOG_LEVEL") + viper.BindEnv("cluster-identifier", "CTRLPLANE_CLUSTER_IDENTIFIER") } diff --git a/cmd/ctrlc/root/root.go b/cmd/ctrlc/root/root.go index 8c5a883..e856067 100644 --- a/cmd/ctrlc/root/root.go +++ b/cmd/ctrlc/root/root.go @@ -1,8 +1,6 @@ package root import ( - "os" - "github.com/MakeNowJust/heredoc/v2" "github.com/charmbracelet/log" @@ -14,6 +12,7 @@ import ( "github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync" "github.com/ctrlplanedev/cli/cmd/ctrlc/root/version" "github.com/spf13/cobra" + "github.com/spf13/viper" ) func NewRootCmd() *cobra.Command { @@ -28,7 +27,11 @@ func NewRootCmd() *cobra.Command { $ ctrlc connect `), PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - switch logLevel { + level := viper.GetString("log-level") + if level == "" { + level = logLevel + } + switch level { case "debug": log.SetLevel(log.DebugLevel) case "info": @@ -45,7 +48,7 @@ func NewRootCmd() *cobra.Command { }, } - cmd.PersistentFlags().StringVar(&logLevel, "log-level", defaultOrEnv("info", "CTRLC_LOG_LEVEL"), "Set the logging level (debug, info, warn, error)") + cmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set the logging level (debug, info, warn, error)") cmd.AddCommand(agent.NewAgentCmd()) cmd.AddCommand(api.NewAPICmd()) @@ -58,14 +61,3 @@ func NewRootCmd() *cobra.Command { return cmd } - -func defaultOrEnv(defaultValue string, envVarName string) string { - if envVarName == "" { - return defaultValue - } - value, set := os.LookupEnv(envVarName) - if !set { - value = defaultValue - } - return value -}