24 lines
457 B
Go
24 lines
457 B
Go
package env
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// Get an environment variable as a time.Duration, specifying a default value if its
|
|
// not set or can't be parsed properly
|
|
func Duration(key string, defaultValue time.Duration) time.Duration {
|
|
val, exists := os.LookupEnv(key)
|
|
if !exists {
|
|
return time.Duration(defaultValue)
|
|
}
|
|
|
|
intVal, err := strconv.Atoi(val)
|
|
if err != nil {
|
|
return time.Duration(defaultValue)
|
|
}
|
|
return time.Duration(intVal)
|
|
|
|
}
|