添加错误码自动提取

This commit is contained in:
2025-09-27 23:58:21 +08:00
parent 49c2d264b5
commit cee7898fff
2 changed files with 46 additions and 13 deletions
+22 -13
View File
@@ -1,19 +1,38 @@
package cmd package cmd
import ( import (
"complie-erlang/parser/zm_lib"
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
type ErrCode struct { type ErrCode struct {
debug bool debug bool
ErrPath string ErrPath string
Plugin string // 代码根目录
} }
func (s *ErrCode) run(_ *cobra.Command, arg []string) { 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
}
fmt.Printf("errPath:%s %s\n", errorLanguageBytes, errPath)
} }
@@ -26,19 +45,9 @@ func init() {
Run: singleSet.run, Run: singleSet.run,
} }
var errorLanguagePath = ""
// 写入默认数据
if currentDir, err := os.Getwd(); err == nil {
pluginSpilt := strings.Split(currentDir, "plugin")
if len(pluginSpilt) > 0 {
pluginSpilt = pluginSpilt[:len(pluginSpilt)-1]
errorLanguagePath = filepath.Join(strings.Join(pluginSpilt, "plugin"), ".cfg/ErrorLanguage.txt")
}
}
logsCmd.PersistentFlags().BoolVar(&singleSet.debug, "debug", false, "是否启动调试模式") logsCmd.PersistentFlags().BoolVar(&singleSet.debug, "debug", false, "是否启动调试模式")
logsCmd.PersistentFlags().StringVar(&singleSet.ErrPath, "path", errorLanguagePath, "错误码地址") logsCmd.PersistentFlags().StringVar(&singleSet.ErrPath, "path", ".cfg/ErrorLanguage.txt", "错误码地址(相对于根目录)")
logsCmd.PersistentFlags().StringVar(&singleSet.Plugin, "plugin", "plugin", "代码根目录")
rootCmd.AddCommand(logsCmd) rootCmd.AddCommand(logsCmd)
} }
+24
View File
@@ -0,0 +1,24 @@
package zm_lib
import (
"errors"
"os"
"path/filepath"
"strings"
)
// GetPluginPathByWd 获取项目根目录 通过当前cmd 地址
func GetPluginPathByWd(pluginName string) (string, error) {
currentDir, err := os.Getwd()
if err != nil {
return "", err
}
// 写入默认数据
pluginSpilt := strings.Split(currentDir, pluginName)
if len(pluginSpilt) > 1 {
pluginSpilt = pluginSpilt[:len(pluginSpilt)-1]
return filepath.Join(strings.Join(pluginSpilt, pluginName), pluginName), nil
}
return "", errors.New("no find plugin")
}