-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate-merge-relay-opts.go
145 lines (111 loc) · 3.46 KB
/
generate-merge-relay-opts.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
// This script will generate a helper func for generating connection options.
// This func is used by the CLI to avoid having to write out a switch manually.
package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"sort"
"strings"
)
const (
grepFile = "build/go/protos/opts/ps_opts_relay.pb.go"
outputFile = "./build/go/protos/opts/batch_merge_relay_opts.pb.go"
)
type MergeMapping struct {
Name string
Capitalized string
}
func main() {
path, err := os.Getwd()
if err != nil {
log.Fatalf("unable to get working dir: %s", err)
}
grepArgs := [][]string {
{`\*RelayGroup.\+protobuf:`, path + "/" + grepFile},
}
// key = backend name
mappings := make(map[string]MergeMapping, 0)
for _, cmd := range grepArgs {
out, err := exec.Command("grep", cmd ... ).CombinedOutput()
if err != nil {
log.Fatalf("unable to exec grep\nOutput: %s\nError: %s", string(out), err)
}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
if len(line) == 0 {
continue
}
r, err := regexp.Compile(`\t?([a-zA-Z0-9]+)\s+\*RelayGroup(.+)Options\s+.+`)
if err != nil {
log.Fatalf("unable to compile regex: %s", err)
}
found := r.FindStringSubmatch(line)
if len(found) != 3 {
log.Fatalf("unexpected number of regex matches '%d'", len(found))
}
normalizedBackendName := strings.ToLower(found[1])
normalizedBackendName = strings.Replace(normalizedBackendName, " ", "", -1)
bm := MergeMapping{
Name: found[1],
Capitalized: found[2],
}
mappings[normalizedBackendName] = bm
}
}
if len(mappings) == 0 {
log.Fatalf("something went wrong - 0 mappings found for relays")
}
fileContents, err := createRelayCode(mappings)
if err != nil {
log.Fatalf("unable to create file contents: %s", err)
}
if err := writeMergeCode(outputFile, fileContents); err != nil {
log.Fatalf("unable to write file '%s': %s", outputFile, err)
}
fmt.Printf("Successfully generated file '%s' with '%d' relays\n", outputFile, len(mappings))
}
func createRelayCode(mappings map[string]MergeMapping) ([]byte, error) {
contents := `// Code generated by generate-merge-relay-opts.go. DO NOT EDIT.
package opts
import (
"errors"
)
func MergeRelayOptions(backend string, relayOpts *RelayOptions, createRelayOpts *CreateRelayOptions) error {
switch backend {
`
// We want to have the same generation order (to not trigger repo updates
keys := make([]string, 0)
for backendName, _ := range mappings {
keys = append(keys, backendName)
}
sort.Strings(keys)
for _, key := range keys {
mapping, ok := mappings[key]
if !ok {
log.Fatalf("unable to find mapping for backend '%s'", key)
}
caseEntry := fmt.Sprintf("\tcase \"%s\":\n", key)
caseEntry += fmt.Sprintf("\t\trelayOpts.%s = &RelayGroup%sOptions{Args: createRelayOpts.%s}\n", mapping.Name, mapping.Capitalized, mapping.Name)
contents += caseEntry
}
// Close the switch and func
contents += fmt.Sprint("\tdefault:\n\t\treturn errors.New(\"unknown backend\")\n\t}\n\n")
contents += fmt.Sprint("\treturn nil\n}\n")
return []byte(contents), nil
}
func writeMergeCode(filename string, contents []byte) error {
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("unable to create file '%s': %s", filename, err)
}
if _, err := f.Write(contents); err != nil {
return fmt.Errorf("unable to write to file '%s': %s", filename, err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("unable to close file '%s': %s", filename, err)
}
return nil
}