forked from abice/go-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (168 loc) · 4.55 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
180
181
182
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/abice/go-enum/generator"
"github.com/labstack/gommon/color"
"github.com/urfave/cli/v2"
)
type rootT struct {
FileNames cli.StringSlice
NoPrefix bool
Lowercase bool
NoCase bool
Marshal bool
SQL bool
Flag bool
Prefix string
Names bool
LeaveSnakeCase bool
SQLNullStr bool
SQLNullInt bool
Ptr bool
}
func main() {
var argv rootT
clr := color.New()
out := func(format string, args ...interface{}) {
_, _ = fmt.Fprintf(clr.Output(), format, args...)
}
app := &cli.App{
Name: "go-enum",
Usage: "An enum generator for go",
HideHelpCommand: true,
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "The file(s) to generate enums. Use more than one flag for more files.",
Required: true,
Destination: &argv.FileNames,
},
&cli.BoolFlag{
Name: "noprefix",
Usage: "Prevents the constants generated from having the Enum as a prefix.",
Destination: &argv.NoPrefix,
},
&cli.BoolFlag{
Name: "lower",
Usage: "Adds lowercase variants of the enum strings for lookup.",
Destination: &argv.Lowercase,
},
&cli.BoolFlag{
Name: "nocase",
Usage: "Adds case insensitive parsing to the enumeration (forces lower flag).",
Destination: &argv.NoCase,
},
&cli.BoolFlag{
Name: "marshal",
Usage: "Adds text (and inherently json) marshalling functions.",
Destination: &argv.Marshal,
},
&cli.BoolFlag{
Name: "sql",
Usage: "Adds SQL database scan and value functions.",
Destination: &argv.SQL,
},
&cli.BoolFlag{
Name: "flag",
Usage: "Adds golang flag functions.",
Destination: &argv.Flag,
},
&cli.StringFlag{
Name: "prefix",
Usage: "Replaces the prefix with a user one.",
Destination: &argv.Prefix,
},
&cli.BoolFlag{
Name: "names",
Usage: "Generates a 'Names() []string' function, and adds the possible enum values in the error response during parsing",
Destination: &argv.Names,
},
&cli.BoolFlag{
Name: "nocamel",
Usage: "Removes the snake_case to CamelCase name changing",
Destination: &argv.LeaveSnakeCase,
},
&cli.BoolFlag{
Name: "ptr",
Usage: "Adds a pointer method to get a pointer from const values",
Destination: &argv.Ptr,
},
&cli.BoolFlag{
Name: "sqlnullint",
Usage: "Adds a Null{{ENUM}} type for marshalling a nullable int value to sql",
Destination: &argv.SQLNullInt,
},
&cli.BoolFlag{
Name: "sqlnullstr",
Usage: "Adds a Null{{ENUM}} type for marshalling a nullable string value to sql. If sqlnullint is specified too, it will be Null{{ENUM}}Str",
Destination: &argv.SQLNullStr,
},
},
Action: func(ctx *cli.Context) error {
for _, fileName := range argv.FileNames.Value() {
g := generator.NewGenerator()
if argv.NoPrefix {
g.WithNoPrefix()
}
if argv.Lowercase {
g.WithLowercaseVariant()
}
if argv.NoCase {
g.WithCaseInsensitiveParse()
}
if argv.Marshal {
g.WithMarshal()
}
if argv.SQL {
g.WithSQLDriver()
}
if argv.Flag {
g.WithFlag()
}
if argv.Names {
g.WithNames()
}
if argv.LeaveSnakeCase {
g.WithoutSnakeToCamel()
}
if argv.Prefix != "" {
g.WithPrefix(argv.Prefix)
}
if argv.Ptr {
g.WithPtr()
}
if argv.SQLNullInt {
g.WithSQLNullInt()
}
if argv.SQLNullStr {
g.WithSQLNullStr()
}
originalName := fileName
out("go-enum started. file: %s\n", color.Cyan(originalName))
fileName, _ = filepath.Abs(fileName)
outFilePath := fmt.Sprintf("%s_enum.go", strings.TrimSuffix(fileName, filepath.Ext(fileName)))
// Parse the file given in arguments
raw, err := g.GenerateFromFile(fileName)
if err != nil {
return fmt.Errorf("failed generating enums\nInputFile=%s\nError=%s", color.Cyan(fileName), color.RedBg(err))
}
mode := int(0644)
err = ioutil.WriteFile(outFilePath, raw, os.FileMode(mode))
if err != nil {
return fmt.Errorf("failed writing to file %s: %s", color.Cyan(outFilePath), color.Red(err))
}
out("go-enum finished. file: %s\n", color.Cyan(originalName))
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}