Config package for environment overloaded by flag configuration

master
Bel LaPointe 2018-10-07 23:05:35 -06:00
parent 74039efb66
commit f7ffe6c829
2 changed files with 46 additions and 0 deletions

30
config/config.go Normal file
View File

@ -0,0 +1,30 @@
package config
import (
"flag"
"os"
"strings"
)
const cdbpath = "DBPath"
type Config struct {
DBPath string
}
func New() *Config {
lookups := make(map[string]*string)
add(cdbpath, "./db", lookups)
flag.Parse()
return &Config{
DBPath: *lookups[cdbpath],
}
}
func add(key string, value string, lookups map[string]*string) {
env := os.Getenv(strings.ToUpper(key))
if env != "" {
value = env
}
lookups[key] = flag.String(key, value, "")
}

16
config/config_test.go Normal file
View File

@ -0,0 +1,16 @@
package config
import (
"os"
"testing"
)
func Test_Config(t *testing.T) {
was := os.Getenv("DBPATH")
defer os.Setenv("DBPATH", was)
os.Setenv("DBPATH", "filler")
c := New()
if c.DBPath != "filler" {
t.Errorf("DBPATH not overridden by env")
}
}