Skip to content

Commit

Permalink
refactor: simplify data bundle creation
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Feb 6, 2025
1 parent a4e3592 commit 4de75ab
Showing 1 changed file with 18 additions and 47 deletions.
65 changes: 18 additions & 47 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,7 @@ func (l Linter) Lint(ctx context.Context) (report.Report, error) {
return report.Report{}, fmt.Errorf("failed to merge config: %w", err)
}

l.dataBundle = &bundle.Bundle{
Manifest: bundle.Manifest{
Roots: &[]string{"internal"},
Metadata: map[string]any{"name": "internal"},
},
Data: map[string]any{
"internal": map[string]any{
"combined_config": config.ToMap(*conf),
"capabilities": rio.ToMap(config.CapabilitiesForThisVersion()),
"path_prefix": l.pathPrefix,
},
},
}
l.dataBundle = l.createDataBundle(*conf)

ignore := conf.Ignore.Files

Expand Down Expand Up @@ -538,18 +526,7 @@ func (l Linter) DetermineEnabledRules(ctx context.Context) ([]string, error) {
return []string{}, fmt.Errorf("failed to merge config: %w", err)
}

l.dataBundle = &bundle.Bundle{
Manifest: bundle.Manifest{
Roots: &[]string{"internal"},
Metadata: map[string]any{"name": "internal"},
},
Data: map[string]any{
"internal": map[string]any{
"combined_config": config.ToMap(*conf),
"capabilities": rio.ToMap(config.CapabilitiesForThisVersion()),
},
},
}
l.dataBundle = l.createDataBundle(*conf)

queryStr := `[rule |
data.regal.rules[cat][rule]
Expand Down Expand Up @@ -604,18 +581,7 @@ func (l Linter) DetermineEnabledAggregateRules(ctx context.Context) ([]string, e
return []string{}, fmt.Errorf("failed to merge config: %w", err)
}

l.dataBundle = &bundle.Bundle{
Manifest: bundle.Manifest{
Roots: &[]string{"internal"},
Metadata: map[string]any{"name": "internal"},
},
Data: map[string]any{
"internal": map[string]any{
"combined_config": config.ToMap(*conf),
"capabilities": rio.ToMap(config.CapabilitiesForThisVersion()),
},
},
}
l.dataBundle = l.createDataBundle(*conf)

queryStr := `[rule |
data.regal.rules[cat][rule].aggregate
Expand Down Expand Up @@ -657,7 +623,7 @@ func (l Linter) DetermineEnabledAggregateRules(ctx context.Context) ([]string, e
return enabledRules, nil
}

func (l Linter) paramsToRulesConfig() map[string]any {
func (l Linter) createDataBundle(conf config.Config) *bundle.Bundle {
params := map[string]any{
"disable_all": l.disableAll,
"disable_category": util.NullToEmpty(l.disableCategory),
Expand All @@ -668,24 +634,29 @@ func (l Linter) paramsToRulesConfig() map[string]any {
"ignore_files": util.NullToEmpty(l.ignoreFiles),
}

return map[string]any{
"eval": map[string]any{
"params": params,
return &bundle.Bundle{
Manifest: bundle.Manifest{
Roots: &[]string{"internal", "eval"},
Metadata: map[string]any{"name": "internal"},
},
Data: map[string]any{
"eval": map[string]any{
"params": params,
},
"internal": map[string]any{
"combined_config": config.ToMap(conf),
"capabilities": rio.ToMap(config.CapabilitiesForThisVersion()),
"path_prefix": l.pathPrefix,
},
},
}
}

func (l Linter) prepareRegoArgs(query ast.Body) ([]func(*rego.Rego), error) {
dataBundle := bundle.Bundle{
Data: l.paramsToRulesConfig(),
Manifest: bundle.Manifest{Roots: &[]string{"eval"}},
}

regoArgs := append([]func(*rego.Rego){
rego.StoreReadAST(true),
rego.Metrics(l.metrics),
rego.ParsedQuery(query),
rego.ParsedBundle("regal_eval_params", &dataBundle),
}, builtins.RegalBuiltinRegoFuncs...)

if l.debugMode && l.printHook == nil {
Expand Down

0 comments on commit 4de75ab

Please # to comment.