-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
131 lines (106 loc) · 3.11 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
//go:build generator
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
)
var (
code = flag.Bool("code", true, "Generate code")
buildTags = flag.String("build-tags", "", "Comma separated list of build tags expected in the source files that will be added to the generated code") //nolint:lll
scheme = flag.Bool("scheme", false, "Generate Avro scheme")
schemeNamespace = flag.String("scheme-namespace", "gateway.nginx.org", "Scheme namespace; required when -scheme is set") //nolint:lll
schemeProtocol = flag.String("scheme-protocol", "", "Scheme protocol; required when -scheme is set")
schemeDataFabricDataType = flag.String("scheme-df-datatype", "", "Scheme data fabric data type; required when -scheme is set") //nolint:lll
typeName = flag.String("type", "", "Type to generate; required")
)
func exitWithError(err error) {
fmt.Fprintln(os.Stderr, "error: "+err.Error())
os.Exit(1)
}
func exitWithUsage() {
flag.Usage()
os.Exit(1)
}
func validateFlags() {
if *typeName == "" {
exitWithUsage()
}
if *scheme {
if *schemeNamespace == "" {
exitWithUsage()
}
if *schemeProtocol == "" {
exitWithUsage()
}
if *schemeDataFabricDataType == "" {
exitWithUsage()
}
}
}
func main() {
flag.Parse()
validateFlags()
pkgName := os.Getenv("GOPACKAGE")
if pkgName == "" {
exitWithError(errors.New("GOPACKAGE is not set"))
}
var buildFlags []string
if *buildTags != "" {
buildFlags = []string{"-tags=" + *buildTags}
}
cfg := parsingConfig{
pkgName: pkgName,
typeName: *typeName,
buildFlags: buildFlags,
}
result, err := parse(cfg)
if err != nil {
exitWithError(fmt.Errorf("failed to parse struct: %w", err))
}
fmt.Printf("Successfully parsed struct %s\n", *typeName)
if *code {
fmt.Println("Generating code")
fileName := strings.ToLower(*typeName) + "_attributes_generated.go"
file, err := os.Create(fileName)
if err != nil {
exitWithError(fmt.Errorf("failed to create file: %w", err))
}
defer file.Close()
var codeGenBuildTags string
if *buildTags != "" {
codeGenBuildTags = strings.ReplaceAll(*buildTags, ",", " && ")
}
codeCfg := codeGenConfig{
packagePath: result.packagePath,
typeName: *typeName,
schemeDataType: *schemeDataFabricDataType,
fields: result.fields,
buildTags: codeGenBuildTags,
}
if err := generateCode(file, codeCfg); err != nil {
exitWithError(fmt.Errorf("failed to generate code: %w", err))
}
}
if *scheme {
fmt.Println("Generating scheme")
fileName := strings.ToLower(*typeName) + ".avdl"
file, err := os.Create(fileName)
if err != nil {
exitWithError(fmt.Errorf("failed to create file: %w", err))
}
defer file.Close()
schemeCfg := schemeGenConfig{
namespace: *schemeNamespace,
protocol: *schemeProtocol,
dataFabricDataType: *schemeDataFabricDataType,
record: *typeName,
fields: result.fields,
}
if err := generateScheme(file, schemeCfg); err != nil {
exitWithError(fmt.Errorf("failed to generate scheme: %w", err))
}
}
}