diff --git a/config/config.go b/config/config.go index bcd93128..2cd0f2a1 100644 --- a/config/config.go +++ b/config/config.go @@ -15,7 +15,7 @@ import ( "github.com/mitchellh/mapstructure" ) -const ( +var ( localConfigName = ".evans.toml" ) @@ -110,9 +110,14 @@ func init() { } } +func setupConfig(c *Config) { + c.REPL.Server = c.Server + c.Env.Server = c.Server +} + func Get() *Config { - var config Config - err := mapstructure.Decode(mConfig.Get(), &config) + var global Config + err := mapstructure.Decode(mConfig.Get(), &global) if err != nil { panic(err) } @@ -124,19 +129,17 @@ func Get() *Config { // if local config missing, return global config if local == nil { - return &config + setupConfig(&global) + return &global } - ic, err := mapstruct.Map(&config, local) + ic, err := mapstruct.Map(&global, local) if err != nil { panic(err) } c := ic.(*Config) - - // TODO: use more better method - c.REPL.Server = c.Server - c.Env.Server = c.Server + setupConfig(c) return c } diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 00000000..b96d9abf --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,35 @@ +package config + +import ( + "io/ioutil" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestConfig_Get(t *testing.T) { + checkValues := func() { + c := Get() + _ = c.REPL.Server.Host + _ = c.Env.Server.Host + } + + t.Run("no local config", func(t *testing.T) { + checkValues() + }) + + t.Run("has local config", func(t *testing.T) { + f, err := ioutil.TempFile("", "") + require.NoError(t, err) + defer f.Close() + + backup := localConfigName + defer func() { + localConfigName = backup + }() + + localConfigName = f.Name() + + checkValues() + }) +} diff --git a/meta/meta.go b/meta/meta.go index ad343f76..ced9bec2 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -2,5 +2,5 @@ package meta const ( Name = "evans" - Version = "0.2.6" + Version = "0.2.7" )