93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"complie-erlang/config"
|
|
tm "complie-erlang/template"
|
|
"complie-erlang/worker"
|
|
"fmt"
|
|
"github.com/spf13/cobra"
|
|
"gopkg.in/yaml.v2"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type SingleFile struct {
|
|
CheckFile string
|
|
OutFile string
|
|
Cfg string
|
|
}
|
|
|
|
func (s *SingleFile) run(_ *cobra.Command, _ []string) {
|
|
|
|
cfg := config.DefaultErlConfig()
|
|
templates := tm.NewTemplate()
|
|
worker1 := worker.NewSingleFile(templates)
|
|
|
|
xmlPath, err := worker1.FindConfigXMLPath(s.Cfg)
|
|
if err != nil {
|
|
fmt.Printf("Err 获取可执行文件路径失败: %v", err)
|
|
return
|
|
}
|
|
|
|
bytes, err := os.ReadFile(xmlPath)
|
|
if err != nil {
|
|
fmt.Printf("Err 读取配置文件失败: %v", err)
|
|
return
|
|
}
|
|
|
|
if err = yaml.Unmarshal(bytes, cfg); err != nil {
|
|
fmt.Printf("Err 解析配置文件 %s 失败: %v", xmlPath, err)
|
|
return
|
|
}
|
|
|
|
// 获取可执行文件所在目录
|
|
exePath, err := os.Executable()
|
|
if err != nil {
|
|
log.Fatalf("获取可执行文件路径失败: %v", err)
|
|
return
|
|
}
|
|
|
|
// 加载模版报错
|
|
if err := templates.ParseGlob(filepath.Join(filepath.Dir(exePath), cfg.TemplateDirPattern)); err != nil {
|
|
log.Fatalf("Err 加载模版报错: %v", err)
|
|
return
|
|
}
|
|
|
|
var parseTemKeys []string
|
|
for _, cfgPublic := range cfg.Keywords {
|
|
// 查询目标文件
|
|
parseTemKey, err := worker1.ParseTemKey(s.CheckFile, cfgPublic)
|
|
if err != nil {
|
|
log.Fatalf("loading templates: %v", err)
|
|
return
|
|
}
|
|
|
|
if len(parseTemKey) == 0 {
|
|
continue
|
|
}
|
|
parseTemKeys = append(parseTemKeys, parseTemKey...)
|
|
}
|
|
if err = os.WriteFile(s.OutFile, []byte(strings.Join(parseTemKeys, "\n\n")), os.ModePerm); err != nil {
|
|
fmt.Println("Err WriteFile:", err)
|
|
return
|
|
}
|
|
fmt.Println("ok")
|
|
}
|
|
|
|
func init() {
|
|
var singleSet = new(SingleFile)
|
|
var logsCmd = &cobra.Command{
|
|
Use: "single",
|
|
Short: "single 单文件生成",
|
|
Long: `这是一个文件监视器工具集合`,
|
|
Run: singleSet.run,
|
|
}
|
|
logsCmd.PersistentFlags().StringVar(&singleSet.CheckFile, "file", "", "读取文件")
|
|
logsCmd.PersistentFlags().StringVar(&singleSet.OutFile, "out", "", "输出文件")
|
|
logsCmd.PersistentFlags().StringVar(&singleSet.Cfg, "cfg", "config.yaml", "配置地址")
|
|
|
|
rootCmd.AddCommand(logsCmd)
|
|
}
|