-
Notifications
You must be signed in to change notification settings - Fork 6
/
env.go
83 lines (71 loc) · 2.09 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
package config
import (
"strings"
"github.com/DoNewsCode/core/contract"
)
// global static variables for Env
const (
// local
EnvLocal Env = "local"
// testing
EnvTesting Env = "testing"
// development
EnvDevelopment Env = "development"
// staging
EnvStaging Env = "staging"
// production
EnvProduction Env = "production"
// unknown
EnvUnknown Env = "unknown"
)
// 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 == EnvLocal
}
// IsTesting returns true if the environment is testing
func (e Env) IsTesting() bool {
return e == EnvTesting
}
// IsDevelopment returns true if the environment is development
func (e Env) IsDevelopment() bool {
return e == EnvDevelopment
}
// IsStaging returns true if the environment is staging
func (e Env) IsStaging() bool {
return e == EnvStaging
}
// IsProduction returns true if the environment is production
func (e Env) IsProduction() bool {
return e == EnvProduction
}
// 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 {
switch strings.ToLower(env) {
case "production", "prod", "online":
return EnvProduction
case "pre-prod", "staging":
return EnvStaging
case "development", "develop", "dev":
return EnvDevelopment
case "local":
return EnvLocal
case "testing", "test":
return EnvTesting
default:
return EnvUnknown
}
}
// NewEnvFromConf reads the name of application from configuration's "env" entry.
func NewEnvFromConf(conf contract.ConfigUnmarshaler) Env {
var envStr string
conf.Unmarshal("env", &envStr)
return NewEnv(envStr)
}