20 Commits

Author SHA1 Message Date
Stephan Renatus 9de558575a cli: fix 'opa exec' parameters
This also adds a new test step running all testscript txtar archives
on all platforms. Our existing lo-fi binary smoke tests should move to
that eventually.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2025-08-25 20:02:01 +02:00
kevinstyra 94a953150a cmd: allow branding
This change allows users that build their own executable or "spin" of
OPA to give it a name, and have it reference itself properly in help
texts.

It's a vanity thing, but I think some people would appreciate it, hat
tip to the international association of pedants.

Signed-off-by: Stephan Renatus <stephan@styra.com>
Co-authored-by: kevinstyra <83973046+kevinstyra@users.noreply.github.com>
2025-07-24 11:33:23 +02:00
kevinstyra 36bae2aac6 cmd: use command.RunE to return errors and perform orderly shutdown of OPA
`os.Exit` immediately exits the program and doesn't run defer functions.
This can be problematic as any command.OnFinalize routines and any logic
after the command.Execute won't be run.

Also suppress all RunE cobra error and usage messages. These would be
printed twice otherwise.

Signed-off-by: Stephan Renatus <stephan@styra.com>
Co-authored-by: Kevin St. Pierre <kevin@styra.com>
2025-07-23 17:17:50 +02:00
Stephan Renatus 334666355a opa exec: stop plugins before exit
This allows certain plugins to do their cleanup routines -- like sending
decision logs to some other location when using a custom decision log
setup.

Signed-off-by: Stephan Renatus <stephan@styra.com>
2025-07-10 20:32:54 +02:00
Anders Eknert e43ef0a979 Use any in place of interface{} (#7566)
Earlier this evening I tried to run the Go
[modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize)
analyzer on OPA. That didn't go as planned:

- https://github.com/golang/go/issues/73661
- https://github.com/golang/go/issues/73663

While we wait for that to be fixed, I figured an old-fashioned
search-and-replace across the repo may work for at least the
`interface{}` to `any` conversion. That should help make it easier
to see the other fixes as applied by the modernize tool once it has
had those issues resolved.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-05-12 13:57:48 +02:00
Anders Eknert 55e87e79ae Add perfsprint linter (#7334)
And update code to conform to the rule.

- Replace unnecessary fmt.Sprintf with string concatenation
- Replace fmt.Sprint with more efficient strconv.Itoa
- Replace static fmt.Errorf calls with more efficient errors.New

Thanks @srenatus for pushing me down this rabbit hole!

Signed-off-by: Anders Eknert <anders@styra.com>
2025-01-31 20:24:05 +01:00
Johan Fylling 7bb6dbe36b Preparing for v1 API
Moving (most) source to v1 root package to prepare for v0/v1 API separation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:09:03 +01:00
Johan Fylling 5521453d87 cmd: Adding --v0-compatible flag (#7065)
To make OPA behave as v0.x post v1.0 release.
If used simultaneously with `--v1-compatible` flag, the `--v0-compatible` flag takes precedence.

Also, future-proofing `cmd` package tests for 1.0.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-09-26 00:45:51 +02:00
Charlie Egan 46b19707e7 docs: Use Example field for alternative commands (#6837)
* docs: Use Example field for alternative commands

This makes it safer to process this text into markdown and other
formats. (<path> is tricky as it looks like an HTML tag.

Signed-off-by: Charlie Egan <charlie@styra.com>

* docs: update CLI docs

Signed-off-by: Charlie Egan <charlie@styra.com>

---------

Signed-off-by: Charlie Egan <charlie@styra.com>
2024-07-01 10:36:39 +01:00
Johan Fylling e50a3061f5 cmd/exec: Supporting simultaneous input from stdin and files (#6831)
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-06-26 15:54:01 +02:00
Colin J Lacy 96800d747b cmd/exec: adds --stdin-input (-I) flag for input piping or manual entry (#6822)
Signed-off-by: Colin Lacy <colinjlacy@gmail.com>
2024-06-26 15:04:54 +02:00
Johan Fylling 5464b005e8 Bumping golangci-lint to v1.59.1 (#6817)
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-06-19 15:13:43 +02:00
Philip Conrad 6b7ede0ee0 cmd/exec: Add timeout flag to help prevent infinite hangs. (#6633)
This commit adds a `--timeout` duration flag to the `opa exec` CLI command.
This flag helps out in use cases such as CI, where stalling indefinitely
is undesirable behavior.

Fixes: #6613

Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
2024-03-18 12:22:41 -04:00
Colin J Lacy 4bfae4e64e cmd: add env var backups to command flags (#6508)
Signed-off-by: Colin Lacy <colinjlacy@gmail.com>
2024-01-29 15:15:35 +01:00
Johan Fylling b36151d992 Adding --v1-compatible flag to all previously unsupported command line commands (#6521)
In addition to those commands already supported:

* build
* check
* eval
* fmt
* test

support has been added to the following commands:

* `bench`
* `deps`
* `exec`
* `inspect`
* `parse`
* `run` (command `server` and `REPL`)

Fixes: #6520

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-01-24 15:42:32 +01:00
Ronnie-personal c3854aa6ae feat(cmd): Adding fail-non-empty flag (#6153)
Add fail-non-empty flag to opa exec

Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com>
2023-08-21 11:30:25 +02:00
Byron Lagrone 235ee309da cmd/exec: add --fail and --fail-defined flags (#5295)
Adds the ability to exit with a non-zero exit code for 'opa exec' by adding the
--fail and --fail-defined flags matching their respective behaviors in 'opa eval':

- Setting the --fail-defined flag allows exit of opa exec with a zero code if all
   results are undefined and there are no errors, or a non-zero code in the
   event of any defined results and/or errors.
   On non-zero exits the error message includes the number of failures/errors
   as well as a reference to the --fail-defined flag being set.
- The --fail flag behaves as the inverse of --fail-defined.

Fixes: #5007

Signed-off-by: Byron Lagrone <byron.lagrone@seqster.com>
2022-10-26 10:43:07 +02:00
Stephan Renatus a8f8b0ef78 cmd/exec: convert slashes of explicit bundles (#5219)
The incoming relative path is now converted as follows:

1. filepath.Abs:     foo/bar          -> c:\a\b\c\foo\bar
2. filepath.ToShasl: c:\a\b\c\foo\bar -> c:/a/b/c/foo/bar
3. prepend `file://`

That's something the runtime can make sense of, and the smoke test passes.

Fixes #5134.

Includes:
* ci: add smoke test with a bundle

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-10-10 13:06:09 +02:00
Sergey Vilgelm 5e51af9244 logger: Configure timestamp format (#5050)
With this, we allow the user to configure the logger's timestamp format by:
* cli argument `log-timestamp-format`
* environment variable `OPA_LOG_TIMESTAMP_FORMAT`

Fixes #2413.

Signed-off-by: Sergey Vilgelm <sergey@vilgelm.com>
2022-08-29 14:23:48 +02:00
Torin Sandall 55b053b79a cmd/exec: Add new exec subcommand
This is just a skeleton but the basic functionality is there: run OPA
in a "one shot" mode against a set of input files and print the
results for each.

Fixes #3525

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2021-12-22 17:46:37 +01:00