package cmd import ( "complie-erlang/cache" "complie-erlang/parser" "complie-erlang/parser/zm_lib" "fmt" "github.com/spf13/cobra" "log" "path/filepath" ) type ErrCode struct { debug bool ErrPath string Plugin string // 代码根目录 } func (s *ErrCode) run(_ *cobra.Command, arg []string) { if len(arg) == 0 { arg = append(arg, "show") } switch arg[0] { case "merge": newCache, err := cache.NewCache("errorCode.json") if err != nil { log.Printf("[error] newCache err:%s", err.Error()) return } fmt.Println("") newCache.Iteration(func(key string, val any) { fmt.Printf(" key:%s val:%s\n", key, val) }) fmt.Println("ok") case "set": newCache, err := cache.NewCache("errorCode.json") if err != nil { log.Printf("[error] newCache err:%s", err.Error()) return } if len(arg) != 3 { log.Printf("[error] len(arg):%d %s", len(arg), arg) return } err = newCache.Set(arg[1], arg[2]) fmt.Println("err:", err) case "get": newCache, err := cache.NewCache("errorCode.json") if err != nil { log.Printf("[error] newCache err:%s", err.Error()) return } fmt.Println("") newCache.Iteration(func(key string, val any) { fmt.Printf(" key:%s val:%s\n", key, val) }) fmt.Println("") default: pluginPathByWd, err := zm_lib.GetPluginPathByWd(s.Plugin) if err != nil { log.Printf("[warn] plugin path no find:%s", err.Error()) pluginPathByWd = "" } errPath := filepath.Join(pluginPathByWd, s.ErrPath) clientCfg, err := parser.NewClientConfigWithFile(errPath) if err != nil { log.Printf("[error] load errPath:%s %s\n", errPath, err.Error()) return } errCode := parser.NewErrCode(clientCfg) err = errCode.LoadWd() newCache, err := cache.NewCache("errorCode.json") if err != nil { log.Printf("[error] newCache err:%s", err.Error()) return } _ = errCode.PrintErrCode(newCache) fmt.Printf("err:%v \n", err) } } func init() { var singleSet = new(ErrCode) var logsCmd = &cobra.Command{ Use: "err_code", Short: "检索代码模块生成错误码配置", Long: `检索代码模块生成错误码配置 更多用法: - 查看缓存区 get - 设置缓存区 set <错误码> <错误码翻译设置> - 提交缓存区 merge <功能名称> `, Run: singleSet.run, PostRun: func(cmd *cobra.Command, args []string) { fmt.Println("\n-h 可解锁更多用法") }, } logsCmd.PersistentFlags().BoolVar(&singleSet.debug, "debug", false, "是否启动调试模式") _ = zm_lib.PersistentFlagsStringVar(logsCmd, &singleSet.ErrPath, globalCache, "path", "cfg/ErrorLanguagePackage.txt", "错误码地址(相对于根目录)") _ = zm_lib.PersistentFlagsStringVar(logsCmd, &singleSet.Plugin, globalCache, "plugin", "plugin", "代码根目录") rootCmd.AddCommand(logsCmd) }