优化缓存机制 补充错误码
This commit is contained in:
+79
-18
@@ -1,11 +1,12 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"complie-erlang/cache"
|
||||
"complie-erlang/parser"
|
||||
"complie-erlang/parser/zm_lib"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
@@ -18,21 +19,71 @@ type ErrCode struct {
|
||||
}
|
||||
|
||||
func (s *ErrCode) run(_ *cobra.Command, arg []string) {
|
||||
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)
|
||||
|
||||
fmt.Printf("errPath:%s %s\n", pluginPathByWd, errPath)
|
||||
errorLanguageBytes, err := os.ReadFile(errPath)
|
||||
if err != nil {
|
||||
log.Printf("[error] read errPath:%s %s\n", errPath, err.Error())
|
||||
return
|
||||
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)
|
||||
|
||||
fmt.Printf("errPath:%s %s\n", errorLanguageBytes, 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,14 +91,24 @@ func init() {
|
||||
var singleSet = new(ErrCode)
|
||||
var logsCmd = &cobra.Command{
|
||||
Use: "err_code",
|
||||
Short: "根据proto 文件构建 功能模版",
|
||||
Long: `构建功能数据`,
|
||||
Run: singleSet.run,
|
||||
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, "是否启动调试模式")
|
||||
logsCmd.PersistentFlags().StringVar(&singleSet.ErrPath, "path", ".cfg/ErrorLanguage.txt", "错误码地址(相对于根目录)")
|
||||
logsCmd.PersistentFlags().StringVar(&singleSet.Plugin, "plugin", "plugin", "代码根目录")
|
||||
|
||||
_ = zm_lib.PersistentFlagsStringVar(logsCmd, &singleSet.ErrPath, globalCache, "path", "cfg/ErrorLanguagePackage.txt", "错误码地址(相对于根目录)")
|
||||
_ = zm_lib.PersistentFlagsStringVar(logsCmd, &singleSet.Plugin, globalCache, "plugin", "plugin", "代码根目录")
|
||||
|
||||
rootCmd.AddCommand(logsCmd)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user