Skip to content

Commit

Permalink
breaks out output logger so we can extend in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Ryan committed Jul 28, 2019
1 parent 82138a4 commit dabc0c4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/hashicorp/go-multierror v0.0.0-20180717150148-3d5d8f294aa0
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946
github.com/magiconair/properties v1.8.0 // indirect
github.com/mattn/go-colorable v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce h1:xdsDDbiBDQTKASoGE
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946 h1:z+WaKrgu3kCpcdnbK9YG+JThpOCd1nU5jO5ToVmSlR4=
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.0 h1:v2XXALHHh6zHfYTJ+cSkwtyffnaOyR1MXaA91mTrb8o=
Expand Down
48 changes: 48 additions & 0 deletions kubeval/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 `ccheck` evaluation will be recorded
// and reported to the end user.
type OutputManager interface {
Put(r ValidationResult) error
Flush() error
}

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

// NewDefaultStdOutputManager instantiates a new instance of STDOutputManager using
// the default logger.
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
}
1 change: 1 addition & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ func Error(message ...interface{}) {
red := color.New(color.FgRed)
red.Println(message...)
}

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()
fmt.Println(out)

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 dabc0c4

Please sign in to comment.