当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 3

AIDY/protoc-go-valid
暂停

forked from A-涛/protoc-go-valid 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.go 3.20 KB
一键复制 编辑 原始数据 按行查看 历史
AIDY 提交于 2024-09-24 14:52 . 修改绝对路径
package main
import (
"bytes"
"flag"
"io/fs"
"os"
"path/filepath"
"strings"
"gitee.com/aidyfocus/protoc-go-valid/file"
"gitee.com/aidyfocus/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")
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/aidyfocus/protoc-go-valid.git
git@gitee.com:aidyfocus/protoc-go-valid.git
aidyfocus
protoc-go-valid
protoc-go-valid
v1.6.19

搜索帮助