1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package env
- import (
- "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 Set(env string) error {
- var err error
- switch strings.ToLower(strings.TrimSpace(env)) {
- case "dev":
- active = dev
- case "fat":
- active = fat
- case "uat":
- active = uat
- case "pro":
- active = pro
- default:
- err = fmt.Errorf("'%s' cannot be found, or it is illegal. enum:dev(开发环境),fat(测试环境),uat(预上线环境),pro(正式环境)", env)
- }
- return err
- }
- // Active 当前配置的env
- func Active() Environment {
- if active == nil {
- fmt.Println("Warning: environment not set. The default 'dev' will be used.")
- active = dev
- }
- return active
- }
|