21 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
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 b942136a4a Use Go 1.22+ int ranges (#7328)
With "some" help from `golangci-lint run --fix ./...`

Signed-off-by: Anders Eknert <anders@styra.com>
2025-01-30 09:57:27 +01: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 a179a24c48 v1 API
All packages, except for `cmd` and `internal`, have been moved into a new `v1` root package.

Old packages are kept for backwards-compatibility reasons. All contained code is replaced with simple type aliases and proxy functions to `v1` implementations.

Old packages default to the Rego v0 syntax, new `v1` packages default to the Rego v1 syntax.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:27:34 +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
Tyler Schade 211b798182 fix improper formatter behavior when comprehension contains comment
Signed-off-by: Tyler Schade <tylerschade99@gmail.com>
2024-11-12 13:30:11 -08: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 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
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
Gianluca Oldani 76e5fda8b7 fmt: report wrong arity for built-in functions
This commit fixes the index out of range error discussed in #5646 and adds error
handling to avoid that `fmt` panics when such errors are encountered by the formatter

The error handling procedure introduced is similar to the one used by the Scanner struct
responsible for the parsing of a `rego` file, since it uses a slice which is filled with
all the eventual errors that can be found during the format procedure

Fixes: #5646
Signed-off-by: Gianluca Oldani <oldanigianluca@gmail.com>
2023-03-27 10:45:11 -07: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
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
Stephan Renatus f7de26ca2a fix codeql go version; use ioutil.Discard. (#3466)
* workflow: fix codeql go version
* cmd/fmt_test: use ioutil.Discard

I had proposed that in a PR review... not thinking that it meant
we couldn't run tests using Go 1.15. My assumption that using

    go 1.15

in go.mod would protect us from that was wrong, apparently.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-05-17 11:06:14 +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