fog/internal/env/env.go
2024-12-30 10:02:01 +00:00

50 lines
1.2 KiB
Go

package env
import (
"log"
"os"
"strconv"
"time"
)
func GetString(key, fallback string) string {
val, ok := os.LookupEnv(key)
if !ok {
log.Printf("Failed to lookup enviornment variable with key %s. Using fallback value '%s'", key, fallback)
return fallback
}
return val
}
func GetInt(key string, fallback int) int {
val, ok := os.LookupEnv(key)
if !ok {
log.Printf("Failed to lookup enviornment variable with key %s. Using fallback value '%d'", key, fallback)
return fallback
}
valInt, err := strconv.Atoi(val)
if err != nil {
log.Printf("Failed to convert environment variable with key %s and value %s to int. Using fallback %d. (err: %s)", key, val, fallback, err)
return fallback
}
return valInt
}
func GetDuration(key string, fallback time.Duration) time.Duration {
val, ok := os.LookupEnv(key)
if !ok {
log.Printf("Failed to lookup enviornment variable with key %s. Using fallback value '%s'", key, fallback)
return fallback
}
duration, err := time.ParseDuration(val)
if err != nil {
log.Printf("Failed to convert environment variable with key %s and value %s to duration. Using fallback %d. (err: %s)", key, val, fallback, err)
return fallback
}
return duration
}