-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
77 lines (63 loc) · 1.35 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
package env
import (
"flag"
"fmt"
"strings"
)
var (
active Environment
dev Environment = &environment{value: "dev"}
fat Environment = &environment{value: "fat"}
uat Environment = &environment{value: "uat"}
pro Environment = &environment{value: "pro"}
)
var _ Environment = (*environment)(nil)
// Environment 环境配置
type Environment interface {
Value() string
IsDev() bool
IsFat() bool
IsUat() bool
IsPro() bool
t()
}
type environment struct {
value string
}
func (e *environment) Value() string {
return e.value
}
func (e *environment) IsDev() bool {
return e.value == "dev"
}
func (e *environment) IsFat() bool {
return e.value == "fat"
}
func (e *environment) IsUat() bool {
return e.value == "uat"
}
func (e *environment) IsPro() bool {
return e.value == "pro"
}
func (e *environment) t() {}
func init() {
env := flag.String("env", "", "请输入运行环境:\n dev:开发环境\n fat:测试环境\n uat:预上线环境\n pro:正式环境\n")
flag.Parse()
switch strings.ToLower(strings.TrimSpace(*env)) {
case "dev":
active = dev
case "fat":
active = fat
case "uat":
active = uat
case "pro":
active = pro
default:
active = fat
fmt.Println("Warning: '-env' cannot be found, or it is illegal. The default 'fat' will be used.")
}
}
// Active 当前配置的env
func Active() Environment {
return active
}