From c37ec344024bb9859e4c32893e5b60e9a09d5c46 Mon Sep 17 00:00:00 2001 From: Rogee Date: Mon, 23 Dec 2024 15:21:34 +0800 Subject: [PATCH] feat: add buf cmd --- cmd/buf.go | 43 ++++++++++++++++++++++++++++++ main.go | 1 + templates/project/buf.gen.yaml.tpl | 29 ++++++++++++++++++++ templates/project/buf.yaml.tpl | 10 +++++++ 4 files changed, 83 insertions(+) create mode 100644 cmd/buf.go create mode 100644 templates/project/buf.gen.yaml.tpl create mode 100644 templates/project/buf.yaml.tpl diff --git a/cmd/buf.go b/cmd/buf.go new file mode 100644 index 0000000..51b7a19 --- /dev/null +++ b/cmd/buf.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "fmt" + "os/exec" + + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func CommandBuf(root *cobra.Command) { + cmd := &cobra.Command{ + Use: "buf", + Short: "run buf commands", + RunE: commandBufE, + } + + root.AddCommand(cmd) +} + +func commandBufE(cmd *cobra.Command, args []string) error { + if _, err := exec.LookPath("buf"); err != nil { + log.Warn("buf 命令不存在,正在安装 buf...") + log.Info("go install github.com/bufbuild/buf/cmd/buf@v1.48.0") + installCmd := exec.Command("go", "install", "github.com/bufbuild/buf/cmd/buf@v1.48.0") + if err := installCmd.Run(); err != nil { + return fmt.Errorf("安装 buf 失败: %v", err) + } + log.Info("buf 安装成功") + + if _, err := exec.LookPath("buf"); err != nil { + return fmt.Errorf("buf 命令不存在,请检查 $PATH") + } + } + + log.Info("buf 命令已存在,正在运行 buf generate...") + generateCmd := exec.Command("buf", "generate") + if err := generateCmd.Run(); err != nil { + return fmt.Errorf("运行 buf generate 失败: %v", err) + } + log.Info("buf generate 运行成功") + return nil +} diff --git a/main.go b/main.go index a6e6b08..8bc9be7 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ func main() { cmd.CommandGen, cmd.CommandSwag, cmd.CommandFmt, + cmd.CommandBuf, } for _, c := range cmds { diff --git a/templates/project/buf.gen.yaml.tpl b/templates/project/buf.gen.yaml.tpl new file mode 100644 index 0000000..5e442b7 --- /dev/null +++ b/templates/project/buf.gen.yaml.tpl @@ -0,0 +1,29 @@ +version: v2 +managed: + enabled: true + override: + - file_option: go_package_prefix + value: {{.ModuleName}}/pkg/proto + +plugins: + - remote: buf.build/protocolbuffers/go + out: gen/go + opt: paths=source_relative + - remote: buf.build/grpc/go + out: gen/go + opt: paths=source_relative,require_unimplemented_servers=false + - remote: buf.build/grpc-ecosystem/gateway + out: gen/go + opt: paths=source_relative + # languages + - remote: buf.build/protocolbuffers/js + out: gen/js + - remote: buf.build/protocolbuffers/php + out: gen/php + # generate openapi documentation for api + - remote: buf.build/grpc-ecosystem/openapiv2 + out: gen/openapiv2 + opt: allow_merge=true,merge_file_name=services + +inputs: + - directory: proto diff --git a/templates/project/buf.yaml.tpl b/templates/project/buf.yaml.tpl new file mode 100644 index 0000000..53da97a --- /dev/null +++ b/templates/project/buf.yaml.tpl @@ -0,0 +1,10 @@ +# For details on buf.yaml configuration, visit https://buf.build/docs/configuration/v2/buf-yaml +version: v2 +modules: + - path: proto +lint: + use: + - STANDARD +breaking: + use: + - FILE