2016-11-04 21:45:25 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2016-11-07 08:45:02 +01:00
|
|
|
"log"
|
2016-11-04 21:45:25 +01:00
|
|
|
"os"
|
2016-11-07 08:45:02 +01:00
|
|
|
"strings"
|
2016-11-04 21:45:25 +01:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/golang/protobuf/protoc-gen-go/generator"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
g := generator.New()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
g.Error(err, "reading input")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := proto.Unmarshal(data, g.Request); err != nil {
|
|
|
|
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 08:45:02 +01:00
|
|
|
// Parse parameters
|
|
|
|
templateDir := "./templates"
|
|
|
|
debug := false
|
|
|
|
if parameter := g.Request.GetParameter(); parameter != "" {
|
|
|
|
for _, param := range strings.Split(parameter, ",") {
|
|
|
|
parts := strings.Split(param, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
log.Printf("Err: invalid parameter: %q", param)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch parts[0] {
|
|
|
|
case "template_dir":
|
|
|
|
templateDir = parts[1]
|
|
|
|
break
|
|
|
|
case "debug":
|
2016-12-14 11:08:51 +01:00
|
|
|
switch strings.ToLower(parts[1]) {
|
|
|
|
case "true", "t":
|
2016-11-07 08:45:02 +01:00
|
|
|
debug = true
|
2016-12-14 11:08:51 +01:00
|
|
|
case "false", "f":
|
|
|
|
default:
|
2016-11-07 08:45:02 +01:00
|
|
|
log.Printf("Err: invalid value for debug: %q", parts[1])
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
log.Printf("Err: unknown parameter: %q", param)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the encoders
|
2016-11-04 21:45:25 +01:00
|
|
|
for _, file := range g.Request.GetProtoFile() {
|
|
|
|
for _, service := range file.GetService() {
|
2016-11-07 08:45:02 +01:00
|
|
|
encoder := NewGenericTemplateBasedEncoder(templateDir, service, file, debug)
|
2016-11-04 21:45:25 +01:00
|
|
|
g.Response.File = append(g.Response.File, encoder.Files()...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the protobufs
|
|
|
|
g.GenerateAllFiles()
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|