-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
234 lines (215 loc) · 5.2 KB
/
app.go
File metadata and controls
234 lines (215 loc) · 5.2 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package env
import (
"flag"
"fmt"
"os"
"strings"
)
// environment helpers
const (
Testing = "testing"
Local = "local"
Staging = "staging"
Production = "production"
)
// IsAppEnvTesting reports whether APP_ENV is "testing" or the process looks like `go test`.
// @group Application environment
// @behavior readonly
//
// Checks APP_ENV, the -test.v flag, and arguments ending with ".test" or "-test.run".
//
// Example: APP_ENV explicitly testing
//
// _ = os.Setenv("APP_ENV", env.Testing)
// env.Dump(env.IsAppEnvTesting())
// // #bool true
//
// Example: no test markers
//
// _ = os.Unsetenv("APP_ENV")
// env.Dump(env.IsAppEnvTesting())
// // #bool false (outside of test binaries)
func IsAppEnvTesting() bool {
return os.Getenv("APP_ENV") == Testing ||
flag.Lookup("test.v") != nil ||
isTestSuffixFromArguments()
}
// isTestSuffixFromArguments checks if the test suffix is present in the command line arguments
func isTestSuffixFromArguments() bool {
anyArgumentContainsTestSuffix := false
for _, arg := range os.Args {
if strings.HasSuffix(arg, ".test") || strings.HasSuffix(arg, "-test.run") {
anyArgumentContainsTestSuffix = true
}
}
return anyArgumentContainsTestSuffix
}
// GetAppEnv returns the current APP_ENV (empty string if unset).
// @group Application environment
// @behavior readonly
//
// Example: simple retrieval
//
// _ = os.Setenv("APP_ENV", "staging")
// env.Dump(env.GetAppEnv())
// // #string "staging"
func GetAppEnv() string {
return os.Getenv("APP_ENV")
}
// IsAppEnv checks if APP_ENV matches any of the provided environments.
// @group Application environment
// @behavior readonly
//
// Example: match any allowed environment
//
// _ = os.Setenv("APP_ENV", "staging")
// env.Dump(env.IsAppEnv(env.Production, env.Staging))
// // #bool true
//
// Example: unmatched environment
//
// _ = os.Setenv("APP_ENV", "local")
// env.Dump(env.IsAppEnv(env.Production, env.Staging))
// // #bool false
func IsAppEnv(envs ...string) bool {
current := os.Getenv("APP_ENV")
for _, env := range envs {
if current == env {
return true
}
}
return false
}
// IsAppEnvProduction checks if APP_ENV is "production".
// @group Application environment
// @behavior readonly
//
// Example:
//
// _ = os.Setenv("APP_ENV", env.Production)
// env.Dump(env.IsAppEnvProduction())
// // #bool true
func IsAppEnvProduction() bool {
return IsAppEnv(Production)
}
// IsAppEnvStaging checks if APP_ENV is "staging".
// @group Application environment
// @behavior readonly
//
// Example:
//
// _ = os.Setenv("APP_ENV", env.Staging)
// env.Dump(env.IsAppEnvStaging())
// // #bool true
func IsAppEnvStaging() bool {
return IsAppEnv(Staging)
}
// IsAppEnvLocalOrStaging checks if APP_ENV is either "local" or "staging".
// @group Application environment
// @behavior readonly
//
// Example:
//
// _ = os.Setenv("APP_ENV", env.Local)
// env.Dump(env.IsAppEnvLocalOrStaging())
// // #bool true
func IsAppEnvLocalOrStaging() bool {
return IsAppEnv(Local, Staging)
}
// IsAppEnvLocal checks if APP_ENV is "local".
// @group Application environment
// @behavior readonly
//
// Example:
//
// _ = os.Setenv("APP_ENV", env.Local)
// env.Dump(env.IsAppEnvLocal())
// // #bool true
func IsAppEnvLocal() bool {
return IsAppEnv(Local)
}
// IsAppEnvTestingOrLocal checks if APP_ENV is "testing" or "local".
// @group Application environment
// @behavior readonly
//
// Example:
//
// _ = os.Setenv("APP_ENV", env.Testing)
// env.Dump(env.IsAppEnvTestingOrLocal())
// // #bool true
func IsAppEnvTestingOrLocal() bool {
return IsAppEnv(Testing, Local)
}
// SetAppEnv sets APP_ENV to a supported value.
// @group Application environment
// @behavior mutates-process-env
//
// Returns an error when the environment is unsupported.
//
// Example: set a supported environment
//
// _ = env.SetAppEnv(env.Staging)
// env.Dump(env.GetAppEnv())
// // #string "staging"
func SetAppEnv(appEnv string) error {
if !isValidAppEnv(appEnv) {
return fmt.Errorf("invalid APP_ENV: %s", appEnv)
}
return os.Setenv("APP_ENV", appEnv)
}
// SetAppEnvLocal sets APP_ENV to "local".
// @group Application environment
// @behavior mutates-process-env
//
// Example:
//
// _ = env.SetAppEnvLocal()
// env.Dump(env.GetAppEnv())
// // #string "local"
func SetAppEnvLocal() error {
return SetAppEnv(Local)
}
// SetAppEnvStaging sets APP_ENV to "staging".
// @group Application environment
// @behavior mutates-process-env
//
// Example:
//
// _ = env.SetAppEnvStaging()
// env.Dump(env.GetAppEnv())
// // #string "staging"
func SetAppEnvStaging() error {
return SetAppEnv(Staging)
}
// SetAppEnvProduction sets APP_ENV to "production".
// @group Application environment
// @behavior mutates-process-env
//
// Example:
//
// _ = env.SetAppEnvProduction()
// env.Dump(env.GetAppEnv())
// // #string "production"
func SetAppEnvProduction() error {
return SetAppEnv(Production)
}
// SetAppEnvTesting sets APP_ENV to "testing".
// @group Application environment
// @behavior mutates-process-env
//
// Example:
//
// _ = env.SetAppEnvTesting()
// env.Dump(env.GetAppEnv())
// // #string "testing"
func SetAppEnvTesting() error {
return SetAppEnv(Testing)
}
func isValidAppEnv(appEnv string) bool {
switch appEnv {
case Testing, Local, Staging, Production:
return true
default:
return false
}
}