Compare commits
No commits in common. "fcb2552e73245ce4d214c4644a95198bbea6a512" and "8dfd5378c087967dd66bbe3434a26251b213ba7a" have entirely different histories.
fcb2552e73
...
8dfd5378c0
@ -22,7 +22,6 @@ type cValue struct {
|
||||
durval time.Duration
|
||||
boolval bool
|
||||
strval string
|
||||
mapval map[string]cValue
|
||||
err error
|
||||
}
|
||||
|
||||
|
189
envconf.go
189
envconf.go
@ -15,14 +15,10 @@ type cEntry struct {
|
||||
dtype DataType
|
||||
unset bool
|
||||
empty bool
|
||||
defval string // The default value
|
||||
hasdef bool // Default value is defined
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
parsed bool
|
||||
env map[string]cEntry
|
||||
mapEnv map[string]map[string]cEntry
|
||||
}
|
||||
|
||||
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
|
||||
@ -32,21 +28,10 @@ func NewConfig() *Config {
|
||||
config := new(Config)
|
||||
config.parsed = false
|
||||
config.env = make(map[string]cEntry)
|
||||
config.mapEnv = make(map[string]map[string]cEntry)
|
||||
|
||||
for _, v := range os.Environ() {
|
||||
splitted := strings.SplitN(v, "=", 2)
|
||||
if len(splitted) == 2 {
|
||||
key := cleanKey(splitted[0])
|
||||
keysplit := strings.Split(key, "_")
|
||||
left := ""
|
||||
right := ""
|
||||
|
||||
if len(keysplit) > 1 {
|
||||
left = strings.Join(keysplit[:len(keysplit)-1], "_")
|
||||
right = keysplit[len(keysplit)-1]
|
||||
}
|
||||
|
||||
key := strings.TrimSpace(strings.ToUpper(splitted[0]))
|
||||
if unicode.IsLetter(getFirstRune(key)) {
|
||||
var entry cEntry
|
||||
entry.value = splitted[1]
|
||||
@ -54,14 +39,13 @@ func NewConfig() *Config {
|
||||
entry.unset = false
|
||||
entry.empty = false
|
||||
config.env[key] = entry
|
||||
config.mapEnv[left][right] = entry
|
||||
}
|
||||
}
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
// Define the type of an environment variable.
|
||||
// Define defines the type of an environment variable.
|
||||
// Variables without a defined type will be ignored by Parse.
|
||||
func (c *Config) Define(key string, dtype DataType) {
|
||||
upper := strings.ToUpper(key)
|
||||
@ -78,20 +62,7 @@ func (c *Config) Define(key string, dtype DataType) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
upper := strings.ToUpper(key)
|
||||
entries, ok := c.mapEnv[upper]
|
||||
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.
|
||||
// DefineDefault defines the type and default value of an environment variable.
|
||||
// Variables without a defined type will be ignored by Parse.
|
||||
func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
||||
upper := strings.ToUpper(key)
|
||||
@ -102,8 +73,6 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
||||
}
|
||||
entry.dtype = dtype
|
||||
entry.empty = false
|
||||
entry.defval = val
|
||||
entry.hasdef = true
|
||||
c.env[upper] = entry
|
||||
} else {
|
||||
var entry cEntry
|
||||
@ -111,8 +80,6 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
||||
entry.unset = true
|
||||
entry.empty = false
|
||||
entry.value = val
|
||||
entry.defval = val
|
||||
entry.hasdef = true
|
||||
c.env[upper] = entry
|
||||
}
|
||||
}
|
||||
@ -135,14 +102,6 @@ func (c *Config) Parse() {
|
||||
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 {
|
||||
for k, v := range c.env {
|
||||
if (v.parsed.err == nil) && v.unset {
|
||||
@ -158,64 +117,27 @@ func (c *Config) Parse() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) help() {
|
||||
max := make([]int, 2, 2)
|
||||
preq := false
|
||||
pdef := false
|
||||
func (c *Config) Help() {
|
||||
max := 0
|
||||
for k, v := range c.env {
|
||||
if v.dtype != TypeNone {
|
||||
if len(k) > max[0] {
|
||||
max[0] = len(k)
|
||||
if len(k) > max {
|
||||
max = len(k)
|
||||
}
|
||||
if len(v.dtype.String()) > max[1] {
|
||||
max[1] = len(v.dtype.String())
|
||||
if len(v.dtype.String()) > max {
|
||||
max = len(v.dtype.String())
|
||||
}
|
||||
if v.hasdef {
|
||||
pdef = true
|
||||
} else {
|
||||
preq = true
|
||||
if len(v.value) > max {
|
||||
max = len(v.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
if pdef {
|
||||
fmt.Println()
|
||||
for k, v := range c.env {
|
||||
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))
|
||||
format := fmt.Sprintf("Variable %%%ds|Type %%%ds|Default %%s\n", max, max)
|
||||
fmt.Printf(format, k, v.dtype, v.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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.
|
||||
@ -234,17 +156,6 @@ 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 {
|
||||
fmt.Fprintln(os.Stderr, "")
|
||||
for _, v := range c.env {
|
||||
@ -258,27 +169,6 @@ func (c *Config) Status() (ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func cleanKey(str string) string {
|
||||
fn := func(r rune) rune {
|
||||
if (r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || r == '_' {
|
||||
return r
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
str = strings.Map(fn, strings.ToUpper(str))
|
||||
|
||||
oldlen := len(str) + 1
|
||||
newlen := len(str)
|
||||
for newlen < oldlen {
|
||||
oldlen = newlen
|
||||
str = strings.ReplaceAll(str, "__", "_")
|
||||
newlen = len(str)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
|
||||
val.dtype = TypeNone
|
||||
if c.parsed {
|
||||
@ -291,27 +181,6 @@ func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
|
||||
return
|
||||
}
|
||||
|
||||
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 {
|
||||
retval[k] = v.parsed
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
return retval
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetInt returns the value of an environment variable.
|
||||
// If the variable is not defined as envconf.TypeInt the function will return 0.
|
||||
func (c *Config) GetInt(key string) int64 {
|
||||
@ -358,38 +227,6 @@ func (c *Config) GetBool(key string) bool {
|
||||
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 getFirstRune(str string) rune {
|
||||
for _, v := range str {
|
||||
return v
|
||||
|
Loading…
Reference in New Issue
Block a user