2021-01-08 22:22:43 +03:00
|
|
|
package main
|
2016-11-04 23:45:25 +03:00
|
|
|
|
|
|
|
import (
|
2021-01-09 19:44:21 +03:00
|
|
|
"fmt"
|
|
|
|
"go/format"
|
2016-11-04 23:45:25 +03:00
|
|
|
"io/ioutil"
|
2016-11-07 10:45:02 +03:00
|
|
|
"log"
|
2020-12-31 04:10:22 +03:00
|
|
|
"net/url"
|
2016-11-04 23:45:25 +03:00
|
|
|
"os"
|
2016-11-07 10:45:02 +03:00
|
|
|
"strings"
|
2016-11-04 23:45:25 +03:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/protoc-gen-go/generator"
|
2019-01-20 08:58:14 +03:00
|
|
|
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
|
2017-05-18 20:44:05 +03:00
|
|
|
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
|
2021-01-08 22:22:43 +03:00
|
|
|
pgghelpers "github.com/unistack-org/protoc-gen-micro/helpers"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2017-05-18 20:44:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
registry *ggdescriptor.Registry // some helpers need access to registry
|
2016-11-04 23:45:25 +03:00
|
|
|
)
|
|
|
|
|
2017-12-25 11:17:09 +03:00
|
|
|
const (
|
|
|
|
boolTrue = "true"
|
|
|
|
boolFalse = "false"
|
|
|
|
)
|
|
|
|
|
2021-02-02 07:44:06 +03:00
|
|
|
var (
|
|
|
|
templateDir = ""
|
|
|
|
templateRepo = ""
|
|
|
|
destinationDir = "."
|
|
|
|
debug = false
|
|
|
|
all = false
|
|
|
|
singlePackageMode = false
|
|
|
|
fileMode = false
|
|
|
|
components = []string{"micro", "grpc"}
|
|
|
|
)
|
|
|
|
|
2016-11-04 23:45:25 +03:00
|
|
|
func main() {
|
|
|
|
g := generator.New()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
g.Error(err, "reading input")
|
|
|
|
}
|
|
|
|
|
2017-12-25 11:17:09 +03:00
|
|
|
if err = proto.Unmarshal(data, g.Request); err != nil {
|
2016-11-04 23:45:25 +03:00
|
|
|
g.Error(err, "parsing input proto")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(g.Request.FileToGenerate) == 0 {
|
|
|
|
g.Fail("no files to generate")
|
|
|
|
}
|
|
|
|
|
|
|
|
g.CommandLineParameters(g.Request.GetParameter())
|
|
|
|
|
2016-11-07 10:45:02 +03:00
|
|
|
// Parse parameters
|
|
|
|
if parameter := g.Request.GetParameter(); parameter != "" {
|
|
|
|
for _, param := range strings.Split(parameter, ",") {
|
|
|
|
parts := strings.Split(param, "=")
|
|
|
|
switch parts[0] {
|
|
|
|
case "template_dir":
|
|
|
|
templateDir = parts[1]
|
2016-12-15 18:25:44 +03:00
|
|
|
case "destination_dir":
|
|
|
|
destinationDir = parts[1]
|
2017-05-18 20:44:05 +03:00
|
|
|
case "single-package-mode":
|
|
|
|
switch strings.ToLower(parts[1]) {
|
2017-12-25 11:17:09 +03:00
|
|
|
case boolTrue, "t":
|
2017-05-18 20:44:05 +03:00
|
|
|
singlePackageMode = true
|
2017-12-25 11:17:09 +03:00
|
|
|
case boolFalse, "f":
|
2017-05-18 20:44:05 +03:00
|
|
|
default:
|
|
|
|
log.Printf("Err: invalid value for single-package-mode: %q", parts[1])
|
|
|
|
}
|
2016-11-07 10:45:02 +03:00
|
|
|
case "debug":
|
2016-12-14 13:08:51 +03:00
|
|
|
switch strings.ToLower(parts[1]) {
|
2017-12-25 11:17:09 +03:00
|
|
|
case boolTrue, "t":
|
2016-11-07 10:45:02 +03:00
|
|
|
debug = true
|
2017-12-25 11:17:09 +03:00
|
|
|
case boolFalse, "f":
|
2016-12-14 13:08:51 +03:00
|
|
|
default:
|
2016-11-07 10:45:02 +03:00
|
|
|
log.Printf("Err: invalid value for debug: %q", parts[1])
|
|
|
|
}
|
2017-01-12 16:46:43 +03:00
|
|
|
case "all":
|
|
|
|
switch strings.ToLower(parts[1]) {
|
2017-12-25 11:17:09 +03:00
|
|
|
case boolTrue, "t":
|
2017-01-12 16:46:43 +03:00
|
|
|
all = true
|
2017-12-25 11:17:09 +03:00
|
|
|
case boolFalse, "f":
|
2017-01-12 16:46:43 +03:00
|
|
|
default:
|
|
|
|
log.Printf("Err: invalid value for debug: %q", parts[1])
|
|
|
|
}
|
2019-03-29 20:35:55 +03:00
|
|
|
case "file-mode":
|
|
|
|
switch strings.ToLower(parts[1]) {
|
|
|
|
case boolTrue, "t":
|
|
|
|
fileMode = true
|
|
|
|
case boolFalse, "f":
|
|
|
|
default:
|
|
|
|
log.Printf("Err: invalid value for file-mode: %q", parts[1])
|
|
|
|
}
|
2020-12-31 04:10:22 +03:00
|
|
|
case "template_repo":
|
|
|
|
_, err := url.Parse(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Err: invalid value for template_repo: %q", parts[1])
|
|
|
|
}
|
|
|
|
templateRepo = parts[1]
|
2021-01-13 08:08:55 +03:00
|
|
|
case "components":
|
|
|
|
_, err := url.Parse(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Err: invalid value for components: %q", parts[1])
|
|
|
|
}
|
|
|
|
components = strings.Split(parts[1], "|")
|
2021-01-08 22:22:43 +03:00
|
|
|
case "paths":
|
|
|
|
// TODO: handle paths=source_relative
|
2016-11-07 10:45:02 +03:00
|
|
|
default:
|
|
|
|
log.Printf("Err: unknown parameter: %q", param)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 13:48:50 +03:00
|
|
|
tmplMap := make(map[string]*plugin_go.CodeGeneratorResponse_File)
|
|
|
|
concatOrAppend := func(file *plugin_go.CodeGeneratorResponse_File) {
|
2017-05-18 20:44:05 +03:00
|
|
|
if val, ok := tmplMap[file.GetName()]; ok {
|
|
|
|
*val.Content += file.GetContent()
|
2017-01-17 13:48:50 +03:00
|
|
|
} else {
|
2017-05-18 20:44:05 +03:00
|
|
|
tmplMap[file.GetName()] = file
|
2017-01-17 13:48:50 +03:00
|
|
|
g.Response.File = append(g.Response.File, file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:44:05 +03:00
|
|
|
if singlePackageMode {
|
|
|
|
registry = ggdescriptor.NewRegistry()
|
2017-05-19 11:02:40 +03:00
|
|
|
pgghelpers.SetRegistry(registry)
|
2017-12-25 11:17:09 +03:00
|
|
|
if err = registry.Load(g.Request); err != nil {
|
2017-05-18 20:44:05 +03:00
|
|
|
g.Error(err, "registry: failed to load the request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:44:06 +03:00
|
|
|
if templateDir == "" && templateRepo != "" {
|
2020-12-31 04:10:22 +03:00
|
|
|
if templateDir, err = ioutil.TempDir("", "gen-*"); err != nil {
|
|
|
|
g.Error(err, "failed to create tmp dir")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(templateDir); err != nil {
|
|
|
|
g.Error(err, "failed to remove tmp dir")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-08 22:22:43 +03:00
|
|
|
if templateRepo != "" {
|
|
|
|
if err = clone(templateRepo, templateDir); err != nil {
|
|
|
|
g.Error(err, "failed to clone repo")
|
|
|
|
}
|
2020-12-31 04:10:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 11:07:05 +03:00
|
|
|
filesToGenerate := map[string]struct{}{}
|
|
|
|
for _, file := range g.Request.FileToGenerate {
|
|
|
|
filesToGenerate[file] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2016-11-07 10:45:02 +03:00
|
|
|
// Generate the encoders
|
2016-11-04 23:45:25 +03:00
|
|
|
for _, file := range g.Request.GetProtoFile() {
|
2021-01-13 11:07:05 +03:00
|
|
|
// Ignore files not in g.Request.FileToGenerate.
|
|
|
|
if _, ok := filesToGenerate[*file.Name]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
2017-01-12 16:46:43 +03:00
|
|
|
if all {
|
2017-05-18 20:44:05 +03:00
|
|
|
if singlePackageMode {
|
2017-12-25 11:17:09 +03:00
|
|
|
if _, err = registry.LookupFile(file.GetName()); err != nil {
|
2017-05-18 20:44:05 +03:00
|
|
|
g.Error(err, "registry: failed to lookup file %q", file.GetName())
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 16:46:43 +03:00
|
|
|
encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir)
|
2017-01-17 13:48:50 +03:00
|
|
|
for _, tmpl := range encoder.Files() {
|
|
|
|
concatOrAppend(tmpl)
|
|
|
|
}
|
|
|
|
|
2017-01-12 16:46:43 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:35:55 +03:00
|
|
|
if fileMode {
|
2019-03-30 02:24:47 +03:00
|
|
|
if s := file.GetService(); s != nil && len(s) > 0 {
|
2019-03-29 20:35:55 +03:00
|
|
|
encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir)
|
|
|
|
for _, tmpl := range encoder.Files() {
|
|
|
|
concatOrAppend(tmpl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-11-04 23:45:25 +03:00
|
|
|
for _, service := range file.GetService() {
|
2017-01-12 16:46:43 +03:00
|
|
|
encoder := NewGenericServiceTemplateBasedEncoder(templateDir, service, file, debug, destinationDir)
|
2017-01-17 13:48:50 +03:00
|
|
|
for _, tmpl := range encoder.Files() {
|
|
|
|
concatOrAppend(tmpl)
|
|
|
|
}
|
2016-11-04 23:45:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the protobufs
|
|
|
|
g.GenerateAllFiles()
|
|
|
|
|
2021-01-09 19:44:21 +03:00
|
|
|
for _, f := range g.Response.File {
|
|
|
|
fdata, err := format.Source([]byte(*f.Content))
|
|
|
|
if err != nil {
|
|
|
|
g.Error(err, fmt.Sprintf("failed to format output: %s", *f.Content))
|
|
|
|
}
|
|
|
|
*f.Content = string(fdata)
|
|
|
|
}
|
|
|
|
|
2016-11-04 23:45:25 +03:00
|
|
|
data, err = proto.Marshal(g.Response)
|
|
|
|
if err != nil {
|
|
|
|
g.Error(err, "failed to marshal output proto")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stdout.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
g.Error(err, "failed to write output proto")
|
|
|
|
}
|
|
|
|
}
|