diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..eb65772 --- /dev/null +++ b/config/config.go @@ -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, "") +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..09cb756 --- /dev/null +++ b/config/config_test.go @@ -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") + } +}