-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (66 loc) · 1.71 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
package main
import (
"fmt"
"github.com/spf13/cobra"
"os"
"path/filepath"
)
var (
typeName string
lineComment bool
indent bool
output string
example string
description string
trimPrefix string
addPrefix string
transform string
templateFile string
)
var command = &cobra.Command{
Use: "enum schema",
Short: "Generate schema",
RunE: func(cmd *cobra.Command, args []string) error {
var (
dir = "."
g Generator
)
if len(args) == 1 && isDirectory(args[0]) {
dir = args[0]
} else if len(args) > 0 {
dir = filepath.Dir(args[0])
}
g.parsePackage(args, []string{})
if err := g.generate(
typeName, trimPrefix, addPrefix, transform,
templateFile, lineComment,
); err != nil {
return err
}
tmpFile, err := os.CreateTemp(dir, fmt.Sprintf("%s-schema-enum.json", typeName))
defer tmpFile.Close()
if err != nil {
return err
}
_, err = tmpFile.Write(g.buf.Bytes())
if err != nil {
os.Remove(tmpFile.Name())
return err
}
return os.Rename(tmpFile.Name(), output)
},
}
func init() {
command.PersistentFlags().StringVar(&typeName, "type", "", "type name")
command.PersistentFlags().BoolVar(&lineComment, "linecomment", false, "line comment")
command.PersistentFlags().StringVarP(&output, "output", "o", "schema.json", "output file")
command.PersistentFlags().StringVar(&trimPrefix, "trimprefix", "", "trim prefix")
command.PersistentFlags().StringVar(&addPrefix, "addprefix", "", "add prefix")
command.PersistentFlags().StringVar(&transform, "transform", "", "transform")
command.PersistentFlags().StringVarP(&templateFile, "template", "t", "", "template file")
}
func main() {
if err := command.Execute(); err != nil {
fmt.Println(err)
}
}