-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (146 loc) · 4.36 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
177
178
179
package main
import (
"fmt"
"log"
"os"
"github.com/alexj212/gox"
"github.com/alexj212/gox/utilx"
"github.com/droundy/goopt"
)
//
// go run protogen /home/alexj/projects/rooms/submodules/protocol-definitions/centralserver/csserver.proto Packet /home/alexj/projects/rooms/proto/cs/mapping.go
//
var (
// BuildDate date string of when build was performed filled in by -X compile flag
BuildDate string
// GitRepo string of the git repo url when build was performed filled in by -X compile flag
GitRepo string
// BuiltBy date string of who performed build filled in by -X compile flag
BuiltBy string
// CommitDate date string of when commit of the build was performed filled in by -X compile flag
CommitDate string
// Branch string of branch in the git repo filled in by -X compile flag
Branch string
// LatestCommit date string of when build was performed filled in by -X compile flag
LatestCommit string
// Version string of build filled in by -X compile flag
Version string
)
var (
formatCode = goopt.Flag([]string{"--format"}, nil, "Format code after generating", "")
forceOverwrite = goopt.Flag([]string{"--overwrite", "-o"}, nil, "Format code after generating", "")
fieldPrefix = goopt.String([]string{"--fieldPrefix"}, "", "Field Prefix to use - defaults to enumName")
protoFile = goopt.String([]string{"--proto"}, "", "protobuf file")
jsonFile = goopt.String([]string{"--json"}, "", "json mappings file")
templateFileName = goopt.String([]string{"--template"}, "", "Template file to use")
enumName = goopt.String([]string{"--enum"}, "", "Enum Name")
)
func init() {
// Setup goopts
goopt.Description = func() string {
return fmt.Sprintf("ProtoGen")
}
goopt.Version = fmt.Sprintf("v%v - GitCommit: %v - BuildDate: %v", Version, LatestCommit, BuildDate)
goopt.Summary = `
` //Parse options
goopt.Parse(nil)
} // init
func main() {
exitApp, err := gox.HandleHistory()
if err != nil {
fmt.Printf("Error handling history: %v\n", err)
os.Exit(1)
}
if exitApp {
fmt.Printf("exitApp: %v\n", exitApp)
os.Exit(1)
}
if len(goopt.Args) != 1 {
fmt.Printf("must specify arguments\n\n")
fmt.Printf(goopt.Usage())
os.Exit(1)
}
outputFile := goopt.Args[0]
codeTemplate := messageMapperTemplate
if *templateFileName != "" {
templateBytes, err := os.ReadFile(*templateFileName)
if err != nil {
log.Printf("Failed to load template file: %s, err: %v", *templateFileName, err)
os.Exit(1)
return
}
codeTemplate = string(templateBytes)
log.Printf("Using template file: %s", *templateFileName)
}
if *protoFile == "" && *jsonFile == "" {
fmt.Fprintf(os.Stderr, "No event list loaded, specify --proto <file> or --json <file> \n")
os.Exit(1)
return
}
var parsed *MessageMapper
if *protoFile != "" {
if !fileExists(*protoFile) {
fmt.Fprintf(os.Stderr, "Protobuf file %v does not exist\n", protoFile)
os.Exit(1)
return
}
if *fieldPrefix == "" {
fieldPrefix = enumName
}
parsed, err = ParseProtoBuf(*protoFile, *enumName, *fieldPrefix)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing: %v\n", err)
os.Exit(1)
return
}
}
if *jsonFile != "" {
parsed, err = utilx.LoadJson(*jsonFile, parsed)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing: %v\n", err)
os.Exit(1)
return
}
}
if parsed == nil {
fmt.Fprintf(os.Stderr, "No event list loaded, specify --proto <file> \n")
os.Exit(1)
return
}
code, err := Generate(codeTemplate, parsed, false)
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating code: %v\n", err)
os.Exit(1)
return
}
exists := false
if _, err := os.Stat(outputFile); err == nil {
exists = true
}
if exists && !*forceOverwrite {
fmt.Fprintf(os.Stderr, "Output file exists: %s\n", outputFile)
os.Exit(1)
return
}
f, err := os.Create(outputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating blob file:%v\n", err)
os.Exit(1)
}
defer f.Close()
err = os.WriteFile(outputFile, code, os.ModePerm)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing blob file error: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "File Written: %v\n", outputFile)
}
// fileExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}