/
search_hdlr.go
226 lines (200 loc) · 5.92 KB
/
search_hdlr.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Package cli provides easy-to-use commands to manage, monitor, and utilize AIS clusters.
// This file provides commands that remove various entities from the cluster.
/*
* Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
*/
package cli
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/NVIDIA/aistore/cmd/cli/teb"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/urfave/cli"
)
var (
searchCmdFlags = []cli.Flag{
regexFlag,
}
searchCommands []cli.Command
similarWords = map[string][]string{
cmdMountpath: {"mount", "unmount", "umount", "disk"},
commandList: {"list", "dir", "contents"},
commandSet: {"update", "assign", "modify"},
commandShow: {"view", "display", "list"},
commandRemove: {"remove", "delete", "del", "evict", "destroy", "cleanup"},
commandRename: {"move", "rename", "ren"},
commandCopy: {"copy", "replicate", "backup"},
commandGet: {"fetch", "read", "download"},
commandPrefetch: {"load", "preload", "warmup", "cache", "get"},
commandMirror: {"protect", "replicate", "copy"},
commandECEncode: {"protect", "encode", "replicate", "erasure-code"},
commandStart: {"do", "run", "execute"},
commandStop: {"abort", "terminate"},
commandPut: {"update", "write", "promote", "modify", "upload"},
commandCreate: {"add", "new"},
commandObject: {"file"},
commandStorage: {"disk", "mountpath", "capacity", "used", "available"},
commandBucket: {"dir", "directory", "container"},
commandJob: {"batch", "async"},
commandArch: {"serialize", "format", "reformat", "compress", "tar", "zip", "gzip"},
cmdAuthAdd: {"register", "create"},
cmdStgCleanup: {"remove", "delete", "evict"},
cmdDownload: {"load", "populate", "copy", "cp"},
}
// app state
cmdStrs []string
keywordMap map[string][]string // mapping of synonym to actual
invIndex map[string][]int // inverted index key: [commands]
)
func initSearch(app *cli.App) {
searchCommands = []cli.Command{
{
Name: commandSearch,
Usage: "search " + cliName + " commands, e.g.:\n" +
indent1 + "\t - 'ais search log' - commands containing 'log' subcommand\n" +
indent1 + "\t - 'ais search --regex log' - include all subcommands that contain 'log' substring\n" +
indent1 + "\t - 'ais search --regex \"\\blog\"' - slightly narrow the search to those that have 'log' on a word boundary, etc.",
ArgsUsage: searchArgument,
Action: searchCmdHdlr,
Flags: searchCmdFlags,
BashComplete: searchBashCmplt,
},
}
cmdStrs = getFullCmdNames(app.Name, app.Commands)
populateKeyMapInvIdx()
}
func populateKeyMapInvIdx() {
invIndex = make(map[string][]int)
keywordMap = invertMap(similarWords)
for i := range cmdStrs {
keywords := strings.Split(cmdStrs[i], " ")
for _, word := range keywords {
keywordMap[word] = append(keywordMap[word], word)
invIndex[word] = append(invIndex[word], i)
}
}
}
func findCmdByKey(key string) cos.StrSet {
result := make(cos.StrSet)
_find(key, result)
// in addition:
for w := range keywordMap {
if strings.HasPrefix(w, key+"-") { // e.g., ("reset-stats", "reset")
_find(w, result)
}
}
return result
}
func _find(key string, result cos.StrSet) {
resKeys, ok := keywordMap[key]
if !ok {
return
}
for _, resKey := range resKeys {
for _, idx := range invIndex[resKey] {
result.Add(cmdStrs[idx])
}
}
}
// (compare w/ findCmdMultiKeyAlt)
func findCmdMultiKey(keys []string) []string {
resultSet := findCmdByKey(keys[0])
for _, key := range keys[1:] {
cmds := findCmdByKey(key)
resultSet = resultSet.Intersection(cmds)
}
result := resultSet.ToSlice()
sort.Strings(result)
return result
}
func findCmdMultiKeyAlt(keys ...string) []string {
var (
result []string
resultSet = findCmdByKey(keys[0])
)
outer:
for cmd := range resultSet {
for _, key := range keys[1:] {
if !strings.Contains(cmd, " "+key+" ") && !strings.HasSuffix(cmd, " "+key) {
continue outer
}
}
result = append(result, cmd)
}
sort.Strings(result)
return result
}
func findCmdMatching(pattern string) []string {
result := make([]string, 0)
for _, cmd := range cmdStrs {
if cond, _ := regexp.MatchString(pattern, cmd); cond {
result = append(result, cmd)
}
}
return result
}
func searchCmdHdlr(c *cli.Context) (err error) {
var commands []string
if !flagIsSet(c, regexFlag) && c.NArg() == 0 {
return missingArgumentsError(c, "keyword")
}
if flagIsSet(c, regexFlag) {
pattern := parseStrFlag(c, regexFlag)
commands = findCmdMatching(pattern)
} else {
if c.NArg() > 1 {
for word, similar := range similarWords {
if !cos.StringInSlice(word, c.Args()) {
continue
}
for _, word2 := range similar {
if cos.StringInSlice(word2, c.Args()) {
warn := fmt.Sprintf("%q and %q are \"similar\"", word, word2)
actionWarn(c, warn+" (search results may include either/or combinations)")
}
}
}
}
commands = findCmdMultiKey(c.Args())
}
if len(commands) > 0 {
err = teb.Print(commands, teb.SearchTmpl)
}
if len(commands) == 0 && err == nil && !flagIsSet(c, regexFlag) {
// tip
msg := fmt.Sprintf("No matches (use %s to include more results, e.g.: '%s %s %s %s')\n",
qflprn(regexFlag), cliName, commandSearch, flprn(regexFlag), c.Args().Get(0))
actionDone(c, msg)
}
return err
}
func searchBashCmplt(_ *cli.Context) {
for key := range keywordMap {
fmt.Println(key)
}
}
func invertMap(inp map[string][]string) map[string][]string {
inv := make(map[string][]string)
for key := range inp {
for _, v := range inp[key] {
inv[v] = append(inv[v], key)
}
}
return inv
}
func getFullCmdNames(base string, cmds cli.Commands) []string {
names := make([]string, 0)
for i := range cmds {
cmd := &cmds[i]
fullCmd := fmt.Sprintf("%s %s", base, cmd.FullName())
if len(cmd.Subcommands) == 0 {
names = append(names, fullCmd)
continue
}
output := getFullCmdNames(fullCmd, cmd.Subcommands)
names = append(names, output...)
}
return names
}