Skip to content

Commit

Permalink
Address reviews
Browse files Browse the repository at this point in the history
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
  • Loading branch information
oguzhand95 committed Apr 5, 2023
1 parent 51f40c8 commit 2475881
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 33 deletions.
49 changes: 33 additions & 16 deletions cmd/cerbos/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ import (
"os"
"os/signal"

"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/verification"

Check failure on line 14 in cmd/cerbos/compile/compile.go

View workflow job for this annotation

GitHub Actions / Test (3)

other declaration of verification

Check failure on line 14 in cmd/cerbos/compile/compile.go

View workflow job for this annotation

GitHub Actions / Test (3)

other declaration of verification

"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/verification"

Check failure on line 16 in cmd/cerbos/compile/compile.go

View workflow job for this annotation

GitHub Actions / Test (3)

verification redeclared in this block

Check failure on line 16 in cmd/cerbos/compile/compile.go

View workflow job for this annotation

GitHub Actions / Test (3)

"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/verification" imported and not used

Check failure on line 16 in cmd/cerbos/compile/compile.go

View workflow job for this annotation

GitHub Actions / Test (3)

verification redeclared in this block

Check failure on line 16 in cmd/cerbos/compile/compile.go

View workflow job for this annotation

GitHub Actions / Test (3)

"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/verification" imported and not used

"github.com/alecthomas/kong"
"github.com/fatih/color"
"github.com/pterm/pterm"
"go.uber.org/zap"

policyv1 "github.com/cerbos/cerbos/api/genpb/cerbos/policy/v1"
compileerrors "github.com/cerbos/cerbos/cmd/cerbos/compile/errors"
internalcompile "github.com/cerbos/cerbos/cmd/cerbos/compile/internal/compilation"
internalerrors "github.com/cerbos/cerbos/cmd/cerbos/compile/internal/errors"
"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/flagset"
"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/lint"
"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/verification"
"github.com/cerbos/cerbos/internal/compile"
"github.com/cerbos/cerbos/internal/engine"
"github.com/cerbos/cerbos/internal/outputcolor"
Expand All @@ -33,7 +36,8 @@ import (
"github.com/cerbos/cerbos/internal/verify"
)

const help = `
const (
help = `
Examples:
# Compile and run tests found in /path/to/policy/repo
Expand All @@ -48,18 +52,19 @@ cerbos compile --run=Delete /path/to/policy/repo
cerbos compile --skip-tests /path/to/policy/repo
`
)

type Cmd struct { //nolint:govet // Kong prints fields in order, so we don't want to reorder fields to save bytes.
Dir string `help:"Policy directory" arg:"" required:"" type:"existingdir"`
IgnoreSchemas bool `help:"Ignore schemas during compilation"`
Tests string `help:"Path to the directory containing tests. Defaults to policy directory." type:"existingdir"`
RunRegex string `help:"Run only tests that match this regex" name:"run"`
SkipTests bool `help:"Skip tests"`
Output flagset.OutputFormat `help:"Output format (${enum})" default:"tree" enum:"tree,list,json" short:"o"`
TestOutput flagset.VerificationOutputFormat `help:"Test output format (${enum})" default:"tree" enum:"tree,list,json,junit"`
Color *outputcolor.Level `help:"Output color level (auto,never,always,256,16m). Defaults to auto." xor:"color"`
NoColor bool `help:"Disable colored output" xor:"color"`
Verbose bool `help:"Verbose output on test failure"`
Dir string `help:"Policy directory" arg:"" required:"" type:"existingdir"`
IgnoreSchemas bool `help:"Ignore schemas during compilation"`
Tests string `help:"Path to the directory containing tests. Defaults to policy directory." type:"existingdir"`
RunRegex string `help:"Run only tests that match this regex" name:"run"`
SkipTests bool `help:"Skip tests"`
Output flagset.OutputFormat `help:"Output format (${enum})" default:"tree" enum:"tree,list,json" short:"o"`
TestOutput *flagset.VerificationOutputFormat `help:"Test output format. If unspecified matches the value of the output flag. (tree,list,json,junit)"`
Color *outputcolor.Level `help:"Output color level (auto,never,always,256,16m). Defaults to auto." xor:"color"`
NoColor bool `help:"Disable colored output" xor:"color"`
Verbose bool `help:"Verbose output on test failure"`
}

func (c *Cmd) Run(k *kong.Kong) error {
Expand Down Expand Up @@ -110,6 +115,19 @@ func (c *Cmd) Run(k *kong.Kong) error {
return fmt.Errorf("failed to create engine: %w", err)
}

if c.TestOutput == nil {
var value flagset.VerificationOutputFormat
switch c.Output {
case flagset.OutputFormatTree:
value = flagset.VerificationOutputFormatTree
case flagset.OutputFormatList:
value = flagset.VerificationOutputFormatList
case flagset.OutputFormatJSON:
value = flagset.VerificationOutputFormatJSON
}
c.TestOutput = &value
}

if !c.SkipTests {
verifyConf := verify.Config{
Run: c.RunRegex,
Expand All @@ -131,14 +149,13 @@ func (c *Cmd) Run(k *kong.Kong) error {
return fmt.Errorf("failed to run tests: %w", err)
}

err = verification.Display(p, results, c.TestOutput, c.Verbose, colorLevel)
if err != nil {
if err = verification.Display(p, results, *c.TestOutput, c.Verbose, colorLevel); err != nil {
return fmt.Errorf("failed to display test results: %w", err)
}

switch results.Summary.OverallResult {
case policyv1.TestResults_RESULT_FAILED, policyv1.TestResults_RESULT_ERRORED:
return internalerrors.ErrTestsFailed
return compileerrors.ErrTestsFailed
default:
}
}
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions cmd/cerbos/compile/internal/compilation/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package compilation

import (
internalerrors "github.com/cerbos/cerbos/cmd/cerbos/compile/internal/errors"
compileerrors "github.com/cerbos/cerbos/cmd/cerbos/compile/errors"
"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/flagset"
"github.com/cerbos/cerbos/internal/compile"
"github.com/cerbos/cerbos/internal/outputcolor"
Expand All @@ -20,15 +20,15 @@ func Display(p *printer.Printer, errs compile.ErrorList, output flagset.OutputFo
return displayList(p, errs)
}

return internalerrors.ErrFailed
return compileerrors.ErrFailed
}

func displayJSON(p *printer.Printer, errs compile.ErrorList, colorLevel outputcolor.Level) error {
if err := p.PrintJSON(map[string]compile.ErrorList{"compileErrors": errs}, colorLevel); err != nil {
return err
}

return internalerrors.ErrFailed
return compileerrors.ErrFailed
}

func displayList(p *printer.Printer, errs compile.ErrorList) error {
Expand All @@ -37,5 +37,5 @@ func displayList(p *printer.Printer, errs compile.ErrorList) error {
p.Printf("%s: %s (%s)\n", colored.FileName(err.File), colored.ErrorMsg(err.Description), err.Error)
}

return internalerrors.ErrFailed
return compileerrors.ErrFailed
}
10 changes: 10 additions & 0 deletions cmd/cerbos/compile/internal/flagset/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package flagset

import "fmt"

type OutputFormat string

const (
Expand All @@ -13,6 +15,14 @@ const (

type VerificationOutputFormat string

func (v *VerificationOutputFormat) Validate() error {
if !(*v == "tree" || *v == "list" || *v == "json" || *v == "junit") {
return fmt.Errorf("available options are tree, list, json or junit")
}

return nil
}

const (
VerificationOutputFormatTree VerificationOutputFormat = "tree"
VerificationOutputFormatList VerificationOutputFormat = "list"
Expand Down
8 changes: 4 additions & 4 deletions cmd/cerbos/compile/internal/lint/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package lint

import (
internalerrors "github.com/cerbos/cerbos/cmd/cerbos/compile/internal/errors"
compileerrors "github.com/cerbos/cerbos/cmd/cerbos/compile/errors"
"github.com/cerbos/cerbos/cmd/cerbos/compile/internal/flagset"
"github.com/cerbos/cerbos/internal/outputcolor"
"github.com/cerbos/cerbos/internal/printer"
Expand All @@ -20,15 +20,15 @@ func Display(p *printer.Printer, errs *index.BuildError, output flagset.OutputFo
return displayList(p, errs)
}

return internalerrors.ErrFailed
return compileerrors.ErrFailed
}

func displayJSON(p *printer.Printer, errs *index.BuildError, colorLevel outputcolor.Level) error {
if err := p.PrintJSON(map[string]*index.BuildError{"lintErrors": errs}, colorLevel); err != nil {
return err
}

return internalerrors.ErrFailed
return compileerrors.ErrFailed
}

func displayList(p *printer.Printer, errs *index.BuildError) error {
Expand Down Expand Up @@ -72,5 +72,5 @@ func displayList(p *printer.Printer, errs *index.BuildError) error {
p.Println()
}

return internalerrors.ErrFailed
return compileerrors.ErrFailed
}
21 changes: 20 additions & 1 deletion cmd/cerbos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
package main

import (
"errors"

"github.com/alecthomas/kong"

"github.com/cerbos/cerbos/cmd/cerbos/compile"
compileerr "github.com/cerbos/cerbos/cmd/cerbos/compile/errors"
"github.com/cerbos/cerbos/cmd/cerbos/healthcheck"
"github.com/cerbos/cerbos/cmd/cerbos/repl"
"github.com/cerbos/cerbos/cmd/cerbos/run"
Expand All @@ -15,6 +18,11 @@ import (
"github.com/cerbos/cerbos/internal/util"
)

const (
CompileFailureExitCode = 3
TestFailureExitCode = 4
)

func main() {
//nolint: govet
var cli struct {
Expand All @@ -34,5 +42,16 @@ func main() {
outputcolor.TypeMapper,
)

ctx.FatalIfErrorf(ctx.Run())
if err := ctx.Run(); err != nil {
switch {
case errors.Is(err, compileerr.ErrFailed):
ctx.Errorf("%v", err)
ctx.Exit(CompileFailureExitCode)
case errors.Is(err, compileerr.ErrTestsFailed):
ctx.Errorf("%v", err)
ctx.Exit(TestFailureExitCode)
default:
ctx.FatalIfErrorf(err)
}
}
}
19 changes: 11 additions & 8 deletions docs/modules/cli/pages/cerbos.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,19 @@ Arguments:
<dir> Policy directory
Flags:
-h, --help Show context-sensitive help.
-h, --help Show context-sensitive help.
--version
-o, --output="tree" Output format (tree,pretty,json)
--tests=STRING Path to the directory containing tests. Defaults to policy directory.
--run=STRING Run only tests that match this regex
--skip-tests Skip tests
--ignore-schemas Ignore schemas during compilation
--verbose Verbose output on test failure
--color Enable or disable colored output
--ignore-schemas Ignore schemas during compilation
--tests=STRING Path to the directory containing tests. Defaults to policy directory.
--run=STRING Run only tests that match this regex
--skip-tests Skip tests
-o, --output="tree" Output format (tree,list,json)
--test-output="unspecified" Test output format. If unspecified matches the value of the output flag. (unspecified,tree,list,json,junit)
--color=COLOR Output color level (auto,never,always,256,16m). Defaults to auto.
--no-color Disable colored output
--verbose Verbose output on test failure
----

[#healthcheck]
Expand Down

0 comments on commit 2475881

Please sign in to comment.