forked from abice/go-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (67 loc) · 2.29 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/go-easygen/cli"
"github.com/marusama/go-enum/generator"
)
type rootT struct {
cli.Helper
FileNames []string `cli:"*f,file" usage:"The file(s) to generate enums. Use more than one flag for more files."`
NoPrefix bool `cli:"noprefix" usage:"Prevents the constants generated from having the Enum as a prefix."`
Lowercase bool `cli:"lower" usage:"Adds lowercase variants of the enum strings for lookup."`
Marshal bool `cli:"marshal" usage:"Adds text marshalling functions."`
JSON bool `cli:"json" usage:"Adds json marshalling functions."`
SQL bool `cli:"sql" usage:"Adds SQL database scan and value functions."`
Flag bool `cli:"flag" usage:"Adds golang flag functions."`
Prefix string `cli:"prefix" usage:"Replaces the prefix with a user one."`
Names bool `cli:"names" usage:"Generates a 'Names() []string' function, and adds the possible enum values in the error response during parsing"`
}
func main() {
cli.Run(new(rootT), func(ctx *cli.Context) error {
argv := ctx.Argv().(*rootT)
for _, fileName := range argv.FileNames {
g := generator.NewGenerator()
if argv.NoPrefix {
g.WithNoPrefix()
}
if argv.Lowercase {
g.WithLowercaseVariant()
}
if argv.Marshal {
g.WithMarshal()
}
if argv.JSON {
g.WithJSON()
}
if argv.SQL {
g.WithSQLDriver()
}
if argv.Flag {
g.WithFlag()
}
if argv.Names {
g.WithNames()
}
originalName := fileName
ctx.String("go-enum started. file: %s\n", ctx.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", ctx.Color().Cyan(fileName), ctx.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", ctx.Color().Cyan(outFilePath), ctx.Color().Red(err))
}
ctx.String("go-enum finished. file: %s\n", ctx.Color().Cyan(originalName))
}
return nil
})
}