package main import ( "fmt" "log/slog" "os" "path/filepath" "slices" "strings" "zzman/actions" "zzman/clients" "zzman/model" "github.com/joho/godotenv" slogjournal "github.com/systemd/slog-journal" ) func main() { // 初始化环境 slog.Debug("初始化环境变量") ex, err := os.Executable() if err != nil { panic(err) } exPath := filepath.Dir(ex) file := filepath.Join(exPath, ".env") println("加载环境变量文件:", file) err = godotenv.Load(file) if err != nil { slog.Error(fmt.Errorf("初始化变量失败:%w", err).Error()) } // 初始化日志 handler, err := slogjournal.NewHandler(&slogjournal.Options{ ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { a.Key = strings.ToUpper(a.Key) a.Key = strings.ReplaceAll(a.Key, "-", "_") return a }, }) slog.SetDefault(slog.New(handler)) // 初始化数据库和 Redis model.Init() defer model.Close() clients.InitRedis() defer clients.CloseRedis() // 执行命令 if len(os.Args) < 2 { println("缺少命令参数") return } switch os.Args[1] { // 同步城市节点 case "sync": err := actions.Sync() if err != nil { slog.Error(fmt.Sprintf("同步城市节点数据失败:%s", err.Error())) } else { slog.Info("同步城市节点数据成功") } return // 更新网关 case "update": var args actions.UpdateArgs if len(os.Args) >= 3 { if slices.Contains(os.Args, "--mock") { args.Mock = true } } err := actions.Update(args) if err != nil { slog.Error(fmt.Sprintf("更新节点失败:%s", err.Error())) } else { slog.Info("更新节点成功") } return // 清空网关 case "clear": err := actions.Clear() if err != nil { slog.Error(fmt.Sprintf("清空节点失败:%s", err.Error())) } else { slog.Info("清空节点成功") } return } println("请输入正确的命令参数") }