80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.apinb.com/bsm-sdk/core/utils"
|
|
"google.golang.org/protobuf/compiler/protogen"
|
|
)
|
|
|
|
var (
|
|
flags flag.FlagSet
|
|
path = flags.String("path", "", "输出目录,默认为当前目录")
|
|
)
|
|
|
|
func main() {
|
|
opts := protogen.Options{
|
|
ParamFunc: func(name, value string) error {
|
|
//fmt.Println(name, value)
|
|
flags.Set(name, value)
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
opts.Run(func(gen *protogen.Plugin) error {
|
|
// set output directory
|
|
if *path == "" {
|
|
*path, _ = os.Getwd()
|
|
*path = filepath.Join(*path, "test/http")
|
|
}
|
|
|
|
if !utils.PathExists(*path) {
|
|
os.MkdirAll(*path, 0755)
|
|
}
|
|
|
|
for _, file := range gen.Files {
|
|
if len(file.Services) == 0 {
|
|
continue
|
|
}
|
|
if err := generateFiles(gen, file); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func generateFiles(gen *protogen.Plugin, file *protogen.File) error {
|
|
for _, service := range file.Services {
|
|
for _, method := range service.Methods {
|
|
sm := fmt.Sprintf("==> %s.%s", service.GoName, method.GoName)
|
|
// 生成测试文件
|
|
if err := generateHttpTestFile(gen, file, service, method); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(sm) // 打印服务和方法名
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func generateHttpTestFile(gen *protogen.Plugin, file *protogen.File, service *protogen.Service, method *protogen.Method) error {
|
|
filename := fmt.Sprintf("%s_%s_test.json", service.GoName, method.GoName)
|
|
g := gen.NewGeneratedFile(filepath.Join(*path, filename), "")
|
|
g.P("{")
|
|
g.P(` "name": "` + method.GoName + `",`)
|
|
g.P(` "url": "http://localhost:8080/` + service.GoName + `/` + method.GoName + `",`)
|
|
g.P(` "method": "POST",`)
|
|
g.P(` "headers": {`)
|
|
g.P(` "Content-Type": "application/json"`)
|
|
g.P(` },`)
|
|
g.P(` "body": {`)
|
|
g.P(` }`)
|
|
g.P("}")
|
|
return nil
|
|
}
|