From 9eb558b85f5aa8b086dc820274f4975c20cea3cd Mon Sep 17 00:00:00 2001 From: Caitlin Elfring Date: Sun, 11 Jul 2021 12:49:34 -0400 Subject: [PATCH] Use `github.com/caitlinelfring/go-env-default` package (#87) --- go.mod | 1 + go.sum | 2 ++ pkg/parser/parser.go | 6 +++--- pkg/printer/printer.go | 4 ++-- pkg/util/env.go | 24 ------------------------ pkg/util/env_test.go | 38 -------------------------------------- 6 files changed, 8 insertions(+), 67 deletions(-) delete mode 100644 pkg/util/env.go delete mode 100644 pkg/util/env_test.go diff --git a/go.mod b/go.mod index 81ee34cf..93817ef5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/get-woke/woke go 1.16 require ( + github.com/caitlinelfring/go-env-default v1.0.0 github.com/fatih/color v1.12.0 github.com/get-woke/fastwalk v1.0.0 github.com/get-woke/go-gitignore v1.1.2 diff --git a/go.sum b/go.sum index ad3f6318..1dd329ad 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= +github.com/caitlinelfring/go-env-default v1.0.0 h1:DY8qY3OKb9PrGe+sjop7dw7tOKh6kV8cdZONlESSbZc= +github.com/caitlinelfring/go-env-default v1.0.0/go.mod h1:vY8iS64s+wIBKayqiNGJsWMwc19NrxaNNTyWzuPvE44= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 473cbcaf..b52026ff 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -3,7 +3,6 @@ package parser import ( "os" "sort" - "strconv" "sync" "github.com/get-woke/woke/pkg/ignore" @@ -14,6 +13,7 @@ import ( "github.com/get-woke/woke/pkg/util" "github.com/get-woke/woke/pkg/walker" + env "github.com/caitlinelfring/go-env-default" "github.com/rs/zerolog/log" ) @@ -103,8 +103,8 @@ func (p *Parser) processFindingInPath(path string, done chan bool) { files := p.walkDir(path, done) // run parallel, but bounded - numWorkerStr := util.GetEnvDefault("WORKER_POOL_COUNT", "0") - if numWorker, err := strconv.Atoi(numWorkerStr); err == nil && numWorker > 0 { + numWorker := env.GetIntDefault("WORKER_POOL_COUNT", 0) + if numWorker > 0 { log.Debug().Str("path", path).Str("type", "bounded").Int("workers", numWorker).Msg("process files") wg.Add(numWorkers) diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index e698dcf4..f5dff8d7 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -6,8 +6,8 @@ import ( "strings" "github.com/get-woke/woke/pkg/result" - "github.com/get-woke/woke/pkg/util" + env "github.com/caitlinelfring/go-env-default" "github.com/rs/zerolog/log" ) @@ -45,7 +45,7 @@ func NewPrinter(f string) (Printer, error) { var p Printer switch f { case OutFormatText: - p = NewText(util.GetEnvBoolDefault("DISABLE_COLORS", false)) + p = NewText(env.GetBoolDefault("DISABLE_COLORS", false)) case OutFormatSimple: p = NewSimple() case OutFormatGitHubActions: diff --git a/pkg/util/env.go b/pkg/util/env.go deleted file mode 100644 index a6cb2fdf..00000000 --- a/pkg/util/env.go +++ /dev/null @@ -1,24 +0,0 @@ -package util - -import ( - "os" - "strconv" -) - -// GetEnvDefault returns the value of the environment variable, or a default -// value if the environment variable is not defined or is an empty string -func GetEnvDefault(envVar, defaultValue string) string { - if v, ok := os.LookupEnv(envVar); ok && len(v) > 0 { - return v - } - return defaultValue -} - -// GetEnvBoolDefault is similar to GetEnvDefault, but with booleans instead of strings -func GetEnvBoolDefault(envVar string, defaultValue bool) bool { - val := GetEnvDefault(envVar, strconv.FormatBool(defaultValue)) - if b, err := strconv.ParseBool(val); err == nil { - return b - } - return defaultValue -} diff --git a/pkg/util/env_test.go b/pkg/util/env_test.go deleted file mode 100644 index b515e67f..00000000 --- a/pkg/util/env_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package util - -import ( - "os" - "testing" - - "github.com/stretchr/testify/assert" -) - -// GetEnvDefault returns the value of the environment variable, or a default -// value if the environment variable is not defined or is an empty string -func TestGetEnvDefault(t *testing.T) { - val := GetEnvDefault("MY_ENV", "default_value") - assert.Equal(t, "default_value", val) - - os.Setenv("MY_ENV", "defined_value") - val = GetEnvDefault("MY_ENV", "default_value") - assert.Equal(t, "defined_value", val) - - os.Unsetenv("MY_ENV") -} - -func TestGetEnvBoolDefault(t *testing.T) { - val := GetEnvBoolDefault("MY_BOOL_ENV", true) - assert.Equal(t, true, val) - - os.Setenv("MY_BOOL_ENV", "true") - val = GetEnvBoolDefault("MY_BOOL_ENV", false) - assert.Equal(t, true, val) - - os.Unsetenv("MY_BOOL_ENV") - - os.Setenv("MY_BOOL_ENV", "notABool") - val = GetEnvBoolDefault("MY_BOOL_ENV", true) - assert.Equal(t, true, val) - - os.Unsetenv("MY_BOOL_ENV") -}