Skip to content

Commit fdd8a90

Browse files
committed
Add autocomplete support for API allowedValues
1 parent de36f6d commit fdd8a90

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

cli/completer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,18 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
413413
return
414414
}
415415

416+
if len(arg.AllowedValues) > 0 {
417+
offset = 0
418+
for _, value := range arg.AllowedValues {
419+
option := value + " "
420+
if strings.HasPrefix(value, argInput) {
421+
options = append(options, []rune(option[len(argInput):]))
422+
offset = len(argInput)
423+
}
424+
}
425+
return
426+
}
427+
416428
autocompleteAPI := findAutocompleteAPI(arg, apiFound, apiMap)
417429
if autocompleteAPI == nil {
418430
return nil, 0

config/cache.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ var bundledAPICache []byte
3939

4040
// APIArg are the args passable to an API
4141
type APIArg struct {
42-
Name string
43-
Type string
44-
Related []string
45-
Description string
46-
Required bool
47-
Length int
42+
Name string
43+
Type string
44+
Related []string
45+
AllowedValues []string
46+
Description string
47+
Required bool
48+
Length int
4849
}
4950

5051
// API describes a CloudStack API
@@ -143,12 +144,23 @@ func (c *Config) UpdateCache(response map[string]interface{}) interface{} {
143144
related = strings.Split(apiArg["related"].(string), ",")
144145
sort.Strings(related)
145146
}
147+
allowedValues := []string{}
148+
if apiArg["allowedvalues"] != nil {
149+
if rawValues, ok := apiArg["allowedvalues"].([]interface{}); ok {
150+
for _, value := range rawValues {
151+
if str, ok := value.(string); ok {
152+
allowedValues = append(allowedValues, str)
153+
}
154+
}
155+
}
156+
}
146157
apiArgs = append(apiArgs, &APIArg{
147-
Name: apiArg["name"].(string) + "=",
148-
Type: apiArg["type"].(string),
149-
Required: apiArg["required"].(bool),
150-
Related: related,
151-
Description: apiArg["description"].(string),
158+
Name: apiArg["name"].(string) + "=",
159+
Type: apiArg["type"].(string),
160+
Required: apiArg["required"].(bool),
161+
Related: related,
162+
Description: apiArg["description"].(string),
163+
AllowedValues: allowedValues,
152164
})
153165
}
154166

0 commit comments

Comments
 (0)