2023-04-23 17:57:36 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:14:15 +08:00
|
|
|
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 {
|
2023-04-23 17:57:36 +08:00
|
|
|
value := Get(name, "")
|
|
|
|
if n, err := strconv.ParseInt(value, 10, 64); err == nil {
|
|
|
|
return n
|
|
|
|
} else {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 11:14:15 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 17:57:36 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|