Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cmd/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,20 @@ The optional "gobench" output format conforms to the Go Benchmark Data Format.
}
return validateEvalParams(&params.evalCommandParams, args)
},
Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

exit, err := benchMain(args, params, os.Stdout, &goBenchRunner{})
if err != nil {
// NOTE: err should only be non-nil if a (highly unlikely)
// presentation error occurs.
fmt.Fprintf(os.Stderr, "error: %v\n", err)
}
os.Exit(exit)
if exit != 0 {
return newExitError(exit)
}
return nil
},
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,15 @@ against OPA v0.22.0:
}
return env.CmdFlags.CheckEnvironmentVariables(Cmd)
},
Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

if err := dobuild(buildParams, args); err != nil {
fmt.Println("error:", err)
os.Exit(1)
return err
}
return nil
},
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ Print the capabilities of a capabilities file
PreRunE: func(cmd *cobra.Command, _ []string) error {
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
RunE: func(*cobra.Command, []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

cs, err := doCapabilities(capabilitiesParams)
if err != nil {
return err
Expand Down
8 changes: 6 additions & 2 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,15 @@ and exit with a non-zero exit code.`,
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},

Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

if err := checkModules(checkParams, args); err != nil {
outputErrors(checkParams.format.String(), err)
os.Exit(1)
return err
}
return nil
},
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ data.policy.is_admin.
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true
if err := deps(args, params, os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
return err
}
return nil
},
}

Expand Down
20 changes: 15 additions & 5 deletions cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,18 @@ const (
defaultPrettyLimit = 80
)

type regoError struct{}
type regoError struct {
wrapped error
}

func (regoError) Error() string {
return "rego"
}

func (r regoError) Unwrap() error {
return r.wrapped
}

func init() {
params := newEvalCommandParams()

Expand Down Expand Up @@ -295,18 +301,22 @@ access.
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

defined, err := eval(args, params, os.Stdout)
if err != nil {
if _, ok := err.(regoError); !ok {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(2)
return newExitErrorWrap(2, err)
}

if (params.fail && !defined) || (params.failDefined && defined) {
os.Exit(1)
return newExitError(1)
}
return nil
},
}

Expand Down Expand Up @@ -444,7 +454,7 @@ func eval(args []string, params evalCommandParams, w io.Writer) (bool, error) {
// If the rego package returned an error, return a special error here so
// that the command doesn't print the same error twice. The error will
// have been printed above by the presentation package.
return false, regoError{}
return false, regoError{wrapped: result.Errors}
} else if len(result.Result) == 0 {
return false, nil
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ e.g., opa exec --decision /foo/bar/baz ...
PreRunE: func(cmd *cobra.Command, _ []string) error {
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

params.Paths = args
params.BundlePaths = bundlePaths.v
if err := runExec(params); err != nil {
logging.Get().WithFields(map[string]any{"err": err}).Error("Unexpected error.")
os.Exit(1)
return err
}
return nil
},
}

Expand Down
11 changes: 9 additions & 2 deletions cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,15 @@ different Rego versions:
PreRunE: func(cmd *cobra.Command, _ []string) error {
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
os.Exit(opaFmt(args, fmtParams))
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true
exit := opaFmt(args, fmtParams)
if exit != 0 {
return newExitError(exit)
}
return nil

},
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ that file and summarize its structure and contents.
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

if err := doInspect(params, args[0], os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
return err
}
return nil
},
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ by the input location.`,
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

if err := dofindDefinition(findDefinitionParams, os.Stdin, os.Stdout, args); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
return err
}
return nil
},
}

Expand Down
11 changes: 9 additions & 2 deletions cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ var parseCommand = &cobra.Command{
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
os.Exit(parse(args, &configuredParseParams, os.Stdout, os.Stderr))
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

exit := parse(args, &configuredParseParams, os.Stdout, os.Stderr)
if exit != 0 {
return newExitError(exit)
}
return nil
},
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/refactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ The 'move' command outputs the below policy to stdout with the package name rewr
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

if err := doMove(moveCommandParams, args, os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
return err
}
return nil
},
}

Expand Down
16 changes: 9 additions & 7 deletions cmd/run.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still an os.Exit(1) in init(). Dropping it wouldn't impact what you're doing here, from what I understand, but it raises the question: do we need that? If we fail to deprecate a flag, maybe we don't really care and should prioritize a non-bricked build(?).

Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,18 @@ See https://godoc.org/crypto/tls#pkg-constants for more information.
PreRunE: func(cmd *cobra.Command, _ []string) error {
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

ctx := context.Background()
addrSetByUser := cmd.Flags().Changed("addr")
rt, err := initRuntime(ctx, cmdParams, args, addrSetByUser)
if err != nil {
fmt.Println("error:", err)
os.Exit(1)
return err
}
startRuntime(ctx, rt, cmdParams.serverMode)
return startRuntime(ctx, rt, cmdParams.serverMode)
},
}

Expand Down Expand Up @@ -388,12 +391,11 @@ func initRuntime(ctx context.Context, params runCmdParams, args []string, addrSe
return rt, nil
}

func startRuntime(ctx context.Context, rt *runtime.Runtime, serverMode bool) {
func startRuntime(ctx context.Context, rt *runtime.Runtime, serverMode bool) error {
if serverMode {
rt.StartServer(ctx)
} else {
rt.StartREPL(ctx)
return rt.Serve(ctx)
}
return rt.StartREPL(ctx)
}

func verifyCipherSuites(cipherSuites []string) (*[]uint16, error) {
Expand Down
8 changes: 6 additions & 2 deletions cmd/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,15 @@ https://www.openpolicyagent.org/docs/latest/management-bundles/#signature-format
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},

Run: func(_ *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

if err := doSign(args, cmdParams); err != nil {
fmt.Println("error:", err)
os.Exit(1)
return err
}
return nil
},
}

Expand Down
11 changes: 9 additions & 2 deletions cmd/test.go
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startWatcher() still has an os.Exit(1), but it's run in a goroutine, so maybe it's tricky to pull out of there.

Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,15 @@ recommended as some updates might cause them to be dropped by OPA.
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},

Run: func(_ *cobra.Command, args []string) {
os.Exit(opaTest(args, testParams))
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

exit := opaTest(args, testParams)
if exit != 0 {
return newExitError(exit)
}
return nil
},
}

Expand Down
30 changes: 30 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2025 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.

package cmd

import (
"fmt"
)

type ExitError struct {
Exit int
wrapped error
}

func newExitError(exit int) error {
return &ExitError{Exit: exit}
}

func newExitErrorWrap(exit int, err error) error {
return &ExitError{Exit: exit, wrapped: err}
}

func (c *ExitError) Error() string {
return fmt.Sprintf("exit %d", c.Exit)
}

func (c *ExitError) Unwrap() error {
return c.wrapped
}
11 changes: 7 additions & 4 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func init() {
PreRunE: func(cmd *cobra.Command, _ []string) error {
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, _ []string) {
generateCmdOutput(os.Stdout, check)
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true
return generateCmdOutput(os.Stdout, check)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we shouldn't wrap the error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, it's not evaluating user-controlled rego code...

},
}

Expand All @@ -39,7 +41,7 @@ func init() {
RootCommand.AddCommand(versionCommand)
}

func generateCmdOutput(out io.Writer, check bool) {
func generateCmdOutput(out io.Writer, check bool) error {
fmt.Fprintln(out, "Version: "+version2.Version)
fmt.Fprintln(out, "Build Commit: "+version2.Vcs)
fmt.Fprintln(out, "Build Timestamp: "+version2.Timestamp)
Expand All @@ -62,9 +64,10 @@ func generateCmdOutput(out io.Writer, check bool) {
err := checkOPAUpdate(out)
if err != nil {
fmt.Fprintf(out, "Error: %v\n", err)
os.Exit(1)
return err
}
}
return nil
}

func checkOPAUpdate(out io.Writer) error {
Expand Down
Loading
Loading