fog/internal/env/env.go

51 lines
1.2 KiB
Go
Raw Normal View History

2024-12-30 06:29:34 +00:00
package env
import (
"log"
"os"
"strconv"
2024-12-30 10:02:01 +00:00
"time"
2024-12-30 06:29:34 +00:00
)
func GetString(key, fallback string) string {
val, ok := os.LookupEnv(key)
if !ok {
2024-12-30 10:02:01 +00:00
log.Printf("Failed to lookup enviornment variable with key %s. Using fallback value '%s'", key, fallback)
2024-12-30 06:29:34 +00:00
return fallback
}
return val
}
func GetInt(key string, fallback int) int {
val, ok := os.LookupEnv(key)
if !ok {
2024-12-30 10:02:01 +00:00
log.Printf("Failed to lookup enviornment variable with key %s. Using fallback value '%d'", key, fallback)
2024-12-30 06:29:34 +00:00
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
}
2024-12-30 10:02:01 +00:00
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
}