17 Commits

Author SHA1 Message Date
3d2005a8a2 add sized hex 2022-01-16 21:48:29 +01:00
1803501272 fix nil slice 2022-01-16 18:29:42 +01:00
d1bcc8c4cc add hex type 2022-01-16 18:24:24 +01:00
afa50b4fca remove invalid map 2022-01-15 21:35:37 +01:00
09acd79fe8 remove regex lookaround 2022-01-15 19:57:38 +01:00
0f1586393b fix regex 2022-01-15 19:54:59 +01:00
0d49b3e716 fix regex 2022-01-15 19:39:51 +01:00
f27c08ef84 rewrite cleanKey 2022-01-15 19:36:59 +01:00
712f3b65cf fix nil map 2022-01-15 18:52:45 +01:00
7b77ef35af fix nil map 2022-01-15 18:36:22 +01:00
fcb2552e73 add env map 2022-01-15 17:55:29 +01:00
a436aff825 use fmt for format 2021-06-18 15:20:38 +02:00
7c34548921 quote values 2021-06-18 15:17:55 +02:00
c7e42dc9c0 add newlines 2021-06-18 15:07:31 +02:00
7d9f8195b7 remove unused variable 2021-06-18 15:00:19 +02:00
4d89362709 add status help 2021-06-18 14:56:49 +02:00
cf1331e3cd align help 2021-06-18 14:29:18 +02:00
4 changed files with 355 additions and 32 deletions

View File

@@ -14,6 +14,11 @@ const (
TypeString DataType = iota TypeString DataType = iota
TypeDirectory DataType = iota TypeDirectory DataType = iota
TypeBool DataType = iota TypeBool DataType = iota
TypeHex DataType = iota
TypeHex16 DataType = iota
TypeHex32 DataType = iota
TypeHex64 DataType = iota
TypeHex128 DataType = iota
) )
type cValue struct { type cValue struct {
@@ -21,6 +26,7 @@ type cValue struct {
intval int64 intval int64
durval time.Duration durval time.Duration
boolval bool boolval bool
binval []byte
strval string strval string
err error err error
} }
@@ -28,7 +34,10 @@ type cValue struct {
func (dtype DataType) parse(key string, str string) (ret cValue) { func (dtype DataType) parse(key string, str string) (ret cValue) {
info, ok := tInfo[dtype] info, ok := tInfo[dtype]
if ok { if ok {
return info.parser(key, str) ret = info.parser(key, str)
if len(ret.binval) == 0 {
ret.binval = make([]byte, 0, 0)
}
} }
return return
} }

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"regexp"
"strings" "strings"
"time" "time"
"unicode" "unicode"
@@ -15,10 +16,14 @@ type cEntry struct {
dtype DataType dtype DataType
unset bool unset bool
empty bool empty bool
defval string // The default value
hasdef bool // Default value is defined
} }
type Config struct { type Config struct {
parsed bool parsed bool
env map[string]cEntry env map[string]cEntry
mapEnv map[string]map[string]cEntry
} }
// NewConfig returns an envconf.Config that is used to read configuration from environment variables. // NewConfig returns an envconf.Config that is used to read configuration from environment variables.
@@ -28,10 +33,14 @@ func NewConfig() *Config {
config := new(Config) config := new(Config)
config.parsed = false config.parsed = false
config.env = make(map[string]cEntry) config.env = make(map[string]cEntry)
config.mapEnv = make(map[string]map[string]cEntry)
for _, v := range os.Environ() { for _, v := range os.Environ() {
splitted := strings.SplitN(v, "=", 2) splitted := strings.SplitN(v, "=", 2)
if len(splitted) == 2 { if len(splitted) == 2 {
key := strings.TrimSpace(strings.ToUpper(splitted[0])) key := cleanKey(splitted[0])
left, right, mappable := keySplit(key)
if unicode.IsLetter(getFirstRune(key)) { if unicode.IsLetter(getFirstRune(key)) {
var entry cEntry var entry cEntry
entry.value = splitted[1] entry.value = splitted[1]
@@ -39,48 +48,72 @@ func NewConfig() *Config {
entry.unset = false entry.unset = false
entry.empty = false entry.empty = false
config.env[key] = entry config.env[key] = entry
if mappable {
if len(config.mapEnv[left]) == 0 {
config.mapEnv[left] = make(map[string]cEntry)
}
config.mapEnv[left][right] = entry
}
} }
} }
} }
return config return config
} }
// Define defines the type of an environment variable. // Define the type of an environment variable.
// Variables without a defined type will be ignored by Parse. // Variables without a defined type will be ignored by Parse.
func (c *Config) Define(key string, dtype DataType) { func (c *Config) Define(key string, dtype DataType) {
upper := strings.ToUpper(key) key = cleanKey(key)
entry, ok := c.env[upper] entry, ok := c.env[key]
if ok { if ok {
entry.dtype = dtype entry.dtype = dtype
c.env[upper] = entry c.env[key] = entry
} else { } else {
var entry cEntry var entry cEntry
entry.dtype = dtype entry.dtype = dtype
entry.unset = true entry.unset = true
entry.empty = true entry.empty = true
c.env[upper] = entry c.env[key] = entry
} }
} }
// DefineDefault defines the type and default value of an environment variable. // Define the type of an environment variable.
// Variables without a defined type will be ignored by Parse.
func (c *Config) DefineMap(key string, dtype DataType) {
key = cleanKey(key)
entries, ok := c.mapEnv[key]
if ok {
for mapKey, entry := range entries {
entry.dtype = dtype
c.mapEnv[key][mapKey] = entry
}
}
}
// Define the type and default value of an environment variable.
// Variables without a defined type will be ignored by Parse. // Variables without a defined type will be ignored by Parse.
func (c *Config) DefineDefault(key string, val string, dtype DataType) { func (c *Config) DefineDefault(key string, val string, dtype DataType) {
upper := strings.ToUpper(key) key = cleanKey(key)
entry, ok := c.env[upper] entry, ok := c.env[key]
if ok { if ok {
if entry.unset { if entry.unset {
entry.value = val entry.value = val
} }
entry.dtype = dtype entry.dtype = dtype
entry.empty = false entry.empty = false
c.env[upper] = entry entry.defval = val
entry.hasdef = true
c.env[key] = entry
} else { } else {
var entry cEntry var entry cEntry
entry.dtype = dtype entry.dtype = dtype
entry.unset = true entry.unset = true
entry.empty = false entry.empty = false
entry.value = val entry.value = val
c.env[upper] = entry entry.defval = val
entry.hasdef = true
c.env[key] = entry
} }
} }
@@ -102,6 +135,14 @@ func (c *Config) Parse() {
c.env[k] = v c.env[k] = v
} }
} }
for k, v := range c.mapEnv {
for mk, mv := range v {
mv.parsed = mv.dtype.parse(k+"_"+mk, mv.value)
c.mapEnv[k][mk] = mv
}
}
if failed { if failed {
for k, v := range c.env { for k, v := range c.env {
if (v.parsed.err == nil) && v.unset { if (v.parsed.err == nil) && v.unset {
@@ -117,27 +158,64 @@ func (c *Config) Parse() {
} }
} }
func (c *Config) Help() { func (c *Config) help() {
max := 0 max := make([]int, 2, 2)
preq := false
pdef := false
for k, v := range c.env { for k, v := range c.env {
if v.dtype != TypeNone { if v.dtype != TypeNone {
if len(k) > max { if len(k) > max[0] {
max = len(k) max[0] = len(k)
} }
if len(v.dtype.String()) > max { if len(v.dtype.String()) > max[1] {
max = len(v.dtype.String()) max[1] = len(v.dtype.String())
} }
if len(v.value) > max { if v.hasdef {
max = len(v.value) pdef = true
} else {
preq = true
} }
} }
} }
for k, v := range c.env { if pdef {
if v.dtype != TypeNone { fmt.Println()
format := fmt.Sprintf("Variable %%%ds|Type %%%ds|Default %%s\n", max, max) for k, v := range c.env {
fmt.Printf(format, k, v.dtype, v.value) if v.dtype != TypeNone {
if v.hasdef {
format := fmt.Sprintf("Variable %%-%ds| Type %%-%ds| Default %%s\n", max[0]+5, max[1]+3)
fmt.Printf(format, fmt.Sprintf(`"%s"`, k), v.dtype, fmt.Sprintf(`"%s"`, v.defval))
}
}
} }
} }
if preq {
fmt.Println()
for k, v := range c.env {
if v.dtype != TypeNone {
if !v.hasdef {
format := fmt.Sprintf("Variable %%-%ds| Type %%-%ds| Required\n", max[0]+5, max[1]+3)
fmt.Printf(format, fmt.Sprintf(`"%s"`, k), v.dtype)
}
}
}
}
fmt.Println()
}
func (c *Config) StatusHelp() bool {
flags := make(map[string]bool)
flags["--help"] = true
flags["-help"] = true
flags["-h"] = true
if len(os.Args) > 1 {
key := strings.ToLower(strings.TrimSpace(os.Args[1]))
_, ok := flags[key]
if ok {
c.help()
return false
}
}
return c.Status()
} }
// Status prints out failures that occured while parsing the environment to os.Stderr. // Status prints out failures that occured while parsing the environment to os.Stderr.
@@ -156,6 +234,17 @@ func (c *Config) Status() (ok bool) {
} }
} }
} }
for _, v := range c.mapEnv {
for _, mv := range v {
err := mv.parsed.err
if err != nil {
ok = false
if !mv.empty {
fmt.Fprintln(os.Stderr, err)
}
}
}
}
if !ok { if !ok {
fmt.Fprintln(os.Stderr, "") fmt.Fprintln(os.Stderr, "")
for _, v := range c.env { for _, v := range c.env {
@@ -169,11 +258,35 @@ func (c *Config) Status() (ok bool) {
return return
} }
func cleanKey(key string) string {
expr := regexp.MustCompile("__+")
fn := func(r rune) rune {
if (r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || r == '_' {
return r
}
return -1
}
key = strings.Trim(strings.Map(fn, strings.ToUpper(key)), "_")
return expr.ReplaceAllString(key, "_")
}
func keySplit(key string) (left string, right string, ok bool) {
key = cleanKey(key)
pos := strings.LastIndex(key, "_")
ok = false
if (pos+1 < len(key)) && (pos > 1) {
return cleanKey(key[:pos]), cleanKey(key[pos:]), true
}
return
}
func (c *Config) getRaw(key string, dtype DataType) (val cValue) { func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
val.dtype = TypeNone val.dtype = TypeNone
val.binval = make([]byte, 0, 0)
if c.parsed { if c.parsed {
upper := strings.ToUpper(key) key = cleanKey(key)
entry, ok := c.env[upper] entry, ok := c.env[key]
if ok && (entry.dtype == dtype) { if ok && (entry.dtype == dtype) {
return entry.parsed return entry.parsed
} }
@@ -181,21 +294,77 @@ func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
return return
} }
// GetInt returns the value of an environment variable. func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue) {
empty = make(map[string]cValue)
retval := make(map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapEnv[key]
if ok {
for k, v := range entries {
if (v.dtype == dtype) && (v.parsed.err == nil) {
retval[k] = v.parsed
} else {
return
}
}
return retval
}
}
return
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeInt the function will return 0. // If the variable is not defined as envconf.TypeInt the function will return 0.
func (c *Config) GetInt(key string) int64 { func (c *Config) GetInt(key string) int64 {
val := c.getRaw(key, TypeInt) val := c.getRaw(key, TypeInt)
return val.intval return val.intval
} }
// GetMetric returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex the function will return []byte{}.
func (c *Config) GetHex(key string) []byte {
val := c.getRaw(key, TypeHex)
return val.binval
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex16 the function will return []byte{}.
func (c *Config) GetHex16(key string) []byte {
val := c.getRaw(key, TypeHex16)
return val.binval
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex32 the function will return []byte{}.
func (c *Config) GetHex32(key string) []byte {
val := c.getRaw(key, TypeHex32)
return val.binval
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex64 the function will return []byte{}.
func (c *Config) GetHex64(key string) []byte {
val := c.getRaw(key, TypeHex64)
return val.binval
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex128 the function will return []byte{}.
func (c *Config) GetHex128(key string) []byte {
val := c.getRaw(key, TypeHex128)
return val.binval
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeMetric the function will return 0. // If the variable is not defined as envconf.TypeMetric the function will return 0.
func (c *Config) GetMetric(key string) int64 { func (c *Config) GetMetric(key string) int64 {
val := c.getRaw(key, TypeMetric) val := c.getRaw(key, TypeMetric)
return val.intval return val.intval
} }
// GetDirectory returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeDirectory the // If the variable is not defined as envconf.TypeDirectory the
// function will return the empty string. // function will return the empty string.
func (c *Config) GetDirectory(key string) string { func (c *Config) GetDirectory(key string) string {
@@ -203,7 +372,7 @@ func (c *Config) GetDirectory(key string) string {
return val.strval return val.strval
} }
// GetString returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeString the // If the variable is not defined as envconf.TypeString the
// function will return the empty string. // function will return the empty string.
func (c *Config) GetString(key string) string { func (c *Config) GetString(key string) string {
@@ -211,7 +380,7 @@ func (c *Config) GetString(key string) string {
return val.strval return val.strval
} }
// GetDuration returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeDuration the // If the variable is not defined as envconf.TypeDuration the
// function will return time.Duration(0). // function will return time.Duration(0).
func (c *Config) GetDuration(key string) time.Duration { func (c *Config) GetDuration(key string) time.Duration {
@@ -219,7 +388,7 @@ func (c *Config) GetDuration(key string) time.Duration {
return val.durval return val.durval
} }
// GetBool returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeBool the // If the variable is not defined as envconf.TypeBool the
// function will return false. // function will return false.
func (c *Config) GetBool(key string) bool { func (c *Config) GetBool(key string) bool {
@@ -227,6 +396,78 @@ func (c *Config) GetBool(key string) bool {
return val.boolval return val.boolval
} }
func (c *Config) GetMapInt(key string) (retval map[string]int64) {
retval = make(map[string]int64)
for k, v := range c.getRawMap(key, TypeInt) {
retval[k] = v.intval
}
return
}
func (c *Config) GetMapDuration(key string) (retval map[string]time.Duration) {
retval = make(map[string]time.Duration)
for k, v := range c.getRawMap(key, TypeDuration) {
retval[k] = v.durval
}
return
}
func (c *Config) GetMapString(key string) (retval map[string]string) {
retval = make(map[string]string)
for k, v := range c.getRawMap(key, TypeString) {
retval[k] = v.strval
}
return
}
func (c *Config) GetMapBool(key string) (retval map[string]bool) {
retval = make(map[string]bool)
for k, v := range c.getRawMap(key, TypeBool) {
retval[k] = v.boolval
}
return
}
func (c *Config) GetMapHex(key string) (retval map[string][]byte) {
retval = make(map[string][]byte)
for k, v := range c.getRawMap(key, TypeHex) {
retval[k] = v.binval
}
return
}
func (c *Config) GetMapHex16(key string) (retval map[string][]byte) {
retval = make(map[string][]byte)
for k, v := range c.getRawMap(key, TypeHex16) {
retval[k] = v.binval
}
return
}
func (c *Config) GetMapHex32(key string) (retval map[string][]byte) {
retval = make(map[string][]byte)
for k, v := range c.getRawMap(key, TypeHex32) {
retval[k] = v.binval
}
return
}
func (c *Config) GetMapHex64(key string) (retval map[string][]byte) {
retval = make(map[string][]byte)
for k, v := range c.getRawMap(key, TypeHex64) {
retval[k] = v.binval
}
return
}
func (c *Config) GetMapHex128(key string) (retval map[string][]byte) {
retval = make(map[string][]byte)
for k, v := range c.getRawMap(key, TypeHex128) {
retval[k] = v.binval
}
return
}
func getFirstRune(str string) rune { func getFirstRune(str string) rune {
for _, v := range str { for _, v := range str {
return v return v

View File

@@ -1,6 +1,7 @@
package envconf package envconf
import ( import (
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@@ -65,6 +66,56 @@ func parseBool(key string, str string) (ret cValue) {
return return
} }
func parseHex(key string, str string) (ret cValue) {
val, err := hex.DecodeString(str)
if err == nil {
ret.binval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex.`, key))
}
return
}
func parseHex16(key string, str string) (ret cValue) {
val, err := hex.DecodeString(str)
if err == nil && len(val) == 16 {
ret.binval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex16.`, key))
}
return
}
func parseHex32(key string, str string) (ret cValue) {
val, err := hex.DecodeString(str)
if err == nil && len(val) == 32 {
ret.binval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex32.`, key))
}
return
}
func parseHex64(key string, str string) (ret cValue) {
val, err := hex.DecodeString(str)
if err == nil && len(val) == 64 {
ret.binval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex64.`, key))
}
return
}
func parseHex128(key string, str string) (ret cValue) {
val, err := hex.DecodeString(str)
if err == nil && len(val) == 128 {
ret.binval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex128.`, key))
}
return
}
func parseDirectory(_ string, str string) (ret cValue) { func parseDirectory(_ string, str string) (ret cValue) {
wd, err := os.Getwd() wd, err := os.Getwd()
if err == nil { if err == nil {

View File

@@ -14,6 +14,13 @@ func init() {
var durInfo dataTypeInfo var durInfo dataTypeInfo
var strInfo dataTypeInfo var strInfo dataTypeInfo
var dirInfo dataTypeInfo var dirInfo dataTypeInfo
var hexInfo dataTypeInfo
var hexInfo16 dataTypeInfo
var hexInfo32 dataTypeInfo
var hexInfo64 dataTypeInfo
var hexInfo128 dataTypeInfo
var boolInfo dataTypeInfo var boolInfo dataTypeInfo
intInfo.name = "int" intInfo.name = "int"
@@ -21,6 +28,7 @@ func init() {
durInfo.name = "duration" durInfo.name = "duration"
dirInfo.name = "directory" dirInfo.name = "directory"
strInfo.name = "string" strInfo.name = "string"
hexInfo.name = "hex"
boolInfo.name = "bool" boolInfo.name = "bool"
intInfo.parser = parseInt intInfo.parser = parseInt
@@ -28,6 +36,13 @@ func init() {
durInfo.parser = parseDuration durInfo.parser = parseDuration
dirInfo.parser = parseDirectory dirInfo.parser = parseDirectory
strInfo.parser = parseString strInfo.parser = parseString
hexInfo.parser = parseHex
hexInfo16.parser = parseHex16
hexInfo32.parser = parseHex32
hexInfo64.parser = parseHex64
hexInfo128.parser = parseHex128
boolInfo.parser = parseBool boolInfo.parser = parseBool
tInfo[TypeInt] = intInfo tInfo[TypeInt] = intInfo
@@ -35,5 +50,12 @@ func init() {
tInfo[TypeDuration] = durInfo tInfo[TypeDuration] = durInfo
tInfo[TypeString] = strInfo tInfo[TypeString] = strInfo
tInfo[TypeDirectory] = dirInfo tInfo[TypeDirectory] = dirInfo
tInfo[TypeHex] = hexInfo
tInfo[TypeHex16] = hexInfo16
tInfo[TypeHex32] = hexInfo32
tInfo[TypeHex64] = hexInfo64
tInfo[TypeHex128] = hexInfo128
tInfo[TypeBool] = boolInfo tInfo[TypeBool] = boolInfo
} }