-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (155 loc) · 4.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"text/tabwriter"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"google.golang.org/protobuf/proto"
"github.com/drekle/protoc-gen-client/pkg/option"
protochelpers "github.com/drekle/protoc-gen-client/pkg/option"
"github.com/drekle/protoc-gen-client/pkg/template"
)
const (
Version = "0.1.0"
)
type Config struct {
Version string `json:"version"`
GitRepo string `json:"gitRepository"`
SkipFiles []string `json:"skipFiles"`
}
func GenConfig() ([]byte, error) {
return json.MarshalIndent(&Config{
Version: Version,
}, " ", "\t")
}
type clientGen struct {
Request *plugin.CodeGeneratorRequest
Response *plugin.CodeGeneratorResponse
Parameters map[string]string
Config Config
}
func (runner *clientGen) PrintParameters(w io.Writer) {
const padding = 3
tw := tabwriter.NewWriter(w, 0, 0, padding, ' ', tabwriter.TabIndent)
fmt.Fprintf(tw, "Parameters:\n")
for k, v := range runner.Parameters {
fmt.Fprintf(tw, "%s:\t%s\n", k, v)
}
fmt.Fprintln(tw, "")
tw.Flush()
}
func (runner *clientGen) GenerateCobra(
cliOpt *protochelpers.ClientOptions,
cmdOpt *protochelpers.CommandOptions,
svc *protochelpers.Service,
procedure *protochelpers.Procedure) error {
goPackage := strings.ReplaceAll(svc.Proto.GetPackage(), ".", "/")
outfileName := fmt.Sprintf("cmd/%s/%s/%s.go", goPackage, *svc.Name, strings.ToLower(*procedure.Name))
var buf bytes.Buffer
t := template.NewTemplate()
ex, err := t.Parse(template.CobraCommandTemplate)
if err != nil {
return err
}
err = ex.Execute(&buf, &template.CobraCommandInput{
Service: svc,
GoPBPath: fmt.Sprintf("gen/%s", goPackage),
RepoURL: cliOpt.Repository,
Procedure: procedure,
})
if err != nil {
return err
}
var outFile plugin.CodeGeneratorResponse_File
outFile.Name = &outfileName
content := buf.String()
outFile.Content = &content
runner.Response.File = append(runner.Response.File, &outFile)
return nil
}
func (runner *clientGen) generateCode() error {
// Initialize the output file slice
files := make([]*plugin.CodeGeneratorResponse_File, 0)
runner.Response.File = files
{
for _, protofile := range runner.Request.ProtoFile {
var clientOpt *protochelpers.ClientOptions
if proto.HasExtension(protofile.Options, protochelpers.E_ClientOptions) {
e := proto.GetExtension(protofile.Options, protochelpers.E_ClientOptions)
if cmd, ok := e.(*option.ClientOptions); ok {
clientOpt = cmd
}
} else {
continue
}
// Create a file and append it to the output files
for _, svc := range protochelpers.GetServices(runner.Request, protofile) {
for _, procedure := range svc.Procedures {
if proto.HasExtension(procedure.Options, protochelpers.E_CommandOptions) {
e := proto.GetExtension(procedure.Options, protochelpers.E_CommandOptions)
if cmdOpt, ok := e.(*protochelpers.CommandOptions); ok {
err := runner.GenerateCobra(clientOpt, cmdOpt, svc, procedure)
if err != nil {
return err
}
}
}
}
}
}
}
return nil
}
func main() {
stdInFile := flag.String("stdinFile", "", "A file to use for stdin")
flag.Parse()
// os.Stdin will contain data which will unmarshal into the following object:
// https://godoc.org/github.com/golang/protobuf/protoc-gen-go/plugin#CodeGeneratorRequest
req := &plugin.CodeGeneratorRequest{}
resp := &plugin.CodeGeneratorResponse{}
// Debugging Support
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
if *stdInFile != "" {
data, err = ioutil.ReadFile(*stdInFile)
if err != nil {
panic(err)
}
}
if err := proto.Unmarshal(data, req); err != nil {
panic(err)
}
// You may require more data than what is in the proto files alone. There are a couple ways in which to do this.
// The first is by parameters. Another may be using leading comments in the proto files which I will cover in generateCode.
parameters := req.GetParameter()
// =grpc,import_path=mypackage:.
clientRunner := &clientGen{
Request: req,
Response: resp,
Parameters: make(map[string]string),
}
groupkv := strings.Split(parameters, ",")
for _, element := range groupkv {
kv := strings.Split(element, "=")
if len(kv) > 1 {
clientRunner.Parameters[kv[0]] = kv[1]
}
}
err = clientRunner.generateCode()
if err != nil {
panic(err)
}
marshalled, err := proto.Marshal(resp)
if err != nil {
panic(err)
}
os.Stdout.Write(marshalled)
}