From 1c0c6b790e8656d3dfd612d32d806c40cd4b18c0 Mon Sep 17 00:00:00 2001 From: Pearl Dsilva Date: Mon, 9 Aug 2021 10:31:24 +0530 Subject: [PATCH 1/2] Make auto completion optional --- cli/completer.go | 31 ++++++++++++++++++------------- cmd/set.go | 27 ++++++++++++++------------- config/config.go | 16 ++++++++++------ 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/cli/completer.go b/cli/completer.go index ce2b7e29..650c84b8 100644 --- a/cli/completer.go +++ b/cli/completer.go @@ -355,22 +355,27 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int) return nil, 0 } - autocompleteAPIArgs := []string{"listall=true"} - if autocompleteAPI.Noun == "templates" { - autocompleteAPIArgs = append(autocompleteAPIArgs, "templatefilter=executable") - } + completeArgs := t.Config.Core.AutoComplete + autocompleteAPIArgs := []string{} + argOptions := []argOption{} + if completeArgs { + autocompleteAPIArgs = []string{"listall=true"} + if autocompleteAPI.Noun == "templates" { + autocompleteAPIArgs = append(autocompleteAPIArgs, "templatefilter=executable") + } - if apiFound.Name != "provisionCertificate" && autocompleteAPI.Name == "listHosts" { - autocompleteAPIArgs = append(autocompleteAPIArgs, "type=Routing") - } + if apiFound.Name != "provisionCertificate" && autocompleteAPI.Name == "listHosts" { + autocompleteAPIArgs = append(autocompleteAPIArgs, "type=Routing") + } - spinner := t.Config.StartSpinner("fetching options, please wait...") - request := cmd.NewRequest(nil, completer.Config, nil) - response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false) - t.Config.StopSpinner(spinner) + spinner := t.Config.StartSpinner("fetching options, please wait...") + request := cmd.NewRequest(nil, completer.Config, nil) + response, _ := cmd.NewAPIRequest(request, autocompleteAPI.Name, autocompleteAPIArgs, false) + t.Config.StopSpinner(spinner) - hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=") - argOptions := buildArgOptions(response, hasID) + hasID := strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=") + argOptions = buildArgOptions(response, hasID) + } filteredOptions := []argOption{} if len(argOptions) > 0 { diff --git a/cmd/set.go b/cmd/set.go index fef9b3f5..c4269489 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -31,19 +31,20 @@ func init() { Name: "set", Help: "Configures options for cmk", SubCommands: map[string][]string{ - "prompt": {"🐵", "🐱", "random"}, - "asyncblock": {"true", "false"}, - "timeout": {"600", "1800", "3600"}, - "output": config.GetOutputFormats(), - "profile": {}, - "url": {}, - "username": {}, - "password": {}, - "domain": {}, - "apikey": {}, - "secretkey": {}, - "verifycert": {"true", "false"}, - "debug": {"true", "false"}, + "prompt": {"🐵", "🐱", "random"}, + "asyncblock": {"true", "false"}, + "timeout": {"600", "1800", "3600"}, + "output": config.GetOutputFormats(), + "profile": {}, + "url": {}, + "username": {}, + "password": {}, + "domain": {}, + "apikey": {}, + "secretkey": {}, + "verifycert": {"true", "false"}, + "debug": {"true", "false"}, + "autocomplete": {"true", "false"}, }, Handle: func(r *Request) error { if len(r.Args) < 1 { diff --git a/config/config.go b/config/config.go index 0b82c4d4..25bf1404 100644 --- a/config/config.go +++ b/config/config.go @@ -55,12 +55,13 @@ type ServerProfile struct { // Core block describes common options for the CLI type Core struct { - Prompt string `ini:"prompt"` - AsyncBlock bool `ini:"asyncblock"` - Timeout int `ini:"timeout"` - Output string `ini:"output"` - VerifyCert bool `ini:"verifycert"` - ProfileName string `ini:"profile"` + Prompt string `ini:"prompt"` + AsyncBlock bool `ini:"asyncblock"` + Timeout int `ini:"timeout"` + Output string `ini:"output"` + VerifyCert bool `ini:"verifycert"` + ProfileName string `ini:"profile"` + AutoComplete bool `ini:"autocomplete"` } // Config describes CLI config file and default options @@ -140,6 +141,7 @@ func defaultCoreConfig() Core { Output: JSON, VerifyCert: true, ProfileName: "localcloud", + AutoComplete: true, } } @@ -339,6 +341,8 @@ func (c *Config) UpdateConfig(key string, value string, update bool) { } else { DisableDebugging() } + case "autocomplete": + c.Core.AutoComplete = value == "true" default: fmt.Println("Invalid option provided:", key) return From 2e54a3611872ede240ff05bdb93316739f40c6d1 Mon Sep 17 00:00:00 2001 From: Pearl Dsilva Date: Mon, 9 Aug 2021 14:09:43 +0530 Subject: [PATCH 2/2] default autocompletion to true in absense of config --- config/config.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/config.go b/config/config.go index 3acae9b8..a2aa400f 100644 --- a/config/config.go +++ b/config/config.go @@ -252,6 +252,9 @@ func saveConfig(cfg *Config) *Config { // Update core := new(Core) conf.Section(ini.DEFAULT_SECTION).MapTo(core) + if (!conf.Section(ini.DEFAULT_SECTION).HasKey("autocomplete")) { + core.AutoComplete = true + } cfg.Core = core }