You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
889 B
Go
47 lines
889 B
Go
// Package envutil provides environment variable parsing helpers with fallback values.
|
|
package envutil
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// String returns the environment variable value or fallback when empty.
|
|
func String(key string, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
// Int returns the parsed integer environment variable or fallback on parse error.
|
|
func Int(key string, fallback int) int {
|
|
v := os.Getenv(key)
|
|
if v == "" {
|
|
return fallback
|
|
}
|
|
|
|
i, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
|
|
return i
|
|
}
|
|
|
|
// Duration returns the parsed duration environment variable or fallback on parse error.
|
|
func Duration(key string, fallback time.Duration) time.Duration {
|
|
v := os.Getenv(key)
|
|
if v == "" {
|
|
return fallback
|
|
}
|
|
|
|
d, err := time.ParseDuration(v)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
|
|
return d
|
|
}
|