2018-10-19 16:27:05 +02:00
|
|
|
package envconst
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2018-12-30 20:43:51 +01:00
|
|
|
"strconv"
|
2018-10-19 16:27:05 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cache sync.Map
|
|
|
|
|
|
|
|
func Duration(varname string, def time.Duration) time.Duration {
|
|
|
|
if v, ok := cache.Load(varname); ok {
|
|
|
|
return v.(time.Duration)
|
|
|
|
}
|
|
|
|
e := os.Getenv(varname)
|
|
|
|
if e == "" {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
d, err := time.ParseDuration(e)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cache.Store(varname, d)
|
|
|
|
return d
|
|
|
|
}
|
2018-12-30 20:43:51 +01:00
|
|
|
|
|
|
|
func Int64(varname string, def int64) int64 {
|
|
|
|
if v, ok := cache.Load(varname); ok {
|
|
|
|
return v.(int64)
|
|
|
|
}
|
|
|
|
e := os.Getenv(varname)
|
|
|
|
if e == "" {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
d, err := strconv.ParseInt(e, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cache.Store(varname, d)
|
|
|
|
return d
|
|
|
|
}
|
2019-03-11 13:46:36 +01:00
|
|
|
|
|
|
|
func Bool(varname string, def bool) bool {
|
|
|
|
if v, ok := cache.Load(varname); ok {
|
|
|
|
return v.(bool)
|
|
|
|
}
|
|
|
|
e := os.Getenv(varname)
|
|
|
|
if e == "" {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
d, err := strconv.ParseBool(e)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cache.Store(varname, d)
|
|
|
|
return d
|
|
|
|
}
|
2019-09-07 19:50:57 +02:00
|
|
|
|
|
|
|
func String(varname string, def string) string {
|
|
|
|
if v, ok := cache.Load(varname); ok {
|
|
|
|
return v.(string)
|
|
|
|
}
|
|
|
|
e := os.Getenv(varname)
|
|
|
|
if e == "" {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
cache.Store(varname, e)
|
|
|
|
return e
|
|
|
|
}
|