-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenv.go
More file actions
45 lines (35 loc) · 966 Bytes
/
Copy pathenv.go
File metadata and controls
45 lines (35 loc) · 966 Bytes
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
package common
import (
"errors"
"os"
)
//use export ENVIRONMENT=testing set global environment
const (
EnvTesting = Environment("testing")
EnvProduction = Environment("production")
)
type Environment string
func (env *Environment) String() string {
return string(*env)
}
func (env *Environment) Production() Environment {
return EnvProduction
}
func (env *Environment) Testing() Environment {
return EnvTesting
}
func (env Environment) Invalid() bool {
return env != EnvTesting && env != EnvProduction
}
// NewGlobalEnvironment 读取系统全局配置的环境变量
func NewGlobalEnvironment() (Environment, error) {
environment, ok := os.LookupEnv("ENVIRONMENT")
if !ok {
return "", errors.New("system environment:ENVIRONMENT not found")
}
env := Environment(environment)
if env != EnvTesting && env != EnvProduction {
return "", errors.New("environment not support, must be production, testing, development")
}
return env, nil
}