-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce config.FromEnv() #41
base: main
Are you sure you want to change the base?
Changes from all commits
e0acf4b
dabd0d6
f06ae78
692eb92
316f0d8
ab056b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package config | |
import ( | ||
stderrors "errors" | ||
"fmt" | ||
"github.com/caarlos0/env/v11" | ||
"github.com/creasty/defaults" | ||
"github.com/goccy/go-yaml" | ||
"github.com/jessevdk/go-flags" | ||
|
@@ -50,6 +51,28 @@ func FromYAMLFile(name string, v Validator) error { | |
return nil | ||
} | ||
|
||
// EnvOptions is a type alias for [env.Options], so that only this package needs to import [env]. | ||
type EnvOptions = env.Options | ||
|
||
// FromEnv parses environment variables and stores the result in the value pointed to by v. | ||
// If v is nil or not a pointer, FromEnv returns an [ErrInvalidArgument] error. | ||
func FromEnv(v Validator, options EnvOptions) error { | ||
rv := reflect.ValueOf(v) | ||
if rv.Kind() != reflect.Ptr || rv.IsNil() { | ||
return errors.Wrapf(ErrInvalidArgument, "non-nil pointer expected, got %T", v) | ||
} | ||
|
||
if err := defaults.Set(v); err != nil { | ||
return errors.Wrap(err, "can't set config defaults") | ||
} | ||
|
||
if err := env.ParseWithOptions(v, options); err != nil { | ||
return errors.Wrap(err, "can't parse environment variables") | ||
} | ||
|
||
return errors.Wrap(v.Validate(), "invalid configuration") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not mistaken, we've discussed in other PRs whether we should reduce error checks before I opt for not returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, since I discovered that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the referenced comment, I primarily wanted a consistence between the two checks. Furthermore, when I am reading |
||
} | ||
|
||
// ParseFlags parses CLI flags and stores the result | ||
// in the value pointed to by v. If v is nil or not a pointer, | ||
// ParseFlags returns an [ErrInvalidArgument] error. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package config | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
import ( | ||
"errors" | ||
"github.com/stretchr/testify/require" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type simpleValidator struct { | ||
Foo int `env:"FOO"` | ||
} | ||
|
||
func (sv simpleValidator) Validate() error { | ||
if sv.Foo == 42 { | ||
return nil | ||
} else { | ||
return errors.New("invalid value") | ||
} | ||
} | ||
|
||
type nonStructValidator int | ||
|
||
func (nonStructValidator) Validate() error { | ||
return nil | ||
} | ||
|
||
type defaultValidator struct { | ||
Foo int `env:"FOO" default:"42"` | ||
} | ||
|
||
func (defaultValidator) Validate() error { | ||
return nil | ||
} | ||
|
||
type prefixValidator struct { | ||
Nested simpleValidator `envPrefix:"PREFIX_"` | ||
} | ||
|
||
func (prefixValidator) Validate() error { | ||
return nil | ||
} | ||
|
||
func TestFromEnv(t *testing.T) { | ||
subtests := []struct { | ||
name string | ||
opts EnvOptions | ||
io Validator | ||
error bool | ||
}{ | ||
{name: "nil", error: true}, | ||
{name: "nonptr", io: simpleValidator{}, error: true}, | ||
{name: "nilptr", io: (*simpleValidator)(nil), error: true}, | ||
{name: "defaulterr", io: new(nonStructValidator), error: true}, | ||
{ | ||
name: "parseeerr", | ||
opts: EnvOptions{Environment: map[string]string{"FOO": "bar"}}, | ||
io: &simpleValidator{}, | ||
error: true, | ||
}, | ||
{ | ||
name: "invalid", | ||
opts: EnvOptions{Environment: map[string]string{"FOO": "23"}}, | ||
io: &simpleValidator{}, | ||
error: true, | ||
}, | ||
{name: "simple", opts: EnvOptions{Environment: map[string]string{"FOO": "42"}}, io: &simpleValidator{42}}, | ||
{name: "default", io: &defaultValidator{42}}, | ||
{name: "override", opts: EnvOptions{Environment: map[string]string{"FOO": "23"}}, io: &defaultValidator{23}}, | ||
{ | ||
name: "prefix", | ||
opts: EnvOptions{Environment: map[string]string{"PREFIX_FOO": "42"}, Prefix: "PREFIX_"}, | ||
io: &simpleValidator{42}, | ||
}, | ||
{ | ||
name: "nested", | ||
opts: EnvOptions{Environment: map[string]string{"PREFIX_FOO": "42"}}, | ||
io: &prefixValidator{simpleValidator{42}}, | ||
}, | ||
} | ||
|
||
for _, st := range subtests { | ||
t.Run(st.name, func(t *testing.T) { | ||
var actual Validator | ||
if vActual := reflect.ValueOf(st.io); vActual != (reflect.Value{}) { | ||
if vActual.Kind() == reflect.Ptr && !vActual.IsNil() { | ||
vActual = reflect.New(vActual.Type().Elem()) | ||
} | ||
|
||
actual = vActual.Interface().(Validator) | ||
} | ||
|
||
if err := FromEnv(actual, st.opts); st.error { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, st.io, actual) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package database | ||
|
||
import ( | ||
"github.com/creasty/defaults" | ||
"github.com/icinga/icinga-go-library/config" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
var defaultOptions Options | ||
require.NoError(t, defaults.Set(&defaultOptions), "setting default options") | ||
|
||
subtests := []struct { | ||
name string | ||
opts config.EnvOptions | ||
expected Config | ||
error bool | ||
}{ | ||
{ | ||
name: "empty-missing-fields", | ||
opts: config.EnvOptions{}, | ||
error: true, | ||
}, | ||
{ | ||
name: "unknown-db-type", | ||
opts: config.EnvOptions{Environment: map[string]string{"TYPE": "☃"}}, | ||
error: true, | ||
}, | ||
{ | ||
name: "minimal-config", | ||
opts: config.EnvOptions{Environment: map[string]string{ | ||
"HOST": "db.example.com", | ||
"USER": "user", | ||
"DATABASE": "db", | ||
}}, | ||
expected: Config{ | ||
Type: "mysql", | ||
Host: "db.example.com", | ||
Database: "db", | ||
User: "user", | ||
Options: defaultOptions, | ||
}, | ||
}, | ||
{ | ||
name: "tls", | ||
opts: config.EnvOptions{Environment: map[string]string{ | ||
"HOST": "db.example.com", | ||
"USER": "user", | ||
"DATABASE": "db", | ||
"TLS": "true", | ||
"CERT": "/var/empty/db.crt", | ||
"CA": "/var/empty/ca.crt", | ||
}}, | ||
expected: Config{ | ||
Type: "mysql", | ||
Host: "db.example.com", | ||
Database: "db", | ||
User: "user", | ||
TlsOptions: config.TLS{ | ||
Enable: true, | ||
Cert: "/var/empty/db.crt", | ||
Ca: "/var/empty/ca.crt", | ||
}, | ||
Options: defaultOptions, | ||
}, | ||
}, | ||
{ | ||
name: "options", | ||
opts: config.EnvOptions{Environment: map[string]string{ | ||
"HOST": "db.example.com", | ||
"USER": "user", | ||
"DATABASE": "db", | ||
"OPTIONS_MAX_CONNECTIONS": "1", | ||
"OPTIONS_MAX_ROWS_PER_TRANSACTION": "65535", | ||
}}, | ||
expected: Config{ | ||
Type: "mysql", | ||
Host: "db.example.com", | ||
Database: "db", | ||
User: "user", | ||
Options: Options{ | ||
MaxConnections: 1, | ||
MaxConnectionsPerTable: 8, | ||
MaxPlaceholdersPerStatement: 8192, | ||
MaxRowsPerTransaction: 65535, | ||
WsrepSyncWait: 7, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range subtests { | ||
t.Run(test.name, func(t *testing.T) { | ||
var out Config | ||
if err := config.FromEnv(&out, test.opts); test.error { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, test.expected, out) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is the third time we are using this check I would move that to a utility function
validateNonNilPointer()
.