Config package for environment overloaded by flag configuration
parent
74039efb66
commit
f7ffe6c829
|
|
@ -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, "")
|
||||
}
|
||||
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue