151 lines
3.1 KiB
Go
151 lines
3.1 KiB
Go
package envvars
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
func String(key, defaultValue string) string {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
return v
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Byte(key string, defaultValue byte) byte {
|
|
return Uint8(key, defaultValue)
|
|
}
|
|
|
|
func Rune(key string, defaultValue rune) rune {
|
|
for _, rn := range String(key, string(defaultValue)) {
|
|
return rn
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Int(key string, defaultValue int) int {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseInt(v, 10, 64); err != nil {
|
|
return int(v2)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Int8(key string, defaultValue int8) int8 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseInt(v, 10, 64); err != nil {
|
|
return int8(v2)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Int16(key string, defaultValue int16) int16 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseInt(v, 10, 64); err != nil {
|
|
return int16(v2)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Int32(key string, defaultValue int32) int32 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseInt(v, 10, 64); err != nil {
|
|
return int32(v2)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Int64(key string, defaultValue int64) int64 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseInt(v, 10, 64); err != nil {
|
|
return v2
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Uint8(key string, defaultValue uint8) uint8 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseUint(v, 10, 64); err != nil {
|
|
return uint8(v2)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Uint16(key string, defaultValue uint16) uint16 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseUint(v, 10, 64); err != nil {
|
|
return uint16(v2)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Uint32(key string, defaultValue uint32) uint32 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseUint(v, 10, 64); err != nil {
|
|
return uint32(v2)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Uint64(key string, defaultValue uint64) uint64 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseUint(v, 10, 64); err != nil {
|
|
return v2
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Float32(key string, defaultValue float32) float32 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseFloat(v, 64); err != nil {
|
|
return float32(v2)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Float64(key string, defaultValue float64) float64 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseFloat(v, 64); err != nil {
|
|
return v2
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Complex64(key string, defaultValue complex64) complex64 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseComplex(v, 64); err != nil {
|
|
return complex64(v2)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Complex128(key string, defaultValue complex128) complex128 {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseComplex(v, 64); err != nil {
|
|
return v2
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func Bool(key string, defaultValue bool) bool {
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
if v2, err := strconv.ParseBool(v); err != nil {
|
|
return v2
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|