forked from tylertreat/frugal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (147 loc) · 3.54 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
package main
import (
"fmt"
"os"
"sort"
"github.com/Workiva/frugal/compiler"
"github.com/Workiva/frugal/compiler/generator"
"github.com/Workiva/frugal/compiler/globals"
"github.com/Workiva/frugal/compiler/parser"
"github.com/urfave/cli"
)
const defaultTopicDelim = "."
var (
help bool
gen string
out string
delim string
audit string
recurse bool
verbose bool
version bool
)
func main() {
app := cli.NewApp()
app.Name = "frugal"
app.Usage = "a tool for code generation"
app.Version = globals.Version
app.HideVersion = true
app.HideHelp = true
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "help, h",
Usage: "show help",
Destination: &help,
},
cli.StringFlag{
Name: "gen",
Usage: genUsage(),
Destination: &gen,
},
cli.StringFlag{
Name: "out",
Usage: "set the output location for generated files (no gen-* folder will be created)",
Destination: &out,
},
cli.StringFlag{
Name: "delim",
Value: defaultTopicDelim,
Usage: "set the delimiter for pub/sub topic tokens",
Destination: &delim,
},
cli.BoolFlag{
Name: "recurse, r",
Usage: "generate included files",
Destination: &recurse,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "verbose mode",
Destination: &verbose,
},
cli.BoolFlag{
Name: "version",
Usage: "print the version",
Destination: &version,
}, cli.StringFlag{
Name: "audit",
Usage: "frugal file to run audit against",
Destination: &audit,
},
}
app.Action = func(c *cli.Context) error {
if help {
cli.ShowAppHelp(c)
os.Exit(0)
}
if version {
cli.ShowVersion(c)
os.Exit(0)
}
if len(c.Args()) == 0 {
fmt.Printf("Usage: %s [options] file\n\n", app.Name)
fmt.Printf("Use %s -help for a list of options\n", app.Name)
os.Exit(1)
}
if gen == "" && audit == "" {
fmt.Println("No output language specified")
fmt.Printf("Usage: %s [options] file\n\n", app.Name)
fmt.Printf("Use %s -help for a list of options\n", app.Name)
os.Exit(1)
}
options := compiler.Options{
Gen: gen,
Out: out,
Delim: delim,
Recurse: recurse,
Verbose: verbose,
}
// Handle panics for graceful error messages.
defer func() {
if r := recover(); r != nil {
fmt.Printf("Failed to generate %s:\n\t%s\n", options.File, r)
os.Exit(1)
}
}()
var err error
auditor := parser.NewAuditor()
for _, options.File = range c.Args() {
if audit == "" {
err = compiler.Compile(options)
} else {
err = auditor.Audit(audit, options.File)
}
if err != nil {
fmt.Printf("Failed to generate %s:\n\t%s\n", options.File, err.Error())
os.Exit(1)
}
}
return nil
}
app.Run(os.Args)
}
func genUsage() string {
usage := "generate code with a registered generator and optional parameters " +
"(lang[:key1=val1[,key2[,key3=val3]]])\n"
langKeys := make([]string, 0, len(generator.Languages))
for lang := range generator.Languages {
langKeys = append(langKeys, lang)
}
sort.Strings(langKeys)
langPrefix := ""
for _, lang := range langKeys {
options := generator.Languages[lang]
optionsStr := ""
optionKeys := make([]string, 0, len(options))
for name := range options {
optionKeys = append(optionKeys, name)
}
sort.Strings(optionKeys)
for _, name := range optionKeys {
optionsStr += fmt.Sprintf("\n\t %s:\t%s", name, options[name])
}
usage += fmt.Sprintf("%s\t %s:%s", langPrefix, lang, optionsStr)
langPrefix = "\n"
}
return usage
}