diff --git a/config/config.go b/config/config.go index dd4596a..235db3e 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,11 @@ // 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. +// +// This package is intended to be used for small daemons: some configuration +// file is optionally populated at program start, then this is used to +// transparently look up configuration values from either that file or the +// environment. package config import ( @@ -82,3 +87,24 @@ func GetDefault(key, def string) string { } return def } + +// Require retrieves a value from either a configuration file or the +// environment. If the key isn't present, it will call log.Fatal, printing +// the missing key. +func Require(key string) string { + if v, ok := vars[key]; ok { + return v + } + + v, ok := os.LookupEnv(prefix + key) + if !ok { + var envMessage string + if prefix != "" { + envMessage = " (note: looked for the key " + prefix + key + envMessage += " in the local env)" + } + log.Fatalf("missing required configuration value %s%s", key, envMessage) + } + + return v +}