2020-06-20 23:12:07 +02:00
|
|
|
package environment
|
|
|
|
|
2020-06-22 16:49:45 +02:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
2020-06-20 23:12:07 +02:00
|
|
|
|
|
|
|
// Available environments example.
|
|
|
|
const (
|
|
|
|
PROD Env = "production"
|
|
|
|
DEV Env = "development"
|
|
|
|
)
|
2020-06-22 16:49:45 +02:00
|
|
|
|
|
|
|
// Env is the environment type.
|
|
|
|
type Env string
|
|
|
|
|
|
|
|
// String just returns the string representation of the Env.
|
|
|
|
func (e Env) String() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadEnv returns the environment of the system environment variable of "key".
|
|
|
|
// Returns the "def" if not found.
|
|
|
|
// Reports a panic message if the environment variable found
|
|
|
|
// but the Env is unknown.
|
|
|
|
func ReadEnv(key string, def Env) Env {
|
|
|
|
v := Getenv(key, def.String())
|
|
|
|
if v == "" {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
env := Env(strings.ToLower(v))
|
|
|
|
switch env {
|
|
|
|
case PROD, DEV: // allowed.
|
|
|
|
default:
|
|
|
|
panic("unexpected environment " + v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getenv returns the value of a system environment variable "key".
|
|
|
|
// Defaults to "def" if not found.
|
|
|
|
func Getenv(key string, def string) string {
|
|
|
|
if v := os.Getenv(key); v != "" {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
return def
|
|
|
|
}
|