Ai
1 Star 1 Fork 0

kade/library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cmd.go 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
kade 提交于 2024-08-15 17:20 +08:00 . v2.1.3 cobra command successful
package generate
import (
"os"
"path/filepath"
"strings"
"gitee.com/go-kade/library/v2/cmd/generate/enum"
"github.com/spf13/cobra"
)
// EnumCmd 枚举生成器
var Cmd = &cobra.Command{
Use: "enum",
Short: "枚举生成器",
Long: "枚举生成器",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
return
}
var matchedFiles []string
for _, arg := range args {
filesName, err := filepath.Glob(arg)
cobra.CheckErr(err)
// 只匹配Go源码文件
if strings.HasSuffix(arg, ".go") {
matchedFiles = append(matchedFiles, filesName...)
}
if len(matchedFiles) == 0 {
return
}
for _, path := range matchedFiles {
// 生成代码
code, err := enum.G.Generate(path)
cobra.CheckErr(err)
if len(code) == 0 {
continue
}
var genFile = ""
if strings.HasSuffix(path, ".pb.go") {
genFile = strings.ReplaceAll(path, ".pb.go", "_enum.pb.go")
} else {
genFile = strings.ReplaceAll(path, ".go", "_enum.go")
}
// 写入文件
err = os.WriteFile(genFile, code, 0644)
cobra.CheckErr(err)
}
}
},
}
func init() {
Cmd.PersistentFlags().BoolVarP(&enum.G.Marshal, "marshal", "m", false, "is generate json MarshalJSON and UnmarshalJSON method")
Cmd.PersistentFlags().BoolVarP(&enum.G.ProtobufExt, "protobuf_ext", "p", false, "is generate protobuf extention method")
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/go-kade/library.git
git@gitee.com:go-kade/library.git
go-kade
library
library
v2.1.3

搜索帮助