Add global config package.
This is a simple config system that I find myself wanting a lot.
This commit is contained in:
parent
6ae393ebf2
commit
ef0f14a512
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
ORDER=stringiformes
|
||||
SPECIES=strix aluco
|
Loading…
Reference in New Issue