From d446034ca3fb468e233f0372dbece1e19a65cdde Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Wed, 3 Mar 2021 14:11:32 -0700 Subject: [PATCH 1/4] fix linting Signed-off-by: Jacob Weinstock --- goconfig.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/goconfig.go b/goconfig.go index aaa6fc6..3832ee3 100644 --- a/goconfig.go +++ b/goconfig.go @@ -45,7 +45,7 @@ func NewParser(opts ...Option) *Parser { // Parse a config file, env vars and cli flags (override is in that order). // // The fields of the confStruct passed in must be exported (uppercase). -// +// // CLI flags by default split camelCase field names with dashes. // e.x. `KeyOne` would be a cli flag of `-key-one`. // To modify this, add a struct tag. From 84647a5ac3014d763830fdfa1c056cf38275c313 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Wed, 17 Mar 2021 21:15:58 -0600 Subject: [PATCH 2/4] remove logging dependency on packethost/pkg; too heavy Signed-off-by: Jacob Weinstock --- README.md | 2 +- go.mod | 5 ++++- go.sum | 9 --------- goconfig.go | 18 ++++++++++++++++-- goconfig_test.go | 3 +-- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 51389af..7d5bf7e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## Usage -`example/main.go` +`go run examples/main.go -config examples/example.yaml` ```go package main diff --git a/go.mod b/go.mod index 4d9dfa7..b41c3bc 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,17 @@ go 1.15 require ( github.com/go-logr/logr v0.4.0 + github.com/go-logr/zapr v0.3.0 github.com/google/go-cmp v0.5.4 github.com/hashicorp/go-multierror v1.1.0 github.com/jacobweinstock/registrar v0.3.2 github.com/kelseyhightower/envconfig v1.4.0 github.com/octago/sflags v0.2.0 - github.com/packethost/pkg/log/logr v0.0.0-20210106215246-8e2e62dc8f0c github.com/pelletier/go-toml v1.8.1 github.com/pkg/errors v0.9.1 + github.com/stretchr/testify v1.6.1 // indirect + go.uber.org/multierr v1.6.0 // indirect + go.uber.org/zap v1.16.0 golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect golang.org/x/tools v0.0.0-20201021000207-d49c4edd7d96 // indirect gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index 03ef15e..72d8a6f 100644 --- a/go.sum +++ b/go.sum @@ -4,12 +4,9 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs 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/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= -github.com/go-logr/logr v0.2.1/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/go-logr/logr v0.3.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc= github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= -github.com/go-logr/zapr v0.2.0 h1:v6Ji8yBW77pva6NkJKQdHLAJKrIJKRHz0RXwPqCHSR4= -github.com/go-logr/zapr v0.2.0/go.mod h1:qhKdvif7YF5GI9NWEpyxTSSBdGmzkNguibrdCNVPunU= github.com/go-logr/zapr v0.3.0 h1:iyiCRZ29uPmbO7mWIjOEiYMXrTxZWTyK4tCatLyGpUY= github.com/go-logr/zapr v0.3.0/go.mod h1:qhKdvif7YF5GI9NWEpyxTSSBdGmzkNguibrdCNVPunU= github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= @@ -21,8 +18,6 @@ github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/jacobweinstock/registrar v0.3.2 h1:hINoN2hzuZuoryQjR5pyCtl0vR2FWqolf1ZKI69QFls= github.com/jacobweinstock/registrar v0.3.2/go.mod h1:69P9dxcMVsAKUwiVRwkchq/BJyBXSFSMJcvl7PhsI2k= -github.com/jacobweinstock/rollzap v0.1.3 h1:9nkpwYew+JiDoMWwVIEUpFyos6hdfY3gDmaj6d+Hq9M= -github.com/jacobweinstock/rollzap v0.1.3/go.mod h1:hlnp7hysC0vG3HB+EXl5k8UwCjTroVtvVNqoKUCotak= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -33,8 +28,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/octago/sflags v0.2.0 h1:XceYzkRXGAHa/lSFmKLcaxSrsh4MTuOMQdIGsUD0wlk= github.com/octago/sflags v0.2.0/go.mod h1:G0bjdxh4qPRycF74a2B8pU36iTp9QHGx0w0dFZXPt80= -github.com/packethost/pkg/log/logr v0.0.0-20210106215246-8e2e62dc8f0c h1:WqXIkzydk2C4k24YI8JB3IaAmblZn48VLITbLV7e8Ac= -github.com/packethost/pkg/log/logr v0.0.0-20210106215246-8e2e62dc8f0c/go.mod h1:dg6GvqmNHgL1eGcyw1nu1LiYSp4RQfOXE5khdOTPLs0= github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM= github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -43,8 +36,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE 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/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rollbar/rollbar-go v1.2.0 h1:CUanFtVu0sa3QZ/fBlgevdGQGLWaE3D4HxoVSQohDfo= -github.com/rollbar/rollbar-go v1.2.0/go.mod h1:czC86b8U4xdUH7W2C6gomi2jutLm8qK0OtrF5WMvpcc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/goconfig.go b/goconfig.go index 3832ee3..d188bc4 100644 --- a/goconfig.go +++ b/goconfig.go @@ -1,13 +1,15 @@ package goconfig import ( + "fmt" "reflect" "strings" "github.com/go-logr/logr" + "github.com/go-logr/zapr" "github.com/jacobweinstock/registrar" + "go.uber.org/zap" - plogr "github.com/packethost/pkg/log/logr" "github.com/pkg/errors" ) @@ -24,7 +26,7 @@ type Parser struct { // NewParser parser struct func NewParser(opts ...Option) *Parser { - log, _, _ := plogr.NewPacketLogr() + log := defaultLogger() c := &Parser{ Logger: log, File: "config.yaml", @@ -109,3 +111,15 @@ func getConfigValue(config interface{}) string { } return name } + +// defaultLogger is zap logr implementation +func defaultLogger() logr.Logger { + config := zap.NewProductionConfig() + config.OutputPaths = []string{"stdout"} + zapLogger, err := config.Build() + if err != nil { + panic(fmt.Sprintf("who watches the watchmen (%v)?", err)) + } + + return zapr.NewLogger(zapLogger) +} diff --git a/goconfig_test.go b/goconfig_test.go index 5c45f8d..7bee0d7 100644 --- a/goconfig_test.go +++ b/goconfig_test.go @@ -8,11 +8,10 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/jacobweinstock/registrar" - plogr "github.com/packethost/pkg/log/logr" ) func TestNewParser(t *testing.T) { - log, _, _ := plogr.NewPacketLogr() + log := defaultLogger() usage := func() {} flagI := &gflags{} envI := &envConfig{} From 27c5a0fec3086292eef12f23ae7492d03e5a0235 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Thu, 1 Apr 2021 20:36:09 -0600 Subject: [PATCH 3/4] Add functionality to require fields: Allows require field checking. Signed-off-by: Jacob Weinstock --- examples/main.go | 8 ++++---- go.mod | 1 + go.sum | 5 ++--- goconfig.go | 11 ++++++++++- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/examples/main.go b/examples/main.go index 422ac83..d9a5b12 100644 --- a/examples/main.go +++ b/examples/main.go @@ -7,9 +7,9 @@ import ( ) type config struct { - ConsumerToken string `flag:"consumertoken"` - APIKey string `flag:"apikey"` - Facility string + ConsumerToken string `flag:"consumertoken"` + APIKey string `flag:"apikey"` + Facility string `valid:"required"` HardwareIDs []string `flag:"hardwareids"` Workers int Config string @@ -20,7 +20,7 @@ func main() { config := goconfig.NewParser(goconfig.WithPrefix("TEST"), goconfig.WithFile("example.yaml")) err := config.Parse(&cfg) if err != nil { - fmt.Println(err) + panic(err) } fmt.Printf("%+v\n", cfg) } diff --git a/go.mod b/go.mod index b41c3bc..9a73391 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/jacobweinstock/goconfig go 1.15 require ( + github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/go-logr/logr v0.4.0 github.com/go-logr/zapr v0.3.0 github.com/google/go-cmp v0.5.4 diff --git a/go.sum b/go.sum index 72d8a6f..fee3a22 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ= +github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= 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= @@ -58,7 +60,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE= golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= @@ -78,7 +79,6 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -86,7 +86,6 @@ golang.org/x/tools v0.0.0-20201021000207-d49c4edd7d96 h1:K+nJoPcImWk+ZGPHOKkDocK golang.org/x/tools v0.0.0-20201021000207-d49c4edd7d96/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/goconfig.go b/goconfig.go index d188bc4..bcdbf5d 100644 --- a/goconfig.go +++ b/goconfig.go @@ -5,6 +5,7 @@ import ( "reflect" "strings" + "github.com/asaskevich/govalidator" "github.com/go-logr/logr" "github.com/go-logr/zapr" "github.com/jacobweinstock/registrar" @@ -88,7 +89,15 @@ func (c *Parser) Parse(confStruct interface{}) error { return errors.WithMessage(err, "error parsing cli flags") } - return nil + return validateRequired(confStruct) +} + +// validateRequired will look at the struct tags for a `valid:"required"` tag +// and make sure those fields have a value. +// If a default value is specified for a required field that will satisfy +func validateRequired(config interface{}) error { + _, err := govalidator.ValidateStruct(config) + return err } func (c *Parser) registerFileInterfaces() { From 2dea9f28afc621e2dfc5a1348066032219c8c093 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Thu, 1 Apr 2021 20:55:46 -0600 Subject: [PATCH 4/4] update log level for warning on config file not found Signed-off-by: Jacob Weinstock --- goconfig.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/goconfig.go b/goconfig.go index bcdbf5d..5283f5c 100644 --- a/goconfig.go +++ b/goconfig.go @@ -74,7 +74,7 @@ func (c *Parser) Parse(confStruct interface{}) error { err = ParseFileFromInterfaces(c.Logger, filename, confStruct, c.FileInterface.GetDriverInterfaces()) if err != nil { - c.Logger.V(0).Info("problem parsing file", "file", filename, "error", err.Error()) + c.Logger.V(1).Info("problem parsing file", "file", filename, "error", err.Error()) } // Overwrite config with environment variables