From e50bbc7d0ffe5c2feb612deb04679d69b6b99223 Mon Sep 17 00:00:00 2001 From: Evgenii Date: Wed, 27 Feb 2019 16:46:08 +0300 Subject: [PATCH] environment configuration (#15) * environment configuration * simplify * fix test * small fix --- helium.go | 6 ++++-- helium_test.go | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/helium.go b/helium.go index 6020a7c..064d951 100644 --- a/helium.go +++ b/helium.go @@ -6,6 +6,7 @@ import ( stdlog "log" "os" "reflect" + "strings" "github.com/im-kulikov/helium/logger" "github.com/im-kulikov/helium/module" @@ -47,12 +48,13 @@ func New(cfg *Settings, mod module.Module) (*Helium, error) { if cfg.Prefix == "" { cfg.Prefix = cfg.Name } + cfg.Prefix = strings.ToUpper(cfg.Prefix) - if tmp := os.Getenv("HELIUM_CONFIG"); tmp != "" { + if tmp := os.Getenv(cfg.Prefix + "_CONFIG"); tmp != "" { cfg.File = tmp } - if tmp := os.Getenv("HELIUM_CONFIG_TYPE"); tmp != "" { + if tmp := os.Getenv(cfg.Prefix + "_CONFIG_TYPE"); tmp != "" { cfg.Type = tmp } diff --git a/helium_test.go b/helium_test.go index aee4870..6320ceb 100644 --- a/helium_test.go +++ b/helium_test.go @@ -49,10 +49,12 @@ func TestHelium(t *testing.T) { defer os.Remove(tmpFile.Name()) // clean up - os.Setenv("HELIUM_CONFIG", tmpFile.Name()) - os.Setenv("HELIUM_CONFIG_TYPE", "toml") + os.Setenv("ABC_CONFIG", tmpFile.Name()) + os.Setenv("ABC_CONFIG_TYPE", "toml") - h, err := New(&Settings{}, module.Module{ + h, err := New(&Settings{ + Name: "Abc", + }, module.Module{ {Constructor: func(cfg *settings.Core) App { c.So(cfg.File, ShouldEqual, tmpFile.Name()) c.So(cfg.Type, ShouldEqual, "toml")