-
Notifications
You must be signed in to change notification settings - Fork 6
/
env.go
68 lines (57 loc) · 1.89 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
package config
import (
"strings"
"github.com/DoNewsCode/core/contract"
)
// Env is the environment of the application. It is primarily used as dependency injection symbol
type Env string
// IsLocal returns true if the environment is local
func (e Env) IsLocal() bool {
return e == "local"
}
// IsTesting returns true if the environment is testing
func (e Env) IsTesting() bool {
return e == "testing"
}
// IsDevelopment returns true if the environment is development
func (e Env) IsDevelopment() bool {
return e == "development"
}
// IsStaging returns true if the environment is staging
func (e Env) IsStaging() bool {
return e == "staging"
}
// IsProduction returns true if the environment is production
func (e Env) IsProduction() bool {
return e == "production"
}
// String returns the string form of the environment. This is a lowercase full word, such as production.
func (e Env) String() string {
return string(e)
}
// NewEnv takes in environment string and returns a Env type. It does some "best-effort" normalization internally.
// For example, prod, PROD, production and PRODUCTION produces the same type. It is recommended to use one of
// "production", "staging", "development", "local", or "testing" as output to avoid unexpected outcome.
func NewEnv(env string) Env {
if strings.EqualFold("prod", env) || strings.EqualFold("production", env) {
return "production"
}
if strings.EqualFold("pre-prod", env) || strings.EqualFold("staging", env) {
return "staging"
}
if strings.EqualFold("development", env) || strings.EqualFold("dev", env) {
return "development"
}
if strings.EqualFold("local", env) {
return "local"
}
if strings.EqualFold("testing", env) {
return "testing"
}
return "unknown"
}
// NewEnvFromConf reads the name of application from configuration's "env" entry.
func NewEnvFromConf(conf contract.ConfigAccessor) Env {
envStr := conf.String("env")
return NewEnv(envStr)
}