envconf/envconf.go

187 lines
5.3 KiB
Go
Raw Normal View History

2021-03-23 09:13:25 +00:00
package envconf
2021-03-23 08:48:02 +00:00
2021-03-23 09:28:16 +00:00
import ("strings"
2021-03-23 10:08:26 +00:00
"unicode"
2021-03-23 11:57:09 +00:00
"errors"
2021-03-23 13:37:00 +00:00
"time"
2021-03-23 11:57:09 +00:00
"fmt"
2021-03-23 09:13:25 +00:00
"os")
2021-03-23 08:48:02 +00:00
2021-03-23 10:40:25 +00:00
type cEntry struct {
value string
2021-03-23 11:57:09 +00:00
parsed cValue
2021-03-23 10:40:25 +00:00
dtype DataType
2021-03-23 11:57:09 +00:00
unset bool
empty bool
2021-03-23 10:40:25 +00:00
}
2021-03-23 11:57:09 +00:00
type Config struct {
parsed bool
env map[string]cEntry
2021-03-23 10:08:26 +00:00
}
2021-03-26 11:06:39 +00:00
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
// The environment variables are stored in envconf.Config, so changes to the environment after NewConfig has been called
// will not be taken into account.
2021-03-23 09:13:25 +00:00
func NewConfig()(*Config) {
config := new(Config)
2021-03-23 11:57:09 +00:00
config.parsed = false
2021-03-23 10:40:25 +00:00
config.env = make(map[string]cEntry)
2021-03-23 09:13:25 +00:00
for _,v := range os.Environ() {
splitted := strings.SplitN(v, "=", 2)
if len(splitted) == 2 {
2021-03-23 18:44:53 +00:00
key := strings.TrimSpace(strings.ToUpper(splitted[0]))
2021-03-23 10:08:26 +00:00
if unicode.IsLetter(getFirstRune(key)) {
2021-03-23 10:40:25 +00:00
var entry cEntry
entry.value = splitted[1]
entry.dtype = TypeNone
2021-03-23 11:57:09 +00:00
entry.unset = false
entry.empty = false
2021-03-23 10:40:25 +00:00
config.env[key] = entry
2021-03-23 10:08:26 +00:00
}
2021-03-23 08:48:02 +00:00
}
}
2021-03-23 09:26:51 +00:00
return config
2021-03-23 08:48:02 +00:00
}
2021-03-26 11:06:39 +00:00
// Define defines the type of an environment variable.
// Variables without a defined type will be ignored by Parse.
2021-03-23 10:40:25 +00:00
func (c *Config) Define(key string, dtype DataType) {
2021-03-23 18:44:53 +00:00
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
2021-03-23 10:40:25 +00:00
if ok {
entry.dtype = dtype
2021-03-24 11:00:54 +00:00
c.env[upper] = entry
2021-03-23 10:40:25 +00:00
} else {
var entry cEntry
2021-03-23 11:57:09 +00:00
entry.dtype = dtype
entry.unset = true
entry.empty = true
2021-03-23 18:44:53 +00:00
c.env[upper] = entry
2021-03-23 10:40:25 +00:00
}
}
2021-03-23 11:57:09 +00:00
2021-03-26 11:06:39 +00:00
// DefineDefault defines the type and default value of an environment variable.
// Variables without a defined type will be ignored by Parse.
2021-03-23 11:57:09 +00:00
func (c *Config) DefineDefault(key string, val string, dtype DataType) {
2021-03-23 18:44:53 +00:00
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
2021-03-23 11:57:09 +00:00
if ok {
if entry.unset {
entry.value = val
}
2021-03-25 08:22:09 +00:00
entry.dtype = dtype
entry.empty = false
c.env[upper] = entry
2021-03-23 11:57:09 +00:00
} else {
var entry cEntry
entry.dtype = dtype
entry.unset = true
entry.empty = false
2021-03-24 15:16:34 +00:00
entry.value = val
2021-03-23 18:44:53 +00:00
c.env[upper] = entry
2021-03-23 11:57:09 +00:00
}
}
2021-03-26 11:06:39 +00:00
// Parse parses the environment variables previously defined by Define and DefineDefault.
// Parse should only be called once for a given envconf.Config.
2021-03-23 11:57:09 +00:00
func (c *Config) Parse() {
2021-03-25 13:32:58 +00:00
if c.parsed {
return
}
2021-03-23 12:05:27 +00:00
c.parsed = true
2021-03-23 11:57:09 +00:00
for k,v := range c.env {
2021-03-23 13:37:00 +00:00
if v.empty {
if v.unset {
v.parsed.err = errors.New(fmt.Sprintf(`Environment variable "%s" not found. It should have been of type %s.`, k, v.dtype))
2021-03-23 12:52:30 +00:00
c.env[k] = v
2021-03-23 11:57:09 +00:00
}
2021-03-23 13:37:00 +00:00
} else {
v.parsed = v.dtype.parse(k, v.value)
c.env[k] = v
2021-03-23 11:57:09 +00:00
}
}
}
2021-03-26 11:06:39 +00:00
// Status prints out failures that occured while parsing the environment to os.Stderr.
// Variables that have been defined without a default value and are
// missing from the environment will be considered a failure.
// If parsing of any of the variables has failed Status will return false.
2021-03-23 13:37:00 +00:00
func (c *Config) Status()(ok bool) {
ok = c.parsed
if ok {
for _,v := range c.env {
err := v.parsed.err
if err != nil {
ok = false
2021-03-26 11:28:50 +00:00
fmt.Fprintln(os.Stderr, err)
2021-03-23 13:37:00 +00:00
}
}
2021-03-23 12:05:27 +00:00
}
2021-03-23 11:57:09 +00:00
return
}
2021-03-25 13:18:26 +00:00
func (c *Config) getRaw(key string, dtype DataType)(val cValue) {
val.dtype = TypeNone
2021-03-23 13:37:00 +00:00
if c.parsed {
2021-03-23 18:44:53 +00:00
upper := strings.ToUpper(key)
entry, ok := c.env[upper]
2021-03-25 13:18:26 +00:00
if ok && (entry.dtype == dtype) {
return entry.parsed
2021-03-23 13:37:00 +00:00
}
}
2021-03-25 13:18:26 +00:00
return
}
2021-03-26 11:06:39 +00:00
// GetInt returns the value of an environment variable.
// If the variable is not defined as envconf.TypeInt the function will return 0.
2021-03-25 13:18:26 +00:00
func (c *Config) GetInt(key string)(int64) {
val := c.getRaw(key, TypeInt)
return val.intval
2021-03-23 13:37:00 +00:00
}
2021-03-26 11:06:39 +00:00
// GetMetric returns the value of an environment variable.
// If the variable is not defined as envconf.TypeMetric the function will return 0.
2021-03-25 11:06:08 +00:00
func (c *Config) GetMetric(key string)(int64) {
2021-03-25 13:18:26 +00:00
val := c.getRaw(key, TypeMetric)
return val.intval
2021-03-25 11:06:08 +00:00
}
2021-03-26 11:06:39 +00:00
// GetDirectory returns the value of an environment variable.
// If the variable is not defined as envconf.TypeDirectory the
// function will return the empty string.
2021-03-24 14:15:37 +00:00
func (c *Config) GetDirectory(key string)(string) {
2021-03-25 13:18:26 +00:00
val := c.getRaw(key, TypeDirectory)
return val.strval
2021-03-24 14:15:37 +00:00
}
2021-03-26 11:06:39 +00:00
// GetString returns the value of an environment variable.
// If the variable is not defined as envconf.TypeString the
// function will return the empty string.
2021-03-23 13:37:00 +00:00
func (c *Config) GetString(key string)(string) {
2021-03-25 13:18:26 +00:00
val := c.getRaw(key, TypeString)
return val.strval
2021-03-23 13:37:00 +00:00
}
2021-03-26 11:06:39 +00:00
// GetDuration returns the value of an environment variable.
// If the variable is not defined as envconf.TypeDuration the
// function will return time.Duration(0).
2021-03-23 13:37:00 +00:00
func (c *Config) GetDuration(key string)(time.Duration) {
2021-03-25 13:18:26 +00:00
val := c.getRaw(key, TypeDuration)
return val.durval
2021-03-23 13:37:00 +00:00
}
2021-03-26 11:06:39 +00:00
// GetBool returns the value of an environment variable.
// If the variable is not defined as envconf.TypeBool the
// function will return false.
2021-03-23 13:37:00 +00:00
func (c *Config) GetBool(key string)(bool) {
2021-03-25 13:18:26 +00:00
val := c.getRaw(key, TypeBool)
return val.boolval
2021-03-23 11:57:09 +00:00
}
func getFirstRune(str string)(rune) {
for _,v := range str {
return v
}
return rune(0)
}