From ef0f14a51291fa6c0c1e8b8321ca7c07d446ba8e Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 2 Mar 2020 17:11:55 -0800 Subject: [PATCH] Add global config package. This is a simple config system that I find myself wanting a lot. --- config/config.go | 84 ++++++++++++++++++++++++++++++++++++++++ config/config_test.go | 66 +++++++++++++++++++++++++++++++ config/testdata/test.env | 2 + 3 files changed, 152 insertions(+) create mode 100644 config/config.go create mode 100644 config/config_test.go create mode 100644 config/testdata/test.env diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..dd4596a --- /dev/null +++ b/config/config.go @@ -0,0 +1,84 @@ +// Package config implements a simple global configuration system that +// supports a file with key=value pairs and environment variables. Note +// that the config system is global. +package config + +import ( + "bufio" + "log" + "os" + "strings" +) + +// NB: Rather than define a singleton type, everything is defined at +// the top-level + +var ( + vars = map[string]string{} + prefix = "" +) + +// SetEnvPrefix sets the prefix for all environment variables; it's +// assumed to not be needed for files. +func SetEnvPrefix(pfx string) { + prefix = pfx +} + +func addLine(line string) { + if strings.HasPrefix(line, "#") || line == "" { + return + } + + lineParts := strings.SplitN(line, "=", 2) + if len(lineParts) != 2 { + log.Print("skipping line: ", line) + return // silently ignore empty keys + } + + lineParts[0] = strings.TrimSpace(lineParts[0]) + lineParts[1] = strings.TrimSpace(lineParts[1]) + vars[lineParts[0]] = lineParts[1] +} + +// LoadFile scans the file at path for key=value pairs and adds them +// to the configuration. +func LoadFile(path string) error { + file, err := os.Open(path) + if err != nil { + return err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + addLine(line) + } + + if err = scanner.Err(); err != nil { + return err + } + + return nil +} + +// Get retrieves a value from either a configuration file or the +// environment. Note that values from a file will override environment +// variables. +func Get(key string) string { + if v, ok := vars[key]; ok { + return v + } + return os.Getenv(prefix + key) +} + +// GetDefault retrieves a value from either a configuration file or +// the environment. Note that value from a file will override +// environment variables. If a value isn't found (e.g. Get returns an +// empty string), the default value will be used. +func GetDefault(key, def string) string { + if v := Get(key); v != "" { + return v + } + return def +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..6d40c99 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,66 @@ +package config + +import ( + "os" + "testing" +) + +const ( + testFilePath = "testdata/test.env" + + // Keys + kOrder = "ORDER" + kSpecies = "SPECIES" + kName = "COMMON_NAME" + + // Env + eOrder = "corvus" + eSpecies = "corvus corax" + eName = "northern raven" + + // File + fOrder = "stringiformes" + fSpecies = "strix aluco" + // Name isn't set in the file to test fall through. +) + +func init() { + os.Setenv(kOrder, eOrder) + os.Setenv(kSpecies, eSpecies) + os.Setenv(kName, eName) +} + +func TestLoadEnvOnly(t *testing.T) { + order := Get(kOrder) + species := Get(kSpecies) + if order != eOrder { + t.Errorf("want %s, have %s", eOrder, order) + } + + if species != eSpecies { + t.Errorf("want %s, have %s", eSpecies, species) + } +} + +func TestLoadFile(t *testing.T) { + err := LoadFile(testFilePath) + if err != nil { + t.Fatal(err) + } + + order := Get(kOrder) + species := Get(kSpecies) + name := Get(kName) + + if order != fOrder { + t.Errorf("want %s, have %s", fOrder, order) + } + + if species != fSpecies { + t.Errorf("want %s, have %s", fSpecies, species) + } + + if name != eName { + t.Errorf("want %s, have %s", eName, name) + } +} diff --git a/config/testdata/test.env b/config/testdata/test.env new file mode 100644 index 0000000..5d66691 --- /dev/null +++ b/config/testdata/test.env @@ -0,0 +1,2 @@ +ORDER=stringiformes +SPECIES=strix aluco