Skip to content

Commit

Permalink
feat: add colors to logger and no-color flag to disable (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrielp authored Sep 25, 2022
1 parent 17f8cc2 commit 383966f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -18,6 +27,12 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
18 changes: 16 additions & 2 deletions schemacheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"

"github.com/fatih/color"
flag "github.com/spf13/pflag"
"github.com/xeipuuv/gojsonschema"
"sigs.k8s.io/yaml"
Expand All @@ -19,6 +20,7 @@ const (
fileUsage = "A Yaml or JSON file to check against a given schema. Default: values.json (can acceptable multiples)"
versionUsage = "Prints out the version of schemacheck"
ignoreValidationErrUsage = "Ignores when a document is not valid but provides a warning."
noColorUsage = "Disables color usage for the logger"
)

// Core variables for flag pointers and info, warning, and error loggers.
Expand All @@ -27,6 +29,7 @@ var (
File []string
Schema string
IgnoreValidationErr bool
NoColor bool
VersionFlag bool

// version is set through ldflags by GoReleaser upon build, taking in the most recent tag
Expand All @@ -35,15 +38,16 @@ var (

// Info, warning, and error loggers.
logger = log.New(os.Stderr, "INFO: ", log.Lshortfile)
warnLogger = log.New(os.Stderr, "WARN: ", log.Lshortfile)
errLogger = log.New(os.Stderr, "ERROR: ", log.Lshortfile)
warnLogger = log.New(os.Stderr, color.HiYellowString("WARN: "), log.Lshortfile)
errLogger = log.New(os.Stderr, color.HiRedString("ERROR: "), log.Lshortfile)
)

// Initialize the flags from the command line and their shorthand counterparts.
func init() {
flag.StringVarP(&Schema, "schema", "s", "", schemaUsage)
flag.StringSliceVarP(&File, "file", "f", []string{}, fileUsage)
flag.BoolVar(&IgnoreValidationErr, "ignore-val-err", false, ignoreValidationErrUsage)
flag.BoolVar(&NoColor, "no-color", false, noColorUsage)
flag.BoolVarP(&VersionFlag, "version", "v", false, versionUsage)
}

Expand Down Expand Up @@ -149,6 +153,16 @@ func main() {
os.Exit(0)
}

// set first to false for CI based on fatih/color docs
color.NoColor = false
// set nocolor to true, and reset err and warn loggers because nocolor is not respected by logger definitions when set
// at var level
if NoColor {
color.NoColor = true
warnLogger = log.New(os.Stderr, "WARN: ", log.Lshortfile)
errLogger = log.New(os.Stderr, "ERROR: ", log.Lshortfile)
}

// Check to ensure required flags aren't empty
missingArgs := CheckForEmptyArg()
if missingArgs {
Expand Down

0 comments on commit 383966f

Please sign in to comment.