优化缓存机制 补充错误码

This commit is contained in:
2025-10-09 02:04:32 +08:00
parent 73480005eb
commit 84899f9935
10 changed files with 596 additions and 28 deletions
+71
View File
@@ -0,0 +1,71 @@
package cmd
import (
"complie-erlang/cache"
"complie-erlang/parser/zm_lib"
"fmt"
"github.com/spf13/cobra"
)
type cacheSet struct {
Path string
debug bool
}
func (c *cacheSet) Run(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("No command specified.")
return
}
if err := c.run(args); err != nil {
fmt.Println(err)
return
}
fmt.Println("Done.")
}
func (c *cacheSet) run(args []string) error {
pathByExecutable, err := zm_lib.FindPathByExecutable(c.Path)
if err != nil {
return err
}
newCache, err := cache.NewCache(pathByExecutable)
if err != nil {
return err
}
switch args[0] {
case "set":
return newCache.Set(args[1], args[2])
case "get":
val, is := newCache.Get(args[1])
if is {
fmt.Printf("%s : %s \n", args[0], val)
} else {
fmt.Printf("%s : nil \n", args[0])
}
default:
return fmt.Errorf("unknown command: %s", args[0])
}
return nil
}
func init() {
var singleSet = new(cacheSet)
var logsCmd = &cobra.Command{
Use: "global",
Short: "缓存数据模版",
Long: `构建功能数据`,
Run: singleSet.Run,
}
logsCmd.PersistentFlags().BoolVar(&singleSet.debug, "debug", false, "是否启动调试模式")
logsCmd.PersistentFlags().StringVar(&singleSet.Path, "path", globalCacheFileName, "缓存数据存放地址(相对于根目录)")
rootCmd.AddCommand(logsCmd)
}
+79 -18
View File
@@ -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)
}
+13 -9
View File
@@ -9,7 +9,6 @@ import (
"log"
"os"
"path/filepath"
"strings"
)
type Pb2Port struct {
@@ -113,20 +112,25 @@ func init() {
// 写入默认数据
if currentDir, err := os.Getwd(); err == nil {
out = fmt.Sprintf("%s_port.erl", filepath.Base(currentDir))
pluginSpilt := strings.Split(currentDir, "plugin")
if len(pluginSpilt) > 0 {
pluginSpilt = pluginSpilt[:len(pluginSpilt)-1]
protoPath = filepath.Join(strings.Join(pluginSpilt, "plugin"), fmt.Sprintf("\\gpb\\game\\pro_%s.proto", filepath.Base(currentDir)))
outPut, _ := globalCache.GetDefault(fmt.Sprintf("%s.out", logsCmd.Use), "%s_port.erl")
out = fmt.Sprintf(outPut.(string), filepath.Base(currentDir))
plugin, _ := globalCache.GetDefault(fmt.Sprintf("%s.Plugin", logsCmd.Use), "plugin")
pluginPathByWd, err := zm_lib.GetPluginPathByWd(plugin.(string))
if err == nil {
defaultProtoPath, _ := globalCache.GetDefault(fmt.Sprintf("%s.Proto", logsCmd.Use), "\\gpb\\game\\pro_%s.proto")
protoPath = filepath.Join(pluginPathByWd, fmt.Sprintf(defaultProtoPath.(string), filepath.Base(currentDir)))
}
}
logsCmd.PersistentFlags().BoolVar(&singleSet.debug, "debug", false, "是否启动调试模式")
logsCmd.PersistentFlags().StringVar(&singleSet.protoPath, "proto", protoPath, "读取文件")
logsCmd.PersistentFlags().StringVar(&singleSet.out, "out", out, "输出文件")
logsCmd.PersistentFlags().StringVar(&singleSet.author, "author", "st,sutong@youkia.net", "作者")
logsCmd.PersistentFlags().StringVar(&singleSet.mainTemplate, "main_tpl", "ErlangPort", "主模版")
logsCmd.PersistentFlags().StringVar(&singleSet.tplDir, "tpl", "./templates/*.tpl", "模版地址")
_ = zm_lib.PersistentFlagsStringVar(logsCmd, &singleSet.author, globalCache, "author", "st,sutong@youkia.net", "作者")
_ = zm_lib.PersistentFlagsStringVar(logsCmd, &singleSet.mainTemplate, globalCache, "main_tpl", "ErlangPort", "主模版")
_ = zm_lib.PersistentFlagsStringVar(logsCmd, &singleSet.tplDir, globalCache, "tpl", "./templates/*.tpl", "模版地址")
//logsCmd.PersistentFlags().StringVar(&singleSet.mainTemplate, "main_tpl", "ErlangPort", "主模版")
//logsCmd.PersistentFlags().StringVar(&singleSet.tplDir, "tpl", "./templates/*.tpl", "模版地址")
rootCmd.AddCommand(logsCmd)
}
+19
View File
@@ -1,11 +1,30 @@
package cmd
import (
"complie-erlang/cache"
"complie-erlang/parser/zm_lib"
"fmt"
"github.com/spf13/cobra"
"os"
)
// 全局缓存
var globalCache = InitGlobalCache()
const globalCacheFileName = "global.json"
func InitGlobalCache() cache.Cache {
pathByExecutable, err := zm_lib.FindPathByExecutable(globalCacheFileName)
if err != nil {
panic("未查找到根目录 Err:" + err.Error())
}
newCache, err := cache.NewCache(pathByExecutable)
if err != nil {
panic("创建缓存失败 Err:" + err.Error())
}
return newCache
}
var rootCmd = &cobra.Command{
Use: "",
Short: "erlang注解生成配置模版工具",