Skip to content

Commit 3293b74

Browse files
authored
Make auto completion optional (#91)
* Make auto completion optional * default autocompletion to true in absense of config
1 parent c5054d8 commit 3293b74

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
lines changed

cli/completer.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,22 +355,27 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
355355
return nil, 0
356356
}
357357

358-
autocompleteAPIArgs := []string{"listall=true"}
359-
if autocompleteAPI.Noun == "templates" {
360-
autocompleteAPIArgs = append(autocompleteAPIArgs, "templatefilter=executable")
361-
}
358+
completeArgs := t.Config.Core.AutoComplete
359+
autocompleteAPIArgs := []string{}
360+
argOptions := []argOption{}
361+
if completeArgs {
362+
autocompleteAPIArgs = []string{"listall=true"}
363+
if autocompleteAPI.Noun == "templates" {
364+
autocompleteAPIArgs = append(autocompleteAPIArgs, "templatefilter=executable")
365+
}
362366

363-
if apiFound.Name != "provisionCertificate" && autocompleteAPI.Name == "listHosts" {
364-
autocompleteAPIArgs = append(autocompleteAPIArgs, "type=Routing")
365-
}
367+
if apiFound.Name != "provisionCertificate" && autocompleteAPI.Name == "listHosts" {
368+
autocompleteAPIArgs = append(autocompleteAPIArgs, "type=Routing")
369+
}
366370

367-
spinner := t.Config.StartSpinner("fetching options, please wait...")
368-
request := cmd.NewRequest(nil, completer.Config, nil)
369-
response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false)
370-
t.Config.StopSpinner(spinner)
371+
spinner := t.Config.StartSpinner("fetching options, please wait...")
372+
request := cmd.NewRequest(nil, completer.Config, nil)
373+
response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false)
374+
t.Config.StopSpinner(spinner)
371375

372-
hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=")
373-
argOptions := buildArgOptions(response, hasID)
376+
hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=")
377+
argOptions = buildArgOptions(response, hasID)
378+
}
374379

375380
filteredOptions := []argOption{}
376381
if len(argOptions) > 0 {

cmd/set.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ func init() {
3131
Name: "set",
3232
Help: "Configures options for cmk",
3333
SubCommands: map[string][]string{
34-
"prompt": {"🐵", "🐱", "random"},
35-
"asyncblock": {"true", "false"},
36-
"timeout": {"600", "1800", "3600"},
37-
"output": config.GetOutputFormats(),
38-
"profile": {},
39-
"url": {},
40-
"username": {},
41-
"password": {},
42-
"domain": {},
43-
"apikey": {},
44-
"secretkey": {},
45-
"verifycert": {"true", "false"},
46-
"debug": {"true", "false"},
34+
"prompt": {"🐵", "🐱", "random"},
35+
"asyncblock": {"true", "false"},
36+
"timeout": {"600", "1800", "3600"},
37+
"output": config.GetOutputFormats(),
38+
"profile": {},
39+
"url": {},
40+
"username": {},
41+
"password": {},
42+
"domain": {},
43+
"apikey": {},
44+
"secretkey": {},
45+
"verifycert": {"true", "false"},
46+
"debug": {"true", "false"},
47+
"autocomplete": {"true", "false"},
4748
},
4849
Handle: func(r *Request) error {
4950
if len(r.Args) < 1 {

config/config.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ type ServerProfile struct {
5656

5757
// Core block describes common options for the CLI
5858
type Core struct {
59-
Prompt string `ini:"prompt"`
60-
AsyncBlock bool `ini:"asyncblock"`
61-
Timeout int `ini:"timeout"`
62-
Output string `ini:"output"`
63-
VerifyCert bool `ini:"verifycert"`
64-
ProfileName string `ini:"profile"`
59+
Prompt string `ini:"prompt"`
60+
AsyncBlock bool `ini:"asyncblock"`
61+
Timeout int `ini:"timeout"`
62+
Output string `ini:"output"`
63+
VerifyCert bool `ini:"verifycert"`
64+
ProfileName string `ini:"profile"`
65+
AutoComplete bool `ini:"autocomplete"`
6566
}
6667

6768
// Config describes CLI config file and default options
@@ -141,6 +142,7 @@ func defaultCoreConfig() Core {
141142
Output: JSON,
142143
VerifyCert: true,
143144
ProfileName: "localcloud",
145+
AutoComplete: true,
144146
}
145147
}
146148

@@ -251,6 +253,9 @@ func saveConfig(cfg *Config) *Config {
251253
// Update
252254
core := new(Core)
253255
conf.Section(ini.DEFAULT_SECTION).MapTo(core)
256+
if (!conf.Section(ini.DEFAULT_SECTION).HasKey("autocomplete")) {
257+
core.AutoComplete = true
258+
}
254259
cfg.Core = core
255260
}
256261

@@ -340,6 +345,8 @@ func (c *Config) UpdateConfig(key string, value string, update bool) {
340345
} else {
341346
DisableDebugging()
342347
}
348+
case "autocomplete":
349+
c.Core.AutoComplete = value == "true"
343350
default:
344351
fmt.Println("Invalid option provided:", key)
345352
return

0 commit comments

Comments
 (0)