代码拉取完成,页面将自动刷新
package main
import (
"bytes"
"flag"
"io/fs"
"os"
"path/filepath"
"strings"
"gitee.com/xuesongtao/protoc-go-valid/file"
"gitee.com/xuesongtao/protoc-go-valid/log"
)
const (
injectToolSh = "inject_tool.sh"
)
// copyInjectTool 将 inject_tool.sh 脚本移动到 GOPATH 下
func copyInjectTool() {
goPath := os.Getenv("GOPATH")
log.Info("GOPATH: ", goPath)
if goPath == "" {
log.Error("it is not found GOPATH, inject_tool.sh can not use")
return
}
// 判断下是否已经移动了, 如果已经移动就不处理了
dest := file.HandlePath(goPath) + injectToolSh
if _, err := os.Stat(dest); os.IsExist(err) {
return
}
// 复制
if err := file.CopyFile(injectToolSh, dest); err != nil {
log.Error("file.CopyFile is failed, err: ", err)
return
}
}
// createInjectToolSh 文件内容替换
func createInjectToolSh(template, create, old, new string) error {
contentByte, err := os.ReadFile(template)
if err != nil {
return err
}
return os.WriteFile(create, bytes.ReplaceAll(contentByte, []byte(old), []byte(new)), fs.ModePerm)
}
// handleDir 按目录处理
func handleDir(dirPath string) (isHasMatch bool) {
dirs, err := os.ReadDir(dirPath)
if err != nil {
log.Error("os.ReadDir is failed, err: ", err)
return
}
dirPath = file.HandlePath(dirPath)
for _, dir := range dirs {
if dir.IsDir() {
continue
}
isHasMatch = true
filename := dirPath + dir.Name()
_ = handleFile(filename)
}
return
}
// handlePatternFiles 根据路径表达式处理
func handlePatternFiles(pattern string) (isHasMatch bool) {
filenames, err := filepath.Glob(pattern)
if err != nil {
log.Error("filepath.Glob is failed, err: ", err)
return
}
for _, filename := range filenames {
isHasMatch = true
_ = handleFile(filename)
}
return
}
// handleFile 处理单个文件
func handleFile(filename string) (isHasMatch bool) {
// 只处理 .go 文件
if !strings.HasSuffix(filename, ".go") {
return
}
isHasMatch = true
log.Infof("parsing file %q for inject tag comments", filename)
areas, err := file.ParseFile(filename)
if err != nil {
log.Error("file.ParseFile is failed, err: ", err)
return
}
// log.Infof("areas: %+v", areas)
if err = file.WriteFile(filename, areas); err != nil {
log.Error("file.WriteFile is failed, err: ", err)
return
}
log.Infof("file: %q is inject tag is success", filename)
return
}
func main() {
var (
initProject bool
inputDir, inputPattern, inputFile string
)
flag.BoolVar(&initProject, "init", false, "是否初始化项目, 如: protoc-go-valid -init=\"true\"")
flag.StringVar(&inputDir, "d", "", "注入的目录, 如: protoc-go-valid -d \"./proto\"")
flag.StringVar(&inputPattern, "p", "", "注入匹配到的多个文件, 如: protoc-go-valid -p \"./*.pb.go\"")
flag.StringVar(&inputFile, "f", "", "注入的单个文件, 如: protoc-go-valid -f \"xxx.pb.go\"")
flag.Parse()
// 判断是否初始化
if initProject {
copyInjectTool()
return
}
var isHasMatch bool
if inputDir != "" {
isHasMatch = handleDir(inputDir)
} else if inputPattern != "" {
isHasMatch = handlePatternFiles(inputPattern)
} else {
isHasMatch = handleFile(inputFile)
}
if !isHasMatch {
log.Error("it is not matched files, see: -help")
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。