opa-eval - Man Page

Evaluate a Rego query


opa eval  [flags]


Evaluate a Rego query and print the result.


To evaluate a simple query:

$ opa eval 'x = 1; y = 2; x < y'

To evaluate a query against JSON data:

$ opa eval --data data.json 'data.names[_] = name'

To evaluate a query against JSON data supplied with a file:// URL:

$ opa eval --data file:///path/to/file.json 'data'

File Bundle Loading

The --bundle flag will load data files and Rego files contained in the bundle specified by the path. It can be either a compressed tar archive bundle file or a directory tree.

$ opa eval --bundle /some/path 'data'

Where /some/path contains:

  +-- bar/
  |     |
  |     +-- data.json
  +-- baz_test.rego
  +-- manifest.yaml

The JSON file 'foo/bar/data.json' would be loaded and rooted under package path contained inside the file. Only data files named data.json or data.yaml will be loaded. In the example above the manifest.yaml would be ignored.

See https://www.openpolicyagent.org/docs/latest/bundles/ for more details on bundle directory structures.

The --data flag can be used to recursively load ALL *.rego, *.json, and *.yaml files under the specified directory.

Output Formats

Set the output format with the --format flag.

--format=json      : output raw query results as JSON
--format=values    : output line separated JSON arrays containing expression values
--format=bindings  : output line separated JSON objects containing variable bindings
--format=pretty    : output query results in a human-readable format


-b, --bundle="" set bundle file(s) or directory path(s). This flag can be repeated.

--coverage[=false] report coverage

-d, --data="" set policy or data file(s). This flag can be repeated.

--disable-indexing[=false] disable indexing optimizations

--disable-inlining=[] set paths of documents to exclude from inlining

--explain=off enable query explanations

--fail[=false] exits with non-zero exit code on undefined/empty result and errors

--fail-defined[=false] exits with non-zero exit code on defined/non-empty result and errors

-f, --format=json set output format

-h, --help[=false] help for eval

--ignore=[] set file and directory names to ignore during loading (e.g., '.*' excludes hidden files)

--import="" set query import(s). This flag can be repeated.

-i, --input="" set input file path

--instrument[=false] enable query instrumentation metrics (implies --metrics)

--metrics[=false] report query performance metrics

--package="" set query package

-p, --partial[=false] perform partial evaluation

--pretty-limit=80 set limit after which pretty output gets truncated

--profile[=false] perform expression profiling

--profile-limit=10 set number of profiling results to show

--profile-sort="" set sort order of expression profiler results

--shallow-inlining[=false] disable inlining of rules that depend on unknowns

--stdin[=false] read query from stdin

-I, --stdin-input[=false] read input document from stdin

--strict-builtin-errors[=false] treat built-in function errors as fatal

-u, --unknowns=[input] set paths to treat as unknown during partial evaluation

See Also


Referenced By


Jan 2021