Skip to content

Commit

Permalink
Allow setting log level for mindev ruletype test (#3119)
Browse files Browse the repository at this point in the history
I was debugging a ruletype change and found it handy to have the engine
debug logs dumped onto the console.

This patch adds a new parameter `--log-level` that defaults to `error`
and lets you set the log level for the evaluation of the engine.
  • Loading branch information
jhrozek committed Apr 17, 2024
1 parent c77ed44 commit fc3afce
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions cmd/dev/app/rule_type/rttst.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/eval/rego"
engif "github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/logger"
"github.com/stacklok/minder/internal/providers"
"github.com/stacklok/minder/internal/providers/credentials"
"github.com/stacklok/minder/internal/util/jsonyaml"
Expand All @@ -53,6 +54,7 @@ func CmdTest() *cobra.Command {
SilenceUsage: true,
}

testCmd.Flags().String("log-level", "error", "Log Level")
testCmd.Flags().StringP("rule-type", "r", "", "file to read rule type definition from")
testCmd.Flags().StringP("entity", "e", "", "YAML file containing the entity to test the rule against")
testCmd.Flags().StringP("profile", "p", "", "YAML file containing a profile to test the rule against")
Expand Down Expand Up @@ -223,11 +225,17 @@ func runEvaluationForRules(
RemMetadata: remMetadata,
},
}

// Enable logging for the engine
ctx := context.Background()
logConfig := serverconfig.LoggingConfig{Level: cmd.Flag("log-level").Value.String()}
ctx = logger.FromFlags(logConfig).WithContext(ctx)

// Perform rule evaluation
evalStatus.SetEvalErr(eng.Eval(context.Background(), inf, evalStatus))
evalStatus.SetEvalErr(eng.Eval(ctx, inf, evalStatus))

// Perform the actions, if any
evalStatus.SetActionsErr(context.Background(), eng.Actions(context.Background(), inf, evalStatus))
evalStatus.SetActionsErr(ctx, eng.Actions(ctx, inf, evalStatus))

if errors.IsActionFatalError(evalStatus.GetActionsErr().RemediateErr) {
cmd.Printf("Remediation failed with fatal error: %s", evalStatus.GetActionsErr().RemediateErr)
Expand Down

0 comments on commit fc3afce

Please sign in to comment.