✨ Parse env vars
This commit is contained in:
parent
25f5c37243
commit
85f666cbc2
5 changed files with 57 additions and 17 deletions
|
|
@ -9,6 +9,8 @@ import (
|
|||
|
||||
"codeberg.org/danjones000/my-log/tools"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/caarlos0/env/v10"
|
||||
mapst "github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
var ConfigPath string
|
||||
|
|
@ -28,7 +30,8 @@ func Load() (Config, error) {
|
|||
return c, err
|
||||
}
|
||||
}
|
||||
// @todo get environ
|
||||
env.Parse(&c)
|
||||
// @todo how to handle env on options so cli args aren't overwrittem?
|
||||
|
||||
l := ""
|
||||
for k, v := range Overrides {
|
||||
|
|
@ -51,3 +54,19 @@ func Load() (Config, error) {
|
|||
_, err = toml.Decode(l, &c)
|
||||
return c, err
|
||||
}
|
||||
|
||||
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
|
||||
mapst.Decode(o.Config, &s)
|
||||
env.Parse(&s)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import (
|
|||
func TestLoad(t *testing.T) {
|
||||
f, _ := os.CreateTemp("", "test")
|
||||
ConfigPath = f.Name()
|
||||
defer f.Close()
|
||||
fmt.Fprint(f, `[input]
|
||||
ext = "log"`)
|
||||
c, err := Load()
|
||||
|
|
@ -20,6 +21,15 @@ ext = "log"`)
|
|||
assert.Equal(t, "log", c.Input.Ext)
|
||||
}
|
||||
|
||||
func TestLoadBadFile(t *testing.T) {
|
||||
f, _ := os.CreateTemp("", "test")
|
||||
ConfigPath = f.Name()
|
||||
defer f.Close()
|
||||
fmt.Fprint(f, `{"not":"toml"}`)
|
||||
_, err := Load()
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestLoadIgnoreMissingFile(t *testing.T) {
|
||||
def, _ := DefaultConfig()
|
||||
ConfigPath = "/not/a/real/file"
|
||||
|
|
@ -27,3 +37,23 @@ func TestLoadIgnoreMissingFile(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
assert.Equal(t, def, c)
|
||||
}
|
||||
|
||||
func TestOverride(t *testing.T) {
|
||||
Overrides = map[string]string{
|
||||
"input.path": "/path/to/it",
|
||||
"input.ext": "~",
|
||||
}
|
||||
c, err := Load()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, Overrides["input.path"], c.Input.Path)
|
||||
assert.Equal(t, "txt", c.Input.Ext)
|
||||
}
|
||||
|
||||
func TestOverrideJson(t *testing.T) {
|
||||
Overrides = map[string]string{"input.ext": `{"a":"b"}`}
|
||||
c, err := Load()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "txt", c.Input.Ext)
|
||||
}
|
||||
|
||||
// @todo test time
|
||||
|
|
|
|||
|
|
@ -1,16 +1,14 @@
|
|||
package config
|
||||
|
||||
import mapst "github.com/mitchellh/mapstructure"
|
||||
|
||||
type Config struct {
|
||||
Input Input
|
||||
Outputs Outputs `toml:"output"`
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
Path string
|
||||
Recurse bool
|
||||
Ext string
|
||||
Path string `env:"LOG_PATH"`
|
||||
Recurse bool `env:"LOG_RECURSE"`
|
||||
Ext string `env:"LOG_EXT"`
|
||||
}
|
||||
|
||||
type Outputs map[string]Output
|
||||
|
|
@ -20,16 +18,6 @@ type Output struct {
|
|||
Config map[string]any
|
||||
}
|
||||
|
||||
func (oo Outputs) Stdout() (s Stdout, enabled bool) {
|
||||
o, ok := oo["stdout"]
|
||||
if !ok {
|
||||
return s, false
|
||||
}
|
||||
enabled = o.Enabled
|
||||
mapst.Decode(o.Config, &s)
|
||||
return
|
||||
}
|
||||
|
||||
type Stdout struct {
|
||||
Json bool
|
||||
Json bool `env:"LOG_STDOUT_JSON"`
|
||||
}
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -4,6 +4,7 @@ go 1.21.5
|
|||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2
|
||||
github.com/caarlos0/env/v10 v10.0.0
|
||||
github.com/mitchellh/mapstructure v1.5.0
|
||||
github.com/spf13/cobra v1.8.0
|
||||
github.com/stretchr/testify v1.8.4
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -1,5 +1,7 @@
|
|||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA=
|
||||
github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue