-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (70 loc) · 1.57 KB
/
main.go
File metadata and controls
76 lines (70 loc) · 1.57 KB
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
package main
import (
"flag"
"github.com/phuslu/log"
"io/fs"
"os"
"path/filepath"
"strings"
)
func main() {
if len(os.Args) == 1 {
log.Fatal().Msg("Target directory is required")
}
var outputDir = flag.String("o", "gen/", "output path")
flag.Parse()
path := os.Args[len(os.Args)-1]
fInfo, err := os.Stat(path)
if err != nil {
log.Fatal().Err(err).Msg("failed check path")
}
var cmds []*Command
if fInfo.IsDir() {
filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if strings.HasSuffix(path, "yaml") {
cmds = append(cmds, GetCommandLists(path)...)
}
return nil
})
} else {
cmds = GetCommandLists(path)
}
for _, c := range cmds {
c.BaseText = strings.ReplaceAll(c.BaseText, " ", "")
var slice []string
for _, s := range c.SubTexts {
slice = append(slice, strings.ReplaceAll(s, " ", ""))
}
c.SubTexts = slice
}
Validate(cmds)
GenerateCommandHandler(cmds, *outputDir)
}
func Validate(cmds []*Command) {
var ids []string
var texts []string
for _, cmd := range cmds {
if isContain(ids, cmd.ID) {
log.Fatal().Msg("cant use same id: " + cmd.ID)
}
ids = append(ids, cmd.ID)
if isContain(texts, cmd.BaseText) {
log.Fatal().Msg("cant use same base text: " + cmd.BaseText)
}
texts = append(texts, cmd.BaseText)
for _, s := range cmd.SubTexts {
if isContain(texts, s) {
log.Fatal().Msg("cant use same sub text: " + s)
}
texts = append(texts, s)
}
}
}
func isContain(base []string, target string) bool {
for _, val := range base {
if target == val {
return true
}
}
return false
}