/
Env.go
122 lines (96 loc) · 4.04 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package infra
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/joho/godotenv"
coreErrors "github.com/suared/core/errors"
)
//Go treats these as singleton so do not need to code singleton manually here
func init() {
//fmt.Println("This will get called on main initialization only")
setEnvironmentVariables()
loadEnvironmentVariables()
setupErrorEventing()
runtime.GOMAXPROCS(runtime.NumCPU())
}
func setEnvironmentVariables() {
//Tells AWS SDK Client to use its default config chain - ~/.aws/config
//Overrides can be done at various levels including the code accessors
os.Setenv("AWS_SDK_LOAD_CONFIG", "1")
}
//Assumes .env file will be in either working directory or infra/ sub-directory. Will check up to 2 previous directories and then give up
//The environment variable PROCESS_ENV_CHECK is used to validate this is the right kind of env file if an invalid file is found
func loadEnvironmentVariables() {
// Directly from the godotenv README here --> https://github.com/joho/godotenv
// Existing envs take precendence of envs that are loaded later.
// e.g. load prod.local followed by .env.local followed by .env.prod followed by .env
// NOTE: I am removing the interim one, seems like overkill for non/test, will bring back only if needed
//fmt.Printf("Starting up, model dir set to: %v", os.Getenv("PROCESS_MODEL_TESTFILE_DIR"))
//var PATHPREFIX string
// Do a search for the env file so that we can keep the src structure clean and test at different sub-package levels without impacting the relative check here
dir, _ := os.Getwd()
//as soon as a .env file is found, the env base dir search will stop
//Give up if have tried this for up to 4 previous directories, note that accidental .env will impact this so will both a) log and b) exit with a panic depending on the situation
newdir, found := checkForEnvFile(dir)
if !found {
newdir, found = checkForEnvFile(newdir)
}
if !found {
newdir, found = checkForEnvFile(newdir)
}
if !found {
newdir, found = checkForEnvFile(newdir)
}
if !found {
panic("Env Setup: Unable to find environment variable file from base: " + dir)
}
//Start processing environment files using the standard logic
env := os.Getenv("PROCESS_ENV")
if "" == env || env == "dev" {
env = "development"
}
//Intentional startup print
fmt.Printf("Loading env: %v, environment directory is %v", env, newdir)
godotenv.Load(filepath.Join(newdir, ".env."+env+".local"))
//fmt.Printf("After initial load, model dir set to: %v", os.Getenv("PROCESS_MODEL_TESTFILE_DIR"))
/*
if "test" != env {
godotenv.Load(".env.local")
}
*/
godotenv.Load(filepath.Join(newdir, "/.env."+env))
//fmt.Printf("after .env.<env> point, model dir set to: %v", os.Getenv("PROCESS_MODEL_TESTFILE_DIR"))
godotenv.Load(filepath.Join(newdir, ".env")) // The Original .env
//fmt.Println("attempted to load: " + filepath.Join(newdir, ".env"))
testVal := os.Getenv("PROCESS_ENV_CHECK")
if testVal == "" {
panic("Env Setup: Invalid environment variable file, missing: PROCESS_ENV_CHECK, received: " + testVal + ", otherval: " + os.Getenv("PROCESS_MODEL_TESTFILE_DIR"))
}
}
func fileExists(filename string) bool {
//log.Printf("checking file %v", filename)
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}
// with return the environment directory or the previous directory in the path along with a bool to indicate if found in which case return director is the directory where found
// or false if not found and the previous directory was returned
func checkForEnvFile(dir string) (string, bool) {
//log.Printf("checking directory %v", dir)
if fileExists(filepath.Join(dir, ".env")) {
return dir, true
} else if fileExists(filepath.Join(dir, "infra", ".env")) {
// log.Printf("checking directory %v/infra", dir)
return filepath.Join(dir, "infra"), true
} else {
// log.Println(".env not found")
return filepath.Join(dir, ".."), false
}
}
// To start, only checking and setting up Sentry - can change later as best
func setupErrorEventing() {
if os.Getenv("ERROR_EVENTING") == "true" {
coreErrors.SetupEventing()
}
}