1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 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 = dev
- fmt.Println("Warning: '-env' cannot be found, or it is illegal. The default 'dev' will be used.")
- }
- }
- // Active 当前配置的env
- func Active() Environment {
- return active
- }
|