2021-02-22 18:11:08 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/compiler/protogen"
|
|
|
|
)
|
|
|
|
|
2021-02-22 23:42:49 +03:00
|
|
|
var (
|
|
|
|
flags flag.FlagSet
|
|
|
|
flagDebug *bool
|
|
|
|
flagComponents *string
|
|
|
|
flagPaths *string
|
|
|
|
flagModule *string
|
|
|
|
)
|
2021-02-22 18:11:08 +03:00
|
|
|
|
2021-02-22 23:42:49 +03:00
|
|
|
func init() {
|
|
|
|
flagDebug = flags.Bool("debug", false, "")
|
|
|
|
flagComponents = flags.String("components", "micro", "")
|
|
|
|
flagPaths = flag.String("paths", "", "")
|
|
|
|
flagModule = flag.String("module", "", "")
|
|
|
|
}
|
2021-02-22 18:11:08 +03:00
|
|
|
|
2021-02-22 23:42:49 +03:00
|
|
|
func main() {
|
2021-02-22 18:11:08 +03:00
|
|
|
opts := &protogen.Options{
|
|
|
|
ParamFunc: flags.Set,
|
|
|
|
}
|
|
|
|
|
2021-02-22 23:42:49 +03:00
|
|
|
g := &Generator{}
|
2021-02-22 18:11:08 +03:00
|
|
|
|
|
|
|
opts.Run(g.Generate)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Generator struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Protoc passes a slice of File structs for us to process
|
2021-02-22 23:42:49 +03:00
|
|
|
for _, component := range strings.Split(*flagComponents, "|") {
|
2021-02-22 18:11:08 +03:00
|
|
|
switch component {
|
|
|
|
case "micro":
|
|
|
|
err = g.microGenerate(component, plugin)
|
|
|
|
case "http":
|
|
|
|
err = g.httpGenerate(component, plugin)
|
|
|
|
case "grpc", "rpc":
|
|
|
|
err = g.rpcGenerate(component, plugin)
|
|
|
|
case "openapi", "swagger":
|
|
|
|
err = g.openapiGenerate(component, plugin)
|
2021-02-22 23:42:49 +03:00
|
|
|
case "gorilla":
|
|
|
|
err = g.gorillaGenerate(component, plugin)
|
|
|
|
case "chi":
|
|
|
|
err = g.chiGenerate(component, plugin)
|
2021-02-22 18:11:08 +03:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown component: %s", component)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
plugin.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|