Compare commits
	
		
			7 Commits
		
	
	
		
			8dfd5378c0
			...
			fcb2552e73
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						fcb2552e73
	
				 | 
					
					
						|||
| 
						
						
							
						
						a436aff825
	
				 | 
					
					
						|||
| 
						
						
							
						
						7c34548921
	
				 | 
					
					
						|||
| 
						
						
							
						
						c7e42dc9c0
	
				 | 
					
					
						|||
| 
						
						
							
						
						7d9f8195b7
	
				 | 
					
					
						|||
| 
						
						
							
						
						4d89362709
	
				 | 
					
					
						|||
| 
						
						
							
						
						cf1331e3cd
	
				 | 
					
					
						
@@ -22,6 +22,7 @@ type cValue struct {
 | 
			
		||||
	durval  time.Duration
 | 
			
		||||
	boolval bool
 | 
			
		||||
	strval  string
 | 
			
		||||
	mapval  map[string]cValue
 | 
			
		||||
	err     error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										189
									
								
								envconf.go
									
									
									
									
									
								
							
							
						
						
									
										189
									
								
								envconf.go
									
									
									
									
									
								
							@@ -15,10 +15,14 @@ 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.
 | 
			
		||||
@@ -28,10 +32,21 @@ 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 := strings.TrimSpace(strings.ToUpper(splitted[0]))
 | 
			
		||||
			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]
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if unicode.IsLetter(getFirstRune(key)) {
 | 
			
		||||
				var entry cEntry
 | 
			
		||||
				entry.value = splitted[1]
 | 
			
		||||
@@ -39,13 +54,14 @@ func NewConfig() *Config {
 | 
			
		||||
				entry.unset = false
 | 
			
		||||
				entry.empty = false
 | 
			
		||||
				config.env[key] = entry
 | 
			
		||||
				config.mapEnv[left][right] = entry
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	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.
 | 
			
		||||
func (c *Config) Define(key string, dtype DataType) {
 | 
			
		||||
	upper := strings.ToUpper(key)
 | 
			
		||||
@@ -62,7 +78,20 @@ func (c *Config) Define(key string, dtype DataType) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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) {
 | 
			
		||||
	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.
 | 
			
		||||
// Variables without a defined type will be ignored by Parse.
 | 
			
		||||
func (c *Config) DefineDefault(key string, val string, dtype DataType) {
 | 
			
		||||
	upper := strings.ToUpper(key)
 | 
			
		||||
@@ -73,6 +102,8 @@ 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
 | 
			
		||||
@@ -80,6 +111,8 @@ 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
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -102,6 +135,14 @@ 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 {
 | 
			
		||||
@@ -117,27 +158,64 @@ func (c *Config) Parse() {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Config) Help() {
 | 
			
		||||
	max := 0
 | 
			
		||||
func (c *Config) help() {
 | 
			
		||||
	max := make([]int, 2, 2)
 | 
			
		||||
	preq := false
 | 
			
		||||
	pdef := false
 | 
			
		||||
	for k, v := range c.env {
 | 
			
		||||
		if v.dtype != TypeNone {
 | 
			
		||||
			if len(k) > max {
 | 
			
		||||
				max = len(k)
 | 
			
		||||
			if len(k) > max[0] {
 | 
			
		||||
				max[0] = len(k)
 | 
			
		||||
			}
 | 
			
		||||
			if len(v.dtype.String()) > max {
 | 
			
		||||
				max = len(v.dtype.String())
 | 
			
		||||
			if len(v.dtype.String()) > max[1] {
 | 
			
		||||
				max[1] = len(v.dtype.String())
 | 
			
		||||
			}
 | 
			
		||||
			if len(v.value) > max {
 | 
			
		||||
				max = len(v.value)
 | 
			
		||||
			if v.hasdef {
 | 
			
		||||
				pdef = true
 | 
			
		||||
			} else {
 | 
			
		||||
				preq = true
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if pdef {
 | 
			
		||||
		fmt.Println()
 | 
			
		||||
		for k, v := range c.env {
 | 
			
		||||
			if v.dtype != TypeNone {
 | 
			
		||||
			format := fmt.Sprintf("Variable %%%ds|Type %%%ds|Default %%s\n", max, max)
 | 
			
		||||
			fmt.Printf(format, k, v.dtype, v.value)
 | 
			
		||||
				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.
 | 
			
		||||
@@ -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 {
 | 
			
		||||
			fmt.Fprintln(os.Stderr, "")
 | 
			
		||||
			for _, v := range c.env {
 | 
			
		||||
@@ -169,6 +258,27 @@ 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 {
 | 
			
		||||
@@ -181,6 +291,27 @@ 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 {
 | 
			
		||||
@@ -227,6 +358,38 @@ 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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user