1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00
pasty/internal/env/env.go

43 lines
1.0 KiB
Go
Raw Normal View History

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"
)
// 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 {
return fallback
}
2021-04-15 21:59:32 +03:00
return value
}
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
}