-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.go
73 lines (62 loc) · 1.57 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
package service
import (
"os"
"strings"
)
// Environment can be configured with the NAGA_ENV environment variable.
// This defaults to EnvUnknown if not otherwise set.s
type Environment int
// EnvVarName is the name of environment variable which contains the app
// environment. Defaults to NAGA_ENV.
var EnvVarName = "NAGA_ENV"
// Environment constants
const (
EnvProduction Environment = iota
EnvStaging
EnvDevelopment
EnvTest
EnvUnknown
)
// EnvMap gives mappings of strings to specific environments.
var EnvMap = map[Environment][]string{
EnvProduction: []string{"production"},
EnvStaging: []string{"staging"},
EnvDevelopment: []string{"development"},
EnvTest: []string{"test"},
}
// IsProduction returns true iff env is production.
func (e Environment) IsProduction() bool {
return e == EnvProduction
}
// IsHosted returns true if env if prod or staging
func (e Environment) IsHosted() bool {
return e == EnvProduction || e == EnvStaging
}
// IsDevelopment returns true iff env is development.
func (e Environment) IsDevelopment() bool {
return e == EnvDevelopment
}
// IsTest returns true iff env is test.
func (e Environment) IsTest() bool {
return e == EnvTest
}
func (e Environment) String() string {
s := EnvMap[e]
if len(s) == 0 {
return "unknown"
}
return s[0]
}
// GetEnvironment returns the app environment parsed from the
// environment variable.
func GetEnvironment() Environment {
v := strings.ToLower(os.Getenv(EnvVarName))
for env, l := range EnvMap {
for _, e := range l {
if e == v {
return env
}
}
}
return EnvUnknown
}