邹方成 45815bfb7d chore: 清理无用文件与优化代码结构
refactor(utils): 修复密码哈希比较逻辑错误
feat(user): 新增按状态筛选优惠券接口
docs: 添加虚拟发货与任务中心相关文档
fix(wechat): 修正Code2Session上下文传递问题
test: 补充订单折扣与积分转换测试用例
build: 更新配置文件与构建脚本
style: 清理多余的空行与注释
2025-12-18 17:35:55 +08:00

90 lines
1.5 KiB
Go

package env
import (
"flag"
"fmt"
"strings"
"sync"
)
var (
active Environment
dev Environment = &environment{value: "dev"}
fat Environment = &environment{value: "fat"}
uat Environment = &environment{value: "uat"}
pro Environment = &environment{value: "pro"}
envFlag *string
once sync.Once
)
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() {
envFlag = flag.String("env", "", "请输入运行环境:\n dev:开发环境\n fat:测试环境\n uat:预上线环境\n pro:正式环境\n")
}
func setup() {
val := ""
if envFlag != nil && flag.Parsed() {
val = *envFlag
}
switch strings.ToLower(strings.TrimSpace(val)) {
case "dev":
active = dev
case "fat":
active = fat
case "uat":
active = uat
case "pro":
active = pro
default:
active = fat
if val != "" {
fmt.Println("Warning: '-env' cannot be found, or it is illegal. The default 'fat' will be used.")
}
}
}
// Active 当前配置的env
func Active() Environment {
once.Do(setup)
return active
}