kos/util/env/env.go

63 lines
1.0 KiB
Go

package env
import (
"os"
"strconv"
"strings"
)
func Get(name string, val string) string {
value := strings.TrimSpace(os.Getenv(name))
if value == "" {
return val
} else {
return value
}
}
func Getter(val string, names ...string) string {
var (
value string
)
for _, name := range names {
if value = strings.TrimSpace(os.Getenv(name)); value != "" {
return value
}
}
return val
}
func Int(name string, val int64) int64 {
value := Get(name, "")
if n, err := strconv.ParseInt(value, 10, 64); err == nil {
return n
} else {
return val
}
}
func Integer(val int64, names ...string) int64 {
value := Getter("", names...)
if n, err := strconv.ParseInt(value, 10, 64); err == nil {
return n
} else {
return val
}
}
func Float(name string, val float64) float64 {
value := Get(name, "")
if n, err := strconv.ParseFloat(value, 64); err == nil {
return n
} else {
return val
}
}
func Set(name string, val string) {
value := os.Getenv(name)
if value == "" {
os.Setenv(name, val)
}
}