package patch
import (
"bytes"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
func StripParam(req *pluginpb.CodeGeneratorRequest, p string) {
if req.Parameter == nil {
return
}
v := stripParam(*req.Parameter, p)
req.Parameter = &v
}
func stripParam(s, p string) string {
var b strings.Builder
for _, param := range strings.Split(s, ",") {
if strings.SplitN(param, "=", 2)[0] != p {
if b.Len() > 0 {
b.WriteString(",")
}
b.WriteString(param)
}
}
return b.String()
}
func RunPlugin(plugin string, req *pluginpb.CodeGeneratorRequest, stderr io.Writer) (*pluginpb.CodeGeneratorResponse, error) {
if stderr == nil {
stderr = os.Stderr
}
b, err := proto.Marshal(req)
if err != nil {
return nil, err
}
var buf bytes.Buffer
cmd := exec.Command("protoc-gen-" + plugin)
cmd.Stdin = bytes.NewReader(b)
cmd.Stdout = &buf
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
return nil, err
}
var res pluginpb.CodeGeneratorResponse
err = proto.Unmarshal(buf.Bytes(), &res)
if err != nil {
return nil, err
}
return &res, nil
}
func ReadRequest(r io.Reader) (*pluginpb.CodeGeneratorRequest, error) {
in, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
req := &pluginpb.CodeGeneratorRequest{}
err = proto.Unmarshal(in, req)
if err != nil {
return nil, err
}
return req, nil
}
func WriteResponse(w io.Writer, res *pluginpb.CodeGeneratorResponse) error {
out, err := proto.Marshal(res)
if err != nil {
return err
}
_, err = w.Write(out)
return err
}