2020-08-23 00:24:38 +03:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-08-24 00:44:47 +03:00
|
|
|
"strconv"
|
2020-09-19 02:56:50 +03:00
|
|
|
"time"
|
2021-04-15 20:26:17 +03:00
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/lus/pasty/internal/static"
|
2020-08-23 00:24:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Load loads an optional .env file
|
|
|
|
func Load() {
|
|
|
|
godotenv.Load()
|
|
|
|
}
|
|
|
|
|
2021-04-15 21:15:42 +03:00
|
|
|
// MustString returns the content of the environment variable with the given key or the given fallback
|
|
|
|
func MustString(key, fallback string) string {
|
2021-04-15 21:59:32 +03:00
|
|
|
value, found := os.LookupEnv(static.EnvironmentVariablePrefix + key)
|
|
|
|
if !found {
|
2020-08-23 00:24:38 +03:00
|
|
|
return fallback
|
|
|
|
}
|
2021-04-15 21:59:32 +03:00
|
|
|
return value
|
2020-08-23 00:24:38 +03:00
|
|
|
}
|
2020-08-24 00:44:47 +03:00
|
|
|
|
2021-04-15 21:15:42 +03:00
|
|
|
// MustBool uses MustString and parses it into a boolean
|
|
|
|
func MustBool(key string, fallback bool) bool {
|
|
|
|
parsed, _ := strconv.ParseBool(MustString(key, strconv.FormatBool(fallback)))
|
2020-08-24 00:44:47 +03:00
|
|
|
return parsed
|
|
|
|
}
|
2020-09-19 02:56:50 +03:00
|
|
|
|
2021-04-15 21:15:42 +03:00
|
|
|
// MustInt uses MustString and parses it into an integer
|
|
|
|
func MustInt(key string, fallback int) int {
|
|
|
|
parsed, _ := strconv.Atoi(MustString(key, strconv.Itoa(fallback)))
|
|
|
|
return parsed
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustDuration uses MustString and parses it into a duration
|
|
|
|
func MustDuration(key string, fallback time.Duration) time.Duration {
|
|
|
|
parsed, _ := time.ParseDuration(MustString(key, fallback.String()))
|
2020-09-19 02:56:50 +03:00
|
|
|
return parsed
|
|
|
|
}
|