Skip to content

Commit

Permalink
feat: output status in tab format (#98)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Rynhard <[email protected]>
  • Loading branch information
andrewrynhard authored Jan 13, 2019
1 parent e93a47e commit 7646221
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
5 changes: 1 addition & 4 deletions cmd/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ var enforceCmd = &cobra.Command{
opts = append(opts, policy.WithCommitMsgFile(&commitMsgFile))
}

if err = e.Enforce(opts...); err != nil {
fmt.Println(err)
os.Exit(1)
}
e.Enforce(opts...)
},
}

Expand Down
41 changes: 25 additions & 16 deletions internal/enforcer/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"text/tabwriter"

"github.com/autonomy/conform/internal/policy"
"github.com/autonomy/conform/internal/policy/commit"
Expand Down Expand Up @@ -49,40 +50,48 @@ func New() (*Conform, error) {
}

// Enforce enforces all policies defined in the conform.yaml file.
func (c *Conform) Enforce(setters ...policy.Option) error {
func (c *Conform) Enforce(setters ...policy.Option) {
opts := policy.NewDefaultOptions(setters...)

const padding = 8
w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', 0)
fmt.Fprintln(w, "POLICY\tSTATUS\tMESSAGE\t")

var failed bool
for _, p := range c.Policies {
fmt.Printf("Enforcing policy %q ... ", p.Type)
err := c.enforce(p, opts)
if err != nil {
fmt.Printf("FAILED\n")
return err
if errs := c.enforce(p, opts); errs != nil {
failed = true
for _, err := range errs {
fmt.Fprintf(w, "%s\t%s\t%v\t\n", p.Type, "FAILED", err)
}
} else {
fmt.Fprintf(w, "%s\t%s\t%s\t\n", p.Type, "PASS", "<none>")
}
fmt.Printf("PASS\n")
}

return nil
w.Flush()

if failed {
os.Exit(1)
}
}

func (c *Conform) enforce(declaration *PolicyDeclaration, opts *policy.Options) error {
func (c *Conform) enforce(declaration *PolicyDeclaration, opts *policy.Options) []error {
if _, ok := policyMap[declaration.Type]; !ok {
return errors.Errorf("Policy %q is not defined", declaration.Type)
return []error{errors.Errorf("Policy %q is not defined", declaration.Type)}
}

p := policyMap[declaration.Type]

err := mapstructure.Decode(declaration.Spec, p)
if err != nil {
return err
return []error{errors.Errorf("Internal error: %v", err)}
}

report := p.Compliance(opts)

if !report.Valid() {
fmt.Printf("Violation of policy %q:\n", declaration.Type)
for i, err := range report.Errors {
fmt.Printf("\tViolation %d: %v\n", i+1, err)
}
os.Exit(1)
return report.Errors
}

return nil
Expand Down

0 comments on commit 7646221

Please sign in to comment.