2021-06-07 16:07:56 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2021-06-07 16:19:57 +00:00
|
|
|
"time"
|
2021-06-07 16:07:56 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-06-07 16:19:57 +00:00
|
|
|
func GetEnvVarDuration(n string) (time.Duration, bool) {
|
|
|
|
str, ok := os.LookupEnv(n)
|
|
|
|
if !ok {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
du, err := time.ParseDuration(str)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("can not parse env var %q as time.Duration, incorrect format", str)
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return du, true
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:07:56 +00:00
|
|
|
func GetEnvVarInt(n string) (int, bool) {
|
|
|
|
str, ok := os.LookupEnv(n)
|
|
|
|
if !ok {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
num, err := strconv.Atoi(str)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("can not parse env var %q as int, incorrect format", str)
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return num, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetEnvVarBool(n string) (bool, bool) {
|
|
|
|
str, ok := os.LookupEnv(n)
|
|
|
|
if !ok {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
num, err := strconv.ParseBool(str)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("can not parse env var %q as bool, incorrect format", str)
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return num, true
|
|
|
|
}
|