1
0
Fork 0
mirror of https://github.com/Luzifer/rconfig.git synced 2024-12-20 19:21:19 +00:00

Update deps, fix linter errors, bump min Go version

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-12-20 12:13:59 +01:00
parent 4c83f34ecf
commit c1c878fab9
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
10 changed files with 120 additions and 116 deletions

View file

@ -2,6 +2,8 @@ package rconfig
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDeriveEnvVarName(t *testing.T) {
@ -17,13 +19,11 @@ func TestDeriveEnvVarName(t *testing.T) {
"CamelCase": "CAMEL_CASE",
"_foobar": "FOOBAR",
"ILoveGoAndJSONSoMuch": "I_LOVE_GO_AND_JSON_SO_MUCH",
"mrT": "MR_T",
"my_case1": "MY_CASE1",
"MyFieldName": "MY_FIELD_NAME",
"SmallCASE": "SMALL_CASE",
"mrT": "MR_T",
"my_case1": "MY_CASE1",
"MyFieldName": "MY_FIELD_NAME",
"SmallCASE": "SMALL_CASE",
} {
if d := deriveEnvVarName(test); d != expect {
t.Errorf("Derived variable %q did not match expectation %q", d, expect)
}
assert.Equal(t, expect, deriveEnvVarName(test))
}
}

View file

@ -49,21 +49,20 @@ func init() {
// For your configuration struct you can use the following struct-tags to control
// the behavior of rconfig:
//
// default: Set a default value
// vardefault: Read the default value from the variable defaults
// env: Read the value from this environment variable
// flag: Flag to read in format "long,short" (for example "listen,l")
// description: A help text for Usage output to guide your users
// default: Set a default value
// vardefault: Read the default value from the variable defaults
// env: Read the value from this environment variable
// flag: Flag to read in format "long,short" (for example "listen,l")
// description: A help text for Usage output to guide your users
//
// The format you need to specify those values you can see in the example to this
// function.
//
func Parse(config interface{}) error {
return parse(config, nil)
}
// ParseAndValidate works exactly like Parse but implements an additional run of
// the go-validator package on the configuration struct. Therefore additonal struct
// the go-validator package on the configuration struct. Therefore additional struct
// tags are supported like described in the readme file of the go-validator package:
//
// https://github.com/go-validator/validator/tree/v2#usage
@ -103,14 +102,20 @@ func SetVariableDefaults(defaults map[string]string) {
variableDefaults = defaults
}
func parseAndValidate(in interface{}, args []string) error {
if err := parse(in, args); err != nil {
//revive:disable-next-line:confusing-naming // The public function is only a wrapper with less args
func parseAndValidate(in interface{}, args []string) (err error) {
if err = parse(in, args); err != nil {
return err
}
return validator.Validate(in)
if err = validator.Validate(in); err != nil {
return fmt.Errorf("validating values: %w", err)
}
return nil
}
//revive:disable-next-line:confusing-naming // The public function is only a wrapper with less args
func parse(in interface{}, args []string) error {
if args == nil {
args = os.Args
@ -123,27 +128,26 @@ func parse(in interface{}, args []string) error {
}
if err := fs.Parse(args); err != nil {
return err
return fmt.Errorf("parsing flag-set: %w", err)
}
if afterFuncs != nil {
for _, f := range afterFuncs {
if err := f(); err != nil {
return err
}
for _, f := range afterFuncs {
if err := f(); err != nil {
return err
}
}
return nil
}
//nolint:funlen,gocognit,gocyclo // Hard to split
func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
if reflect.TypeOf(in).Kind() != reflect.Ptr {
return nil, errors.New("Calling parser with non-pointer")
return nil, errors.New("calling parser with non-pointer")
}
if reflect.ValueOf(in).Elem().Kind() != reflect.Struct {
return nil, errors.New("Calling parser with pointer to non-struct")
return nil, errors.New("calling parser with pointer to non-struct")
}
afterFuncs := []afterFunc{}
@ -166,11 +170,10 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
case reflect.TypeOf(time.Duration(0)):
v, err := time.ParseDuration(value)
if err != nil {
if value == "" {
v = time.Duration(0)
} else {
return nil, err
if value != "" {
return nil, fmt.Errorf("parsing time.Duration: %w", err)
}
v = time.Duration(0)
}
if typeField.Tag.Get("flag") != "" {
@ -215,14 +218,13 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
matched := false
for _, tf := range timeParserFormats {
if t, err := time.Parse(tf, *sVar); err == nil {
matched = true
valField.Set(reflect.ValueOf(t))
return nil
}
}
if !matched {
return fmt.Errorf("Value %q did not match expected time formats", *sVar)
return fmt.Errorf("value %q did not match expected time formats", *sVar)
}
return nil
@ -259,11 +261,10 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
vt, err := strconv.ParseInt(value, 10, 64)
if err != nil {
if value == "" {
vt = 0
} else {
return nil, err
if value != "" {
return nil, fmt.Errorf("parsing int: %w", err)
}
vt = 0
}
if typeField.Tag.Get("flag") != "" {
registerFlagInt(typeField.Type.Kind(), fs, valField.Addr().Interface(), parts, vt, typeField.Tag.Get("description"))
@ -274,11 +275,10 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
vt, err := strconv.ParseUint(value, 10, 64)
if err != nil {
if value == "" {
vt = 0
} else {
return nil, err
if value != "" {
return nil, fmt.Errorf("parsing uint: %w", err)
}
vt = 0
}
if typeField.Tag.Get("flag") != "" {
registerFlagUint(typeField.Type.Kind(), fs, valField.Addr().Interface(), parts, vt, typeField.Tag.Get("description"))
@ -289,11 +289,10 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
case reflect.Float32, reflect.Float64:
vt, err := strconv.ParseFloat(value, 64)
if err != nil {
if value == "" {
vt = 0.0
} else {
return nil, err
if value != "" {
return nil, fmt.Errorf("parsing float: %w", err)
}
vt = 0.0
}
if typeField.Tag.Get("flag") != "" {
registerFlagFloat(typeField.Type.Kind(), fs, valField.Addr().Interface(), parts, vt, typeField.Tag.Get("description"))
@ -315,7 +314,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
for _, v := range strings.Split(value, ",") {
it, err := strconv.ParseInt(strings.TrimSpace(v), 10, 64)
if err != nil {
return nil, err
return nil, fmt.Errorf("parsing int: %w", err)
}
def = append(def, int(it))
}
@ -329,7 +328,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
if len(del) == 0 {
del = ","
}
var def = []string{}
def := []string{}
if value != "" {
def = strings.Split(value, del)
}
@ -355,9 +354,9 @@ func registerFlagFloat(t reflect.Kind, fs *pflag.FlagSet, field interface{}, par
}
case reflect.Float64:
if len(parts) == 1 {
fs.Float64Var(field.(*float64), parts[0], float64(vt), desc)
fs.Float64Var(field.(*float64), parts[0], vt, desc)
} else {
fs.Float64VarP(field.(*float64), parts[0], parts[1], float64(vt), desc)
fs.Float64VarP(field.(*float64), parts[0], parts[1], vt, desc)
}
}
}
@ -384,9 +383,9 @@ func registerFlagInt(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts
}
case reflect.Int64:
if len(parts) == 1 {
fs.Int64Var(field.(*int64), parts[0], int64(vt), desc)
fs.Int64Var(field.(*int64), parts[0], vt, desc)
} else {
fs.Int64VarP(field.(*int64), parts[0], parts[1], int64(vt), desc)
fs.Int64VarP(field.(*int64), parts[0], parts[1], vt, desc)
}
}
}
@ -419,9 +418,9 @@ func registerFlagUint(t reflect.Kind, fs *pflag.FlagSet, field interface{}, part
}
case reflect.Uint64:
if len(parts) == 1 {
fs.Uint64Var(field.(*uint64), parts[0], uint64(vt), desc)
fs.Uint64Var(field.(*uint64), parts[0], vt, desc)
} else {
fs.Uint64VarP(field.(*uint64), parts[0], parts[1], uint64(vt), desc)
fs.Uint64VarP(field.(*uint64), parts[0], parts[1], vt, desc)
}
}
}

View file

@ -2,12 +2,14 @@ package rconfig
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestErrors(t *testing.T) {
for test, parsable := range map[string]interface{}{
"use string as default to int": struct {
A int `default:"a"`
A int `default:"a"` //revive:disable-line:struct-tag // Intentional error for testing
}{},
"use string as default to float": struct {
A float32 `default:"a"`
@ -24,18 +26,12 @@ func TestErrors(t *testing.T) {
A []int `default:"a,b"`
}{},
} {
if err := parse(&parsable, nil); err == nil {
t.Errorf("Expected error but got none. Test: %s", test)
}
assert.Error(t, parse(&parsable, nil), test) //#nosec:G601 // Fine for this test
}
if err := parse(struct {
assert.Error(t, parse(struct {
A string `default:"a"`
}{}, nil); err == nil {
t.Errorf("Expected error when feeding non-pointer struct to parse")
}
}{}, nil), "feeding non-pointer to parse")
if err := parse("test", nil); err == nil {
t.Errorf("Expected error when feeding non-pointer string to parse")
}
assert.Error(t, parse("test", nil), "feeding non-pointer string to parse")
}

View file

@ -1,8 +1,10 @@
package rconfig
package rconfig_test
import (
"fmt"
"os"
"github.com/Luzifer/rconfig/v2"
)
func ExampleParse() {
@ -23,14 +25,16 @@ func ExampleParse() {
"--user=Luzifer",
}
Parse(&config)
if err := rconfig.Parse(&config); err != nil {
panic(err)
}
fmt.Printf("Hello %s, happy birthday for your %dth birthday.",
config.Username,
config.Details.Age)
// You can also show an usage message for your user
Usage()
rconfig.Usage()
// Output:
// Hello Luzifer, happy birthday for your 25th birthday.

View file

@ -4,6 +4,9 @@ import (
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGeneralExecution(t *testing.T) {
@ -20,14 +23,10 @@ func TestGeneralExecution(t *testing.T) {
)
exec := func(desc string, tests [][2]interface{}) {
if err := parse(&cfg, args); err != nil {
t.Fatalf("Parsing options caused error: %s", err)
}
require.NoError(t, parse(&cfg, args))
for _, test := range tests {
if !reflect.DeepEqual(reflect.ValueOf(test[0]).Elem().Interface(), test[1]) {
t.Errorf("%q expected value does not match: %#v != %#v", desc, test[0], test[1])
}
assert.Equal(t, test[1], reflect.ValueOf(test[0]).Elem().Interface(), desc)
}
}
@ -51,11 +50,11 @@ func TestGeneralExecution(t *testing.T) {
cfg = test{}
args = []string{}
os.Setenv("shell", "test546")
require.NoError(t, os.Setenv("shell", "test546"))
exec("no arguments and set env", [][2]interface{}{
{&cfg.Test, "test546"},
})
os.Unsetenv("shell")
require.NoError(t, os.Unsetenv("shell"))
cfg = test{}
args = []string{
@ -70,9 +69,7 @@ func TestGeneralExecution(t *testing.T) {
{&cfg.DefaultFlag, "goo"},
})
if !reflect.DeepEqual(Args(), []string{"positional1", "positional2"}) {
t.Errorf("expected positional arguments to match")
}
assert.Equal(t, []string{"positional1", "positional2"}, Args())
}
func TestValidationIntegration(t *testing.T) {
@ -85,7 +82,5 @@ func TestValidationIntegration(t *testing.T) {
args = []string{}
)
if err := parseAndValidate(&cfgValidated, args); err == nil {
t.Errorf("Expected error, got none")
}
assert.Error(t, parseAndValidate(&cfgValidated, args))
}

12
go.mod
View file

@ -1,9 +1,15 @@
module github.com/Luzifer/rconfig/v2
go 1.17
go 1.20
require (
github.com/spf13/pflag v1.0.5
gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0
gopkg.in/yaml.v2 v2.4.0
github.com/stretchr/testify v1.8.4
gopkg.in/validator.v2 v2.0.1
gopkg.in/yaml.v3 v3.0.1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)

18
go.sum
View file

@ -1,8 +1,16 @@
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/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
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=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0 h1:EFLtLCwd8tGN+r/ePz3cvRtdsfYNhDEdt/vp6qsT+0A=
gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/validator.v2 v2.0.1 h1:xF0KWyGWXm/LM2G1TrEjqOu4pa6coO9AlWSf3msVfDY=
gopkg.in/validator.v2 v2.0.1/go.mod h1:lIUZBlB3Im4s/eYp39Ry/wkR02yOPhZ9IwIRBjuPuG8=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -3,6 +3,9 @@ package rconfig
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPrecedence(t *testing.T) {
@ -11,7 +14,6 @@ func TestPrecedence(t *testing.T) {
}
var (
err error
cfg testcfg
args []string
vardefaults map[string]string
@ -20,20 +22,14 @@ func TestPrecedence(t *testing.T) {
exec := func(desc string, fn func() interface{}, exp interface{}) {
cfg = testcfg{}
SetVariableDefaults(vardefaults)
err = parse(&cfg, args)
assert.NoError(t, parse(&cfg, args), desc)
if err != nil {
t.Errorf("%q parsing caused error: %s", desc, err)
}
if res := fn(); res != exp {
t.Errorf("%q expected value does not match: %#v != %#v", desc, res, exp)
}
assert.Equal(t, exp, fn())
}
// Provided: Flag, Env, Default, VarDefault
args = []string{"-a", "5"}
os.Setenv("a", "8")
require.NoError(t, os.Setenv("a", "8"))
vardefaults = map[string]string{
"a": "3",
}
@ -42,7 +38,7 @@ func TestPrecedence(t *testing.T) {
// Provided: Env, Default, VarDefault
args = []string{}
os.Setenv("a", "8")
require.NoError(t, os.Setenv("a", "8"))
vardefaults = map[string]string{
"a": "3",
}
@ -51,7 +47,7 @@ func TestPrecedence(t *testing.T) {
// Provided: Default, VarDefault
args = []string{}
os.Unsetenv("a")
require.NoError(t, os.Unsetenv("a"))
vardefaults = map[string]string{
"a": "3",
}
@ -60,7 +56,7 @@ func TestPrecedence(t *testing.T) {
// Provided: Default
args = []string{}
os.Unsetenv("a")
require.NoError(t, os.Unsetenv("a"))
vardefaults = map[string]string{}
exec("Provided: Default", func() interface{} { return cfg.A }, 1)

View file

@ -1,14 +1,14 @@
package rconfig
import (
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)
// VarDefaultsFromYAMLFile reads contents of a file and calls VarDefaultsFromYAML
func VarDefaultsFromYAMLFile(filename string) map[string]string {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename) //#nosec:G304 // Loading file from var is intended
if err != nil {
return make(map[string]string)
}

View file

@ -1,10 +1,12 @@
package rconfig
import (
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVardefaultParsing(t *testing.T) {
@ -18,6 +20,7 @@ func TestVardefaultParsing(t *testing.T) {
var (
cfg test
args = []string{}
err error
vardefaults = map[string]string{
"my_secret_value": "veryverysecretkey",
"unkownkey": "hi there",
@ -26,14 +29,10 @@ func TestVardefaultParsing(t *testing.T) {
)
exec := func(desc string, tests [][2]interface{}) {
if err := parse(&cfg, args); err != nil {
t.Fatalf("Parsing options caused error: %s", err)
}
require.NoError(t, parse(&cfg, args))
for _, test := range tests {
if !reflect.DeepEqual(reflect.ValueOf(test[0]).Elem().Interface(), test[1]) {
t.Errorf("%q expected value does not match: %#v != %#v", desc, test[0], test[1])
}
assert.Equal(t, test[1], reflect.ValueOf(test[0]).Elem().Interface(), desc)
}
}
@ -53,13 +52,14 @@ func TestVardefaultParsing(t *testing.T) {
{&cfg.SomeVar, ""},
})
tmp, _ := ioutil.TempFile("", "")
defer func() {
tmp.Close()
os.Remove(tmp.Name())
}()
tmp, _ := os.CreateTemp("", "")
t.Cleanup(func() {
tmp.Close() //nolint:errcheck,gosec,revive // Just cleanup, will be closed automatically
os.Remove(tmp.Name()) //nolint:errcheck,gosec,revive // Just cleanup of tmp-file
})
yamlData := "---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n"
tmp.WriteString(yamlData)
_, err = tmp.WriteString(yamlData)
require.NoError(t, err)
SetVariableDefaults(VarDefaultsFromYAMLFile(tmp.Name()))
exec("defaults from YAML file", [][2]interface{}{
{&cfg.IntVar, int64(42)},