Ai
1 Star 0 Fork 0

kade/mcube

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cmd.go 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
kadegolang 提交于 2023-12-13 17:31 +08:00 . copy
package generate
import (
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"gitee.com/go-kade/mcube/cmd/generate/enum"
)
// EnumCmd 枚举生成器
var Cmd = &cobra.Command{
Use: "enum",
Short: "枚举生成器",
Long: `枚举生成器`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
return
}
matchedFiles := []string{}
for _, v := range args {
files, err := filepath.Glob(v)
cobra.CheckErr(err)
// 只匹配Go源码文件
if strings.HasSuffix(v, ".go") {
matchedFiles = append(matchedFiles, files...)
}
}
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 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/go-kade/mcube.git
git@gitee.com:go-kade/mcube.git
go-kade
mcube
mcube
1225d9a674f1

搜索帮助