🐛 Load config env before paraing cli

This commit is contained in:
Dan Jones 2024-02-10 11:48:23 -06:00
commit a6f3cf39a0
3 changed files with 37 additions and 8 deletions

View file

@ -31,7 +31,7 @@ func Load() (Config, error) {
}
}
env.Parse(&c)
// @todo how to handle env on options so cli args aren't overwrittem?
c.Outputs["stdout"] = loadStdout(c.Outputs["stdout"])
l := ""
for k, v := range Overrides {
@ -55,18 +55,25 @@ func Load() (Config, error) {
return c, err
}
func loadStdout(stdout Output) Output {
st := stdoutEnabled{stdout.Enabled}
env.Parse(&st)
stdout.Enabled = st.Enabled
var std Stdout
mapst.Decode(stdout.Config, &std)
env.Parse(&std)
mapst.Decode(std, &stdout.Config)
return stdout
}
func (oo Outputs) Stdout() (s Stdout, enabled bool) {
o, ok := oo["stdout"]
if !ok {
return s, false
}
st := struct {
Enabled bool `env:"LOG_STDOUT_ENABLED"`
}{o.Enabled}
env.Parse(&st)
enabled = st.Enabled
enabled = o.Enabled
mapst.Decode(o.Config, &s)
env.Parse(&s)
return
}

View file

@ -57,3 +57,21 @@ func TestOverrideJson(t *testing.T) {
}
// @todo test time
func TestStdoutMissing(t *testing.T) {
var oo Outputs = map[string]Output{}
std, en := oo.Stdout()
assert.False(t, en)
assert.Equal(t, Stdout{}, std)
}
func TestStdoutLoad(t *testing.T) {
os.Setenv("LOG_STDOUT_JSON", "true")
defer os.Unsetenv("LOG_STDOUT_JSON")
os.Setenv("LOG_STDOUT_ENABLED", "true")
defer os.Unsetenv("LOG_STDOUT_ENABLED")
c, _ := Load()
std, en := c.Outputs.Stdout()
assert.True(t, en)
assert.True(t, std.Json)
}

View file

@ -19,5 +19,9 @@ type Output struct {
}
type Stdout struct {
Json bool `env:"LOG_STDOUT_JSON"`
Json bool `env:"LOG_STDOUT_JSON" mapstructure:"json"`
}
type stdoutEnabled struct {
Enabled bool `env:"LOG_STDOUT_ENABLED"`
}