-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsv.go
201 lines (174 loc) · 5.06 KB
/
zsv.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bufio"
"encoding/json"
"log"
"os/exec"
"strings"
)
type ZsvFlag struct {
Name string `json:"name"`
Argument string `json:"argument,omitempty"`
}
type ZsvCommand struct {
Name string `json:"name"`
Flags []ZsvFlag `json:"flags,omitempty"`
}
type ZsvCLI struct {
GlobalFlags []ZsvFlag `json:"global_flags"`
Commands []ZsvCommand `json:"commands"`
}
// version -> CLI
type ZsvCLIs map[string]ZsvCLI
func getCLIsJSON(versions []string) (string, bool) {
log.Printf("loading CLIs for all zsv versions %v", versions)
clis := ZsvCLIs{}
for _, version := range versions {
cli, ok := loadCLI(version)
if !ok {
log.Printf("failed to load CLI for zsv %v", version)
return "", false
}
clis[version] = cli
}
zsvCLIsJson, err := json.Marshal(clis)
if err != nil {
log.Printf("failed to marshal CLIs to JSON, error: %v", err)
return "", false
}
log.Printf("loaded CLIs for all zsv versions successfully")
return string(zsvCLIsJson), true
}
func loadCLI(version string) (ZsvCLI, bool) {
log.Printf("loading CLI [%v]", version)
zsv := getExePath(version)
globalFlags, commandList, ok := loadGlobalFlagsAndCommands(zsv)
if !ok {
log.Print("failed to parse 'zsv help' command")
return ZsvCLI{}, false
}
// log.Print("listing global flags")
// for _, zsvFlag := range globalFlags {
// if zsvFlag.Argument == "" {
// log.Print(zsvFlag.Name)
// } else {
// log.Printf("%v | %v", zsvFlag.Name, zsvFlag.Argument)
// }
// }
commands := loadCommands(zsv, commandList)
log.Printf("loaded CLI successfully [%v]", version)
return ZsvCLI{GlobalFlags: globalFlags, Commands: commands}, true
}
func parseFlags(scanner *bufio.Scanner) []ZsvFlag {
flags := []string{}
for scanner.Scan() {
flag := scanner.Text()
if len(flag) > 3 && strings.HasPrefix(flag, " -") {
flag = strings.TrimSpace(flag)
if !strings.HasPrefix(flag, "-o") && !strings.HasPrefix(flag, "-h") {
index := strings.Index(flag, ":")
if index != -1 {
flag = strings.TrimSpace(flag[:strings.Index(flag, ":")])
flags = append(flags, flag)
}
}
} else if strings.TrimSpace(flag) == "" {
break
}
}
return normalizeFlags(flags)
}
func normalizeFlags(flags []string) []ZsvFlag {
zsvFlags := []ZsvFlag{}
for _, flag := range flags {
flag = strings.Replace(flag, ", ", ",", 1)
index := strings.Index(flag, " ")
zsvFlg := ZsvFlag{}
if index == -1 { // without argument
zsvFlg.Name = flag
} else { // with argument
zsvFlg.Name = flag[:index]
zsvFlg.Argument = strings.ReplaceAll(strings.ToLower(strings.TrimSpace(flag[index+1:])), " ", "_")
}
zsvFlags = append(zsvFlags, zsvFlg)
}
return zsvFlags
}
func parseCommands(scanner *bufio.Scanner) []string {
commands := []string{}
for scanner.Scan() {
cmd := strings.TrimSpace(scanner.Text())
if cmd == "" {
break
} else if strings.Contains(cmd, ":") {
cmd = strings.TrimSpace(cmd[:strings.Index(cmd, ":")])
commands = append(commands, cmd)
}
}
return commands
}
func loadGlobalFlagsAndCommands(zsv string) ([]ZsvFlag, []string, bool) {
log.Print("loading global flags and commands")
output, err := exec.Command(zsv, "help").Output()
if err != nil {
log.Printf("failed to get output of 'zsv help', error: %v", err)
return nil, nil, false
}
globalFlags := []ZsvFlag{}
commands := []string{}
scanner := bufio.NewScanner(strings.NewReader(string(output)))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "Options common to all commands") {
globalFlags = parseFlags(scanner)
} else if strings.HasPrefix(line, "Commands that parse CSV") {
commands = parseCommands(scanner)
}
if len(globalFlags) > 0 && len(commands) > 0 {
break
}
}
if err := scanner.Err(); err != nil {
log.Printf("failed to parse 'zsv help' command, error: %v", err)
return nil, nil, false
}
log.Printf("global flags: %v", globalFlags)
log.Printf("commands: %v", commands)
log.Print("loaded global flags and commands successfully")
return globalFlags, commands, true
}
func loadCommands(zsv string, commandList []string) []ZsvCommand {
log.Print("loading all commands")
commands := []ZsvCommand{}
for _, command := range commandList {
// log.Printf("command: %v", command)
output, err := exec.Command(zsv, "help", command).Output()
if err != nil {
log.Printf("command: %v, error: %v", command, err)
// zsv help 2json returns exit code 5
// return false
}
flags := []ZsvFlag{}
scanner := bufio.NewScanner(strings.NewReader(string(output)))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "Options") {
flags = parseFlags(scanner)
}
}
commands = append(commands, ZsvCommand{Name: command, Flags: flags})
}
// log.Printf("listing commands with flags: %v", commands)
// for _, command := range commands {
// log.Printf("command: %v", command)
// for _, flag := range command.Flags {
// if flag.Argument == "" {
// log.Print(flag.Name)
// } else {
// log.Print(flag.Name, " | ", flag.Argument)
// }
// }
// }
log.Print("loaded all commands successfully")
return commands
}