diff --git a/cmd/errcode.go b/cmd/errcode.go index 6991323..3ed9e5e 100644 --- a/cmd/errcode.go +++ b/cmd/errcode.go @@ -1,19 +1,38 @@ package cmd import ( + "complie-erlang/parser/zm_lib" + "fmt" "github.com/spf13/cobra" + "log" "os" "path/filepath" - "strings" ) type ErrCode struct { debug bool ErrPath string + Plugin 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, } - 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().StringVar(&singleSet.ErrPath, "path", errorLanguagePath, "错误码地址") + logsCmd.PersistentFlags().StringVar(&singleSet.ErrPath, "path", ".cfg/ErrorLanguage.txt", "错误码地址(相对于根目录)") + logsCmd.PersistentFlags().StringVar(&singleSet.Plugin, "plugin", "plugin", "代码根目录") rootCmd.AddCommand(logsCmd) } diff --git a/parser/zm_lib/public_tools.go b/parser/zm_lib/public_tools.go new file mode 100644 index 0000000..dfe7fc9 --- /dev/null +++ b/parser/zm_lib/public_tools.go @@ -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") +}