-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
88 lines (75 loc) · 2.56 KB
/
env.go
File metadata and controls
88 lines (75 loc) · 2.56 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
package env
import (
"fmt"
"os"
"path/filepath"
"github.com/abstratium-informatique-sarl/stratis/pkg/logging"
"github.com/joho/godotenv"
)
const _PROD = "prod"
var env string
// Setup will load the environment variables from the .env file and the privateEnvFileLocation
// privateEnvFileLocation is the path to the private environment variables file
// e.g. it might live in your home folder, but basically anywhere outside of your repository,
// so that the secrets are not made public
func Setup(privateEnvFileLocation string) {
log := logging.GetLog("env")
env = os.Getenv("STRATIS_ENV") // empty means prod
wd, _ := os.Getwd()
log.Info().Msgf("=======================================")
log.Info().Msgf(" ENV %s", env)
log.Info().Msgf(" ")
log.Info().Msgf("searching for env file, pwd=%s", wd)
// determine where the files are - when tests run, they run with the working dir lower than the root
dots := "."
for {
filename := dots + "/.env"
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
// file don't exist
log.Info().Msgf("no env file found in %s/%s, going up a directory...", wd, dots)
dots = dots + "/.."
} else {
panic(fmt.Sprintf("Error checking file '%s': %+v\n", filename, err))
}
} else {
break
}
}
f, _ := filepath.Abs(wd + "/" + dots)
if len(env) > 0 {
log.Info().Msgf("loading %s/.env.%s", f, env)
godotenv.Load(dots + "/.env." + env)
}
log.Info().Msgf("loading (adding) %s/.env", f)
var err error
err = godotenv.Load(dots + "/.env") // It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults.
if err != nil {
panic(fmt.Sprintf("Error loading file '%s/.env': %+v\n", f, err))
}
// finally add privateEnvFileLocation if present, as it holds secrets for when developing locally
_, err = os.Stat(privateEnvFileLocation)
if err != nil {
if os.IsNotExist(err) {
log.Info().Msgf("skipping env file %s as it does not exist", privateEnvFileLocation)
} else {
panic(fmt.Sprintf("Error checking file '%s': %+v\n", privateEnvFileLocation, err))
}
} else {
log.Info().Msgf("loading (adding) env file %s", privateEnvFileLocation)
godotenv.Load(privateEnvFileLocation) // It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults.
}
if len(env) == 0 {
env = _PROD
}
}
func Getenv() string {
return env
}
func GetenvIsNotProd() bool {
return !GetenvIsProd()
}
func GetenvIsProd() bool {
return env == _PROD
}