39 Commits

Author SHA1 Message Date
Loïc Rosso 3ede316faa feat: do not overwrite file on fmt without changes (#8223)
* feat: do not overwrite file on fmt without changes

When no changes are detected, avoid unnecessary rewrite.

It also help fmt CI tools not to see modified files like treefmt

Signed-off-by: Loïc Rosso <37541460+Loic-R@users.noreply.github.com>

* chore: add tests

Signed-off-by: Loïc Rosso <37541460+Loic-R@users.noreply.github.com>

* chore: fix tests by adding 2 sec sleep time

Signed-off-by: Loïc Rosso <37541460+Loic-R@users.noreply.github.com>

* chore: reduce sleep time, add permission check and better naming

Co-authored-by: Charlie Egan <charlie_egan@apple.com>
Signed-off-by: Loïc Rosso <37541460+Loic-R@users.noreply.github.com>

---------

Signed-off-by: Loïc Rosso <37541460+Loic-R@users.noreply.github.com>
Co-authored-by: Charlie Egan <charlie_egan@apple.com>
2026-01-20 16:54:26 +00: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
Johan Fylling 817b6635a8 ast,format: Allowing keywords in Rego references (#7709)
Updating the parser and formatter to allow keywords in refs.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2025-06-25 15:19:21 +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
Charlie Egan 92bee25cc6 fmt: Fix v0-compatible fmt with stdin (#7410)
Fixes https://github.com/open-policy-agent/opa/issues/7409

Signed-off-by: Charlie Egan <charlie@styra.com>
2025-03-04 13:14:19 +00:00
Anders Eknert cc4783a0cc perf: opa fmt 3x faster formatting (#7341)
We have a Regal rule checking that a file is formatted, making the
performance of the formatter more important than it normally might be.

Since I did some work on this in Regal recently, I was curious to see
if this could be improved. Turned out to be a few bottlenecks that we
could remove with great results. The Regal policies are possibly not
representative for all kinds of Rego, and one thing that stands out is
thay they have a *lot* of metadata annotations. Turned out that comment
processing was the main bottleneck, so improving this meant a huge
boost for formatting a typical Regal policy. The improvements here
should make formatting of any file faster though.

**BenchmarkFormatLargePolicy-10 main**
```
382	   3064960 ns/op	 4573131 B/op	   26266 allocs/op
```

**BenchmarkFormatLargePolicy-10 opa-fmt-perf**
```
1396	    812859 ns/op	  362651 B/op	    8811 allocs/op
```

Signed-off-by: Anders Eknert <anders@styra.com>
2025-02-06 10:37:52 +01:00
Charlie Egan 6de4565f4d docs: Use preformatted strings in fmt help (#7263)
Without this, the `--` are rendered in HTML as `–` making the commands
with flags incorrect in the docs.

Signed-off-by: Charlie Egan <charlie@styra.com>
2025-01-14 16:23:30 +00:00
Johan Fylling 24c45fcdc4 Renaming --rego-v1 cmd flag to --v0-v1 (#7225)
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-18 14:10:25 +01:00
Johan Fylling f88306274f Updating formatter to not drop rego.v1 and future.keywords imports (#7224)
to maximize compatibility surface across OPA versions.

Adding `--drop-v0-imports` flag to `opa fmt` for opting in to dropping redundant v0 imports.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-17 17:12: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
Stephan Renatus 20885fe4a9 golangci: bump version, addess all new findings
The previous version has been failing without any good reason for me,
so let's try this.

About the version pick: It's not the latest version (v1.62.0 at the
moment), because that would introduce a new revive rule,
redeclares-builtin-id, and that flags every variable called `min` or
`max` in the code base. I had started addressing these, but they were
just too many.

The new issues related to this version are mostly that it complains
whenever it finds a non-static string that makes its way into a printf-
like function. However, that's a common pattern in some place here, so
I've sprinkled some nolint:govet on it.

Signed-off-by: Stephan Renatus <stephan@styra.com>
2024-11-14 20:36:50 +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
Johan Fylling e8aa24c262 format: Produce error when --rego-v1 formatted module has rule name conflicting with keyword (#6867)
Fixes: #6833
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-07-11 10:31:14 +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
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 38c2f0c5e0 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>
2023-12-20 10:59:14 +01:00
Colin J Lacy 2f431321ef cmd: removes hidden dependency on diff command (#6452)
Replace call to local diff command as a hidden dependency
with call to go-diff, which is already included in the repo.

Fixes #6284

Signed-off-by: Colin Lacy <colinjlacy@gmail.com>
2023-12-04 09:21:29 +01:00
Johan Fylling 187d688c58 cmd & format: Adding rego-v1 mode to opa fmt (#6413)
Fixes: #6297
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2023-11-30 11:00:01 +01:00
Anders Eknert 9a597feb2e chore: don't use the deprecated ioutil functions (#5319)
Another annoyance removed :P

Signed-off-by: Anders Eknert <anders@eknert.com>
2022-10-27 14:30:26 +02:00
David Kuridža 6e5c0fc723 format: Output list and diff changes with --fail flag (#4508) (#4710)
The change enables using --diff and --list together with --fail as
discussed in #4508, for example:

	$ opa fmt [path [...]] --list --fail; exit $?
	path/to/file-1.rego
	path/to/file-2.rego
	unexpected diff
	2

Previously, the same command returned only the error:

	$ opa fmt [path [...]] --list --fail; exit $?
	unexpected diff
	2

Signed-off-by: David Kuridža <david@kuridza.si>
2022-06-05 21:44:46 +02:00
André Håland c6344d0525 cmd/fmt: fix incorrect help text (#3520)
The help text for opa fmt stated that -e could
be used as shorthand. This is not correct.
This commit changed to the correct flag --fail.

Fixes: #3518

Signed-off-by: Andre Håland <andre.haland@gmail.com>
2021-05-31 18:15:34 +02:00
Will Beason 3be1d08b87 Change check-lint to use golangci-lint (#3465)
golint is deprecated. The author of the code no longer supports the
codebase. golangci-lint is faster than golint, and is in use by other
opa repositories (e.g. Gatekeeper).

This commit changes tools.go to reference golangci (so it ends up in
vendor) and modifies check-lint to use golangci instead.

Breaking API Changes:

- plugins/rest/rest.go: Fix typo "AllowInsureTLS" -> "AllowInsecureTLS"
- storage/errors.go: Removed unused IndexingNotSupportedErr

Signed-off-by: Will Beason <willbeason@google.com>
2021-05-19 07:52:02 +02:00
Joshua Shanks b118e2b611 cmd/fmt: Add flag to return non zero exit code on diff (#3445)
Running fmt during CI/CD is a common practice to ensure
files being merged are properly formatted. While this
can be accomplished with shell commands the OPA docker
image doesn't ship with a shell so adding this flag to
return a non zero error code allows the docker image to
be used in CI/CD.

Signed-off-by: Joshua Shanks <jjshanks@gmail.com>
2021-05-11 16:42:52 +02:00
Patrick East 47c7e6e47b cmd/fmt: Only list files if there were changes
Fixes an issue introduced in https://github.com/open-policy-agent/opa/pull/2249
where we would now always list the filename, regardless of whether or
not the file had changes.

This corrects the behavior to only do it when there are changes in the
formatted output and the original content.

Fixes: #2295
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-04-13 17:18:08 -07:00
Patrick East fd79a7e0c7 cmd/fmt: Always write to stdout
There was an explicit check that would prevent writing to stdout if
the formatting didn't change anything.

This makes sense for the "write to file" or "diff" options as we can
save work by bailing out early, but if configured to write the file
contents to stdout we should do so in all cases.

Fixes: #2235
Signed-off-by: Patrick East <east.patrick@gmail.com>
2020-04-02 05:36:32 -07:00
Patrick East b48c534722 Run make fmt with new goimports cmd
Signed-off-by: Patrick East <east.patrick@gmail.com>
2019-09-27 09:55:11 -04:00
Torin Sandall 6a0fec7fb4 cmd: Fix eval and fmt to support file:// URLs
Users should be able to pass file:// URLs to any of the
sub-commands. In 3be55ed6 the eval and fmt sub-commands were not
updated to accept file:// URLs for the input file and normal paths
(respectively).

This commit just moves the unexported cleanFileURL function from the
loader package into it's own internal package so that it can be shared
in OPA.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-09-06 12:29:37 -07:00
ethan 379b9bdf0e cleanup up fmt.go help message typo
Signed-off-by: Guangming Wang <guangming.wang@daocloud.io>
2019-08-11 13:40:57 -07:00
Brendan Ryan 350a74832e make path optional
Signed-off-by: Brendan Ryan <brendanjryan@github.com>
2019-07-23 14:23:54 -07:00
Brendan Ryan 57d72ae14a fixes usage of fmt command
Signed-off-by: Brendan Ryan <brendanjryan@github.com>
2019-07-23 14:23:54 -07:00
Torin Sandall 80dfee89b5 Add stdin mode to opa fmt 2017-09-14 08:35:39 -07:00
Torin Sandall 8019c7e6be Remove backup creation from fmt subcommand
Overwriting is not the default and policies are usually stored in a VCS.
As a result, the backup files are not likely to be needed.
2017-09-12 17:28:49 -07:00
Torin Sandall 9ab9d8913d Refactor commands to namespace parameters 2017-09-07 11:40:22 -07:00
Torin Sandall 1cb62879e5 Tweak one line descriptions of check and fmt 2017-09-07 11:40:22 -07:00
Matthew Mussomele d4edbf8454 Properly remove temporary files when running opa fmt -d
When computing diffs, `opa fmt` created temporary files in order to use
the diff utility. However, it did not properly clean up after itself,
leaving these temporary files behind.
2017-07-25 17:56:57 -07:00
Matthew Mussomele 2a001ef1c7 Correct opa fmt panic on missing files
If the provided file name does not exist, `opa fmt` would panic
due to filepath.Walk passing in a nil `os.FileInfo`. The WalkFunc
now checks the incoming error properly to catch this case.
2017-07-06 09:31:40 -07:00
Matthew Mussomele a204d79989 Only output filenames when -l is passed to 'opa fmt'
If the -l flag was used without the -w flag also being supplied,
the filenames would be lost in the output. Now, if -l is supplied,
any writes to stdout will be discarded, causing only the filenames
to be displayed.
2017-06-30 10:51:10 -04:00
Matthew Mussomele 2c4c433e7a Implement CLI for opa format 2017-06-29 13:10:36 -04:00