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
|
durval time.Duration
|
||||||
boolval bool
|
boolval bool
|
||||||
strval string
|
strval string
|
||||||
mapval map[string]cValue
|
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
189
envconf.go
189
envconf.go
@ -15,14 +15,10 @@ 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.
|
||||||
@ -32,21 +28,10 @@ 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 := cleanKey(splitted[0])
|
key := strings.TrimSpace(strings.ToUpper(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)) {
|
if unicode.IsLetter(getFirstRune(key)) {
|
||||||
var entry cEntry
|
var entry cEntry
|
||||||
entry.value = splitted[1]
|
entry.value = splitted[1]
|
||||||
@ -54,14 +39,13 @@ func NewConfig() *Config {
|
|||||||
entry.unset = false
|
entry.unset = false
|
||||||
entry.empty = false
|
entry.empty = false
|
||||||
config.env[key] = entry
|
config.env[key] = entry
|
||||||
config.mapEnv[left][right] = entry
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return config
|
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.
|
// 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)
|
upper := strings.ToUpper(key)
|
||||||
@ -78,20 +62,7 @@ func (c *Config) Define(key string, dtype DataType) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the type 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) 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.
|
// 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)
|
upper := strings.ToUpper(key)
|
||||||
@ -102,8 +73,6 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
|||||||
}
|
}
|
||||||
entry.dtype = dtype
|
entry.dtype = dtype
|
||||||
entry.empty = false
|
entry.empty = false
|
||||||
entry.defval = val
|
|
||||||
entry.hasdef = true
|
|
||||||
c.env[upper] = entry
|
c.env[upper] = entry
|
||||||
} else {
|
} else {
|
||||||
var entry cEntry
|
var entry cEntry
|
||||||
@ -111,8 +80,6 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
|
|||||||
entry.unset = true
|
entry.unset = true
|
||||||
entry.empty = false
|
entry.empty = false
|
||||||
entry.value = val
|
entry.value = val
|
||||||
entry.defval = val
|
|
||||||
entry.hasdef = true
|
|
||||||
c.env[upper] = entry
|
c.env[upper] = entry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -135,14 +102,6 @@ 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 {
|
||||||
@ -158,64 +117,27 @@ func (c *Config) Parse() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) help() {
|
func (c *Config) Help() {
|
||||||
max := make([]int, 2, 2)
|
max := 0
|
||||||
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[0] {
|
if len(k) > max {
|
||||||
max[0] = len(k)
|
max = len(k)
|
||||||
}
|
}
|
||||||
if len(v.dtype.String()) > max[1] {
|
if len(v.dtype.String()) > max {
|
||||||
max[1] = len(v.dtype.String())
|
max = len(v.dtype.String())
|
||||||
}
|
}
|
||||||
if v.hasdef {
|
if len(v.value) > max {
|
||||||
pdef = true
|
max = len(v.value)
|
||||||
} else {
|
|
||||||
preq = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if pdef {
|
|
||||||
fmt.Println()
|
|
||||||
for k, v := range c.env {
|
for k, v := range c.env {
|
||||||
if v.dtype != TypeNone {
|
if v.dtype != TypeNone {
|
||||||
if v.hasdef {
|
format := fmt.Sprintf("Variable %%%ds|Type %%%ds|Default %%s\n", max, max)
|
||||||
format := fmt.Sprintf("Variable %%-%ds| Type %%-%ds| Default %%s\n", max[0]+5, max[1]+3)
|
fmt.Printf(format, k, v.dtype, v.value)
|
||||||
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.
|
||||||
@ -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 {
|
if !ok {
|
||||||
fmt.Fprintln(os.Stderr, "")
|
fmt.Fprintln(os.Stderr, "")
|
||||||
for _, v := range c.env {
|
for _, v := range c.env {
|
||||||
@ -258,27 +169,6 @@ func (c *Config) Status() (ok bool) {
|
|||||||
return
|
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) {
|
func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
|
||||||
val.dtype = TypeNone
|
val.dtype = TypeNone
|
||||||
if c.parsed {
|
if c.parsed {
|
||||||
@ -291,27 +181,6 @@ func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
|
|||||||
return
|
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.
|
// GetInt 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 {
|
||||||
@ -358,38 +227,6 @@ 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 getFirstRune(str string) rune {
|
func getFirstRune(str string) rune {
|
||||||
for _, v := range str {
|
for _, v := range str {
|
||||||
return v
|
return v
|
||||||
|
Loading…
Reference in New Issue
Block a user