Skip to content

Commit

Permalink
Merge pull request #160 from brendanjryan/bjr-output-formatters
Browse files Browse the repository at this point in the history
breaks out output logger into standard interface
  • Loading branch information
garethr committed Aug 4, 2019
2 parents 82138a4 + 22db812 commit 2e4542b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
46 changes: 46 additions & 0 deletions kubeval/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package kubeval

import (
"github.com/instrumenta/kubeval/log"
)

// TODO (brendanryan) move these structs to `/log` once we have removed the potential
// circular dependancy between this package and `/log`

// OutputManager controls how results of the `kubeval` evaluation will be recorded
// and reported to the end user.
type OutputManager interface {
Put(r ValidationResult) error
Flush() error
}

// STDOutputManager reports `kubeval` results to stdout.
type STDOutputManager struct {
}

// NewSTDOutputManager instantiates a new instance of STDOutputManager.
func NewSTDOutputManager() *STDOutputManager {
return &STDOutputManager{}
}

func (s *STDOutputManager) Put(result ValidationResult) error {
if len(result.Errors) > 0 {
log.Warn("The file", result.FileName, "contains an invalid", result.Kind)
for _, desc := range result.Errors {
log.Info("--->", desc)
}
} else if result.Kind == "" {
log.Success("The file", result.FileName, "contains an empty YAML document")
} else if !result.ValidatedAgainstSchema {
log.Warn("The file", result.FileName, "containing a", result.Kind, "was not validated against a schema")
} else {
log.Success("The file", result.FileName, "contains a valid", result.Kind)
}

return nil
}

func (s *STDOutputManager) Flush() error {
// no op
return nil
}
38 changes: 23 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"runtime"
"strings"

multierror "github.com/hashicorp/go-multierror"
"github.com/fatih/color"
multierror "github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -69,7 +69,11 @@ var RootCmd = &cobra.Command{
log.Error(err)
os.Exit(1)
}
success = logResults(results, success)
success, err = logResults(results, success)
if err != nil {
log.Error(err)
os.Exit(1)
}
} else {
if len(args) < 1 && len(directories) < 1 {
log.Error("You must pass at least one file as an argument, or at least one directory to the directories flag")
Expand Down Expand Up @@ -97,7 +101,12 @@ var RootCmd = &cobra.Command{
success = false
continue
}
success = logResults(results, success)
success, err = logResults(results, success)
if err != nil {
log.Error(err)
os.Exit(1)
}

}
}
if !success {
Expand All @@ -106,23 +115,22 @@ var RootCmd = &cobra.Command{
},
}

func logResults(results []kubeval.ValidationResult, success bool) bool {
func logResults(results []kubeval.ValidationResult, success bool) (bool, error) {
//// fetch output logger based on enviroments params -- for now we only support
//// the stdout logger
out := kubeval.NewSTDOutputManager()

for _, result := range results {
if len(result.Errors) > 0 {
success = false
log.Warn("The file", result.FileName, "contains an invalid", result.Kind)
for _, desc := range result.Errors {
log.Info("--->", desc)
}
} else if result.Kind == "" {
log.Success("The file", result.FileName, "contains an empty YAML document")
} else if !result.ValidatedAgainstSchema {
log.Warn("The file", result.FileName, "containing a", result.Kind, "was not validated against a schema")
} else {
log.Success("The file", result.FileName, "contains a valid", result.Kind)
}
err := out.Put(result)
if err != nil {
return success, err
}
}
return success

return success, nil
}

func aggregateFiles(args []string) ([]string, error) {
Expand Down

0 comments on commit 2e4542b

Please sign in to comment.