This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
env.go
75 lines (67 loc) · 1.47 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package env
import (
"fmt"
"log"
"os"
"strings"
"github.com/joho/godotenv"
"github.com/lytics/logrus"
)
var envLoaded = false
//Load load .env file
func Load(baseDir string, isTesting bool) {
var envFile string
if isTesting {
envFile = "env-test"
} else {
envFile = "env-default"
}
err := godotenv.Load(os.ExpandEnv(
fmt.Sprintf("%s/%s", baseDir, envFile),
))
if err != nil {
logrus.Warn(fmt.Sprintf("Failed loading env file, error: %d", err.Error()))
}
envLoaded = true
//checkRequiredEnvs()
}
// Getenv Gets env var value
func Getenv(varName string) string {
return os.Getenv(varName)
}
// MustGetenv Gets env var value, will panic if var is not set
func MustGetenv(varName string) string {
value := os.Getenv(varName)
if value == "" {
panic(fmt.Sprintf("Can't get ENV variable `%s`", varName))
}
return value
}
func checkRequiredEnvs() {
forceEnv := strings.TrimSpace(Getenv("FORCE_ENV_CHECKING"))
if forceEnv != "1" && forceEnv != "0" {
log.Fatal("env var FORCE_ENV_CHECKING is missing! Set it to one of 1|0")
return
}
if forceEnv == "0" {
return
}
requiredVars := []string{
"BASE_URL",
"EMAIL_FROM",
"AWS_SES_KEY",
"AWS_SES_SECRET",
"SLACK_WEBHOOK_URL",
}
missingFatal := []string{}
for _, val := range requiredVars {
if Getenv(val) == "" {
missingFatal = append(missingFatal, val)
}
}
if len(missingFatal) > 0 {
log.Fatal(
fmt.Sprintf("Some required env vars are missing [%s]", strings.Join(missingFatal, ", ")),
)
}
}