Skip to content

Commit

Permalink
feat: add --debug flag to ratify CLI (ratify-project#1188)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts authored Nov 29, 2023
1 parent 69bd336 commit 00f16de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cmd/ratify/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ const (
var Root = New(use, shortDesc)

func New(use, short string) *cobra.Command {
common.SetLoggingLevelFromEnv(logrus.StandardLogger())
featureflag.InitFeatureFlagsFromEnv()

var enableDebug bool
root := &cobra.Command{
Use: use,
Short: short,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if enableDebug {
common.SetLoggingLevel("debug", logrus.StandardLogger())
} else {
common.SetLoggingLevelFromEnv(logrus.StandardLogger())
}
},
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
Expand All @@ -50,6 +56,6 @@ func New(use, short string) *cobra.Command {
root.AddCommand(NewCmdVersion(use, versionUse))
root.AddCommand(NewCmdResolve(use, resolveUse))

// TODO debug logging
root.PersistentFlags().BoolVarP(&enableDebug, "debug", "d", false, "Enable debug mode. If enabled, set logger level to debug")
return root
}
3 changes: 3 additions & 0 deletions pkg/common/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ import (

const defaultLevel = logrus.InfoLevel

// SetLoggingLevelFromEnv sets logrus level to the value of environment variable
// RATIFY_LOG_LEVEL
func SetLoggingLevelFromEnv(logger *logrus.Logger) {
SetLoggingLevel(os.Getenv("RATIFY_LOG_LEVEL"), logger)
}

// SetLoggingLevel sets logrus level with Info level as default
func SetLoggingLevel(level string, logger *logrus.Logger) {
var logrusLevel logrus.Level
if level == "" {
Expand Down

0 comments on commit 00f16de

Please sign in to comment.