forked from zarldev/goenums
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·61 lines (51 loc) · 1.34 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
package main
import (
"fmt"
"os"
"github.com/broderick-westrope/goenums/internal/config"
"github.com/broderick-westrope/goenums/internal/generator"
flag "github.com/spf13/pflag"
)
func main() {
flag.Usage = func() {
fmt.Println("Usage: goenums <config path> <output path>")
flag.PrintDefaults()
os.Exit(0)
}
var format string
flag.StringVarP(&format, "format", "f", "", "The format of the config file (json, yaml, yml)")
flag.Parse()
// Check for the config file
if len(os.Args) < 2 {
fmt.Println("Error: No config file provided.")
os.Exit(1)
}
cfgPath := os.Args[1]
// Parse the config file
cfg, err := config.Parse(cfgPath, format)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
if len(cfg.EnumConfigs) == 0 {
fmt.Printf("Error: No enum configurations found in the config file %q.\n", cfgPath)
os.Exit(1)
}
// Check for the output path in both the config and the arguments
if len(os.Args) < 3 {
if cfg.OutputPath == "" {
fmt.Println("Error: Config file contained no output path and no output path was provided as an argument.")
os.Exit(1)
}
} else {
// Prioritise the output path provided as an argument, when present
cfg.OutputPath = os.Args[2]
}
// Generate the code
g := generator.New(cfg)
err = g.Generate()
if err != nil {
fmt.Println("Error generating code:", err)
os.Exit(1)
}
}