-
Notifications
You must be signed in to change notification settings - Fork 567
/
shell.go
245 lines (221 loc) · 5.91 KB
/
shell.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package shell
import (
"fmt"
"os"
"os/exec"
"strings"
prompt "github.com/c-bata/go-prompt"
"github.com/fatih/color"
"github.com/pachyderm/pachyderm/v2/src/internal/config"
"github.com/pachyderm/pachyderm/v2/src/internal/uuid"
"github.com/spf13/cobra"
)
const (
completionAnnotation string = "completion"
ldThreshold int = 2
defaultMaxCompletions int64 = 64
)
// CacheFunc is a function which returns whether or not cached results from a
// previous call to a CompletionFunc can be reused.
type CacheFunc func(flag, text string) bool
// CacheAll is a CacheFunc that always returns true (always use cached results).
func CacheAll(_, _ string) bool { return true }
// CacheNone is a CacheFunc that always returns false (never cache anything).
func CacheNone(_, _ string) bool { return false }
// SameFlag is a CacheFunc that returns true if the flags are the same.
func SameFlag(flag string) CacheFunc {
return func(_flag, _ string) bool {
return flag == _flag
}
}
// AndCacheFunc ands 0 or more cache funcs together.
func AndCacheFunc(fs ...CacheFunc) CacheFunc {
return func(flag, text string) bool {
for _, f := range fs {
if !f(flag, text) {
return false
}
}
return true
}
}
// CompletionFunc is a function which returns completions for a command.
type CompletionFunc func(flag, text string, maxCompletions int64) ([]prompt.Suggest, CacheFunc)
var completions map[string]CompletionFunc = make(map[string]CompletionFunc)
// RegisterCompletionFunc registers a completion function for a command.
// NOTE: RegisterCompletionFunc must be called before cmd is passed to
// functions that make copies of it (such as cmdutil.CreateAlias. This is
// because RegisterCompletionFunc modifies cmd in a superficial way by adding
// an annotation (to the Annotations field) that associates it with the
// completion function. This means that
func RegisterCompletionFunc(cmd *cobra.Command, completionFunc CompletionFunc) {
id := uuid.NewWithoutDashes()
if cmd.Annotations == nil {
cmd.Annotations = make(map[string]string)
} else if _, ok := cmd.Annotations[completionAnnotation]; ok {
panic("duplicate completion func registration")
}
cmd.Annotations[completionAnnotation] = id
completions[id] = completionFunc
}
type shell struct {
rootCmd *cobra.Command
maxCompletions int64
// variables for caching completion calls
completionID string
suggests []prompt.Suggest
cacheF CacheFunc
}
func newShell(rootCmd *cobra.Command, maxCompletions int64) *shell {
if maxCompletions == 0 {
maxCompletions = defaultMaxCompletions
}
return &shell{
rootCmd: rootCmd,
maxCompletions: maxCompletions,
}
}
func (s *shell) executor(in string) {
if in == "exit" {
os.Exit(0)
}
cmd := exec.Command("bash")
cmd.Stdin = strings.NewReader("pachctl " + in)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run() //nolint:errcheck
}
func (s *shell) suggestor(in prompt.Document) []prompt.Suggest {
args := strings.Fields(in.Text)
if len(strings.TrimSuffix(in.Text, " ")) < len(in.Text) {
args = append(args, "")
}
cmd := s.rootCmd
text := ""
if len(args) > 0 {
var err error
cmd, _, err = s.rootCmd.Traverse(args[:len(args)-1])
if err != nil {
Fatal(err)
}
text = args[len(args)-1]
}
flag := ""
if len(args) > 1 {
if args[len(args)-2][0] == '-' {
flag = args[len(args)-2]
}
}
suggestions := cmd.SuggestionsFor(text)
if len(suggestions) > 0 {
var result []prompt.Suggest
for _, suggestion := range suggestions {
cmd, _, err := cmd.Traverse([]string{suggestion})
if err != nil {
Fatal(err)
}
result = append(result, prompt.Suggest{
Text: suggestion,
Description: cmd.Short,
})
}
return result
}
if id, ok := cmd.Annotations[completionAnnotation]; ok {
completionFunc := completions[id]
if s.completionID != id || s.cacheF == nil || !s.cacheF(flag, text) {
s.completionID = id
s.suggests, s.cacheF = completionFunc(flag, text, s.maxCompletions)
}
var result []prompt.Suggest
for _, sug := range s.suggests {
sText := sug.Text
if len(text) < len(sText) {
sText = sText[:len(text)]
}
if ld(sText, text, true) < ldThreshold {
result = append(result, sug)
}
if int64(len(result)) > s.maxCompletions {
break
}
}
return result
}
return nil
}
func (s *shell) clearCache() {
s.completionID = ""
s.suggests = nil
s.cacheF = nil
}
func (s *shell) run() {
fmt.Printf("Type 'exit' or press Ctrl-D to exit.\n")
color.NoColor = true // color doesn't work in terminal
prompt.New(
s.executor,
s.suggestor,
prompt.OptionPrefix(">>> "),
prompt.OptionTitle("Pachyderm Shell"),
prompt.OptionAddKeyBind(prompt.KeyBind{
Key: prompt.F5,
Fn: func(*prompt.Buffer) { s.clearCache() },
}),
prompt.OptionLivePrefix(func() (string, bool) {
cfg, err := config.Read(true, false)
if err != nil {
return "", false
}
activeContext, _, err := cfg.ActiveContext(false)
if err != nil {
return "", false
}
return fmt.Sprintf("context:(%s) >>> ", activeContext), true
}),
).Run()
if err := closePachClient(); err != nil {
Fatal(err)
}
}
// Run runs a prompt, it does not return.
func Run(rootCmd *cobra.Command, maxCompletions int64) {
newShell(rootCmd, maxCompletions).run()
}
// ld computes the Levenshtein Distance for two strings.
func ld(s, t string, ignoreCase bool) int {
if ignoreCase {
s = strings.ToLower(s)
t = strings.ToLower(t)
}
d := make([][]int, len(s)+1)
for i := range d {
d[i] = make([]int, len(t)+1)
}
for i := range d {
d[i][0] = i
}
for j := range d[0] {
d[0][j] = j
}
for j := 1; j <= len(t); j++ {
for i := 1; i <= len(s); i++ {
if s[i-1] == t[j-1] {
d[i][j] = d[i-1][j-1]
} else {
min := d[i-1][j]
if d[i][j-1] < min {
min = d[i][j-1]
}
if d[i-1][j-1] < min {
min = d[i-1][j-1]
}
d[i][j] = min + 1
}
}
}
return d[len(s)][len(t)]
}
func Fatal(args ...any) {
fmt.Fprint(os.Stderr, args...)
os.Exit(1)
}