mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
Adding --v1-compatible flag to build, opa eval (#6478)
* ast+cmd+rego: Adding `--rego-v1` flag to `opa eval` Fixes: #6463 Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Adding `--rego-v1` flag to `opa build` Fixes: #6463 Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Formatting PE support modules to comply with rego-v1 when required Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Removing rego.v1 import when formatting for rego-v1 (not rego-v0-compat-v1) Signed-off-by: Johan Fylling <johan.dev@fylling.se> * touch up Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Fixing linting issues Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Consolidating `Bundle.FormatModules()` and `Bundle.FormatModulesForRegoVersion()` Suggested by @ashutosh-narkar Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Adding descriptions to `RegoVersion` Requested by @ashutosh-narkar Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Using `--v1-compatible` flag instead of `--rego-v1` Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Updating docs Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Reintroducing `ParserOptions.RegoV1Compatible` to avoid breaking change Signed-off-by: Johan Fylling <johan.dev@fylling.se> * cmd & tester Adding `--v1-compatible` flag to `opa test` Fixes: #6463 Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Adding `--v1-compatible` flag to `opa fmt` Fixes: #6463 Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Adding `--v1-compatible` flag to `opa check` Fixes: #6463 Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Making linter happy Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Review modifications suggested by @ashutosh-narkar * Changing `ParserOptions.RegoV1Compatible` take precedence over `ParserOptions.RegoVersion` * Fixing comment in test * Updating `fmt --rego-v1` CLI description Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Review modifications suggested by @ashutosh-narkar * Changing `ParserOptions.RegoV1Compatible` take precedence over `ParserOptions.RegoVersion` * Fixing comment in test * Updating `fmt --rego-v1` CLI description * Adding back `Opts.RegoV1` and deprecating. * Making `Opts.RegoV1` take precedence over `Opts.RegoVersion` Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Review modifications suggested by @ashutosh-narkar * Changing `ParserOptions.RegoV1Compatible` take precedence over `ParserOptions.RegoVersion` * Fixing comment in test * Updating `fmt --rego-v1` CLI description * Adding back `Opts.RegoV1` and deprecating. * Making `Opts.RegoV1` take precedence over `Opts.RegoVersion` * `TestPartialWitRegoV1` -> `TestPartialWithRegoV1` Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Review modifications suggested by @ashutosh-narkar * Changing `ParserOptions.RegoV1Compatible` take precedence over `ParserOptions.RegoVersion` * Fixing comment in test * Updating `fmt --rego-v1` CLI description * Adding back `Opts.RegoV1` and deprecating. * Making `Opts.RegoV1` take precedence over `Opts.RegoVersion` * `TestPartialWitRegoV1` -> `TestPartialWithRegoV1` * removing `Println` in test Signed-off-by: Johan Fylling <johan.dev@fylling.se> * Review modifications suggested by @ashutosh-narkar * Changing `ParserOptions.RegoV1Compatible` take precedence over `ParserOptions.RegoVersion` * Fixing comment in test * Updating `fmt --rego-v1` CLI description * Adding back `Opts.RegoV1` and deprecating. * Making `Opts.RegoV1` take precedence over `Opts.RegoVersion` * `TestPartialWitRegoV1` -> `TestPartialWithRegoV1` * removing `Println` in test * Updating docs with per-command behavioural descriptions for `--v1-compatible`. Signed-off-by: Johan Fylling <johan.dev@fylling.se> --------- Signed-off-by: Johan Fylling <johan.dev@fylling.se>
This commit is contained in:
+15
-3
@@ -83,6 +83,7 @@ type Compiler struct {
|
||||
metadata *map[string]interface{} // represents additional data included in .manifest file
|
||||
fsys fs.FS // file system to use when loading paths
|
||||
ns string
|
||||
regoVersion ast.RegoVersion
|
||||
}
|
||||
|
||||
// New returns a new compiler instance that can be invoked.
|
||||
@@ -242,6 +243,11 @@ func (c *Compiler) WithPartialNamespace(ns string) *Compiler {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Compiler) WithRegoVersion(v ast.RegoVersion) *Compiler {
|
||||
c.regoVersion = v
|
||||
return c
|
||||
}
|
||||
|
||||
func addEntrypointsFromAnnotations(c *Compiler, ar []*ast.AnnotationsRef) error {
|
||||
for _, ref := range ar {
|
||||
var entrypoint ast.Ref
|
||||
@@ -356,8 +362,14 @@ func (c *Compiler) Build(ctx context.Context) error {
|
||||
c.bundle.Manifest.Metadata = *c.metadata
|
||||
}
|
||||
|
||||
if err := c.bundle.FormatModules(false); err != nil {
|
||||
return err
|
||||
if c.regoVersion == ast.RegoV1 {
|
||||
if err := c.bundle.FormatModulesForRegoVersion(c.regoVersion, false, false); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := c.bundle.FormatModules(false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if c.bsc != nil {
|
||||
@@ -432,7 +444,7 @@ func (c *Compiler) initBundle() error {
|
||||
// TODO(tsandall): the metrics object should passed through here so we that
|
||||
// we can track read and parse times.
|
||||
|
||||
load, err := initload.LoadPaths(c.paths, c.filter, c.asBundle, c.bvc, false, c.useRegoAnnotationEntrypoints, c.capabilities, c.fsys)
|
||||
load, err := initload.LoadPathsForRegoVersion(c.regoVersion, c.paths, c.filter, c.asBundle, c.bvc, false, c.useRegoAnnotationEntrypoints, c.capabilities, c.fsys)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load error: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user