feat: add buf cmd
This commit is contained in:
43
cmd/buf.go
Normal file
43
cmd/buf.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
1
main.go
1
main.go
@@ -18,6 +18,7 @@ func main() {
|
|||||||
cmd.CommandGen,
|
cmd.CommandGen,
|
||||||
cmd.CommandSwag,
|
cmd.CommandSwag,
|
||||||
cmd.CommandFmt,
|
cmd.CommandFmt,
|
||||||
|
cmd.CommandBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cmds {
|
for _, c := range cmds {
|
||||||
|
|||||||
29
templates/project/buf.gen.yaml.tpl
Normal file
29
templates/project/buf.gen.yaml.tpl
Normal file
@@ -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
|
||||||
10
templates/project/buf.yaml.tpl
Normal file
10
templates/project/buf.yaml.tpl
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user