Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/ctrlc/ctrlc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
22 changes: 7 additions & 15 deletions cmd/ctrlc/root/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package root

import (
"os"

"github.com/MakeNowJust/heredoc/v2"

"github.com/charmbracelet/log"
Expand All @@ -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 {
Expand All @@ -28,7 +27,11 @@ func NewRootCmd() *cobra.Command {
$ ctrlc connect <agent-name>
`),
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":
Expand All @@ -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())
Expand All @@ -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
}