You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ro...@apache.org on 2018/04/14 01:14:42 UTC

[cloudstack-cloudmonkey] branch master updated: set: autocompletion for set parameters

This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git


The following commit(s) were added to refs/heads/master by this push:
     new 3584ac0  set: autocompletion for set parameters
3584ac0 is described below

commit 3584ac03f6899c7debd218f54351f70429983f11
Author: Rohit Yadav <ro...@apache.org>
AuthorDate: Sat Apr 14 06:44:25 2018 +0530

    set: autocompletion for set parameters
    
    Signed-off-by: Rohit Yadav <ro...@apache.org>
---
 cli/completer.go | 31 +++++++++++++++++++++----------
 cli/shell.go     |  1 +
 cmd/api.go       |  2 +-
 cmd/command.go   |  2 +-
 cmd/set.go       | 20 ++++++++++++++++----
 config/cache.go  |  2 +-
 config/config.go | 17 +++++++++++++++++
 7 files changed, 58 insertions(+), 17 deletions(-)

diff --git a/cli/completer.go b/cli/completer.go
index 271faa1..8ade3f6 100644
--- a/cli/completer.go
+++ b/cli/completer.go
@@ -37,12 +37,23 @@ func buildAPICacheMap(apiMap map[string][]*config.API) map[string][]*config.API
 	for _, cmd := range cmd.AllCommands() {
 		verb := cmd.Name
 		if cmd.SubCommands != nil && len(cmd.SubCommands) > 0 {
-			for _, scmd := range cmd.SubCommands {
-				dummyAPI := &config.API{
-					Name: scmd,
-					Verb: verb,
+			for command, opts := range cmd.SubCommands {
+				var args []*config.APIArg
+				options := opts
+				if command == "profile" {
+					options = config.GetProfiles()
+				}
+				for _, opt := range options {
+					args = append(args, &config.APIArg{
+						Name: opt,
+					})
 				}
-				apiMap[verb] = append(apiMap[verb], dummyAPI)
+				apiMap[verb] = append(apiMap[verb], &config.API{
+					Name: command,
+					Verb: verb,
+					Noun: command,
+					Args: args,
+				})
 			}
 		} else {
 			dummyAPI := &config.API{
@@ -151,7 +162,7 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
 	splitLine := strings.Split(string(line), " ")
 	line = trimSpaceLeft([]rune(splitLine[len(splitLine)-1]))
 	for _, arg := range apiFound.Args {
-		search := arg.Name + "="
+		search := arg.Name
 		if !runes.HasPrefix(line, []rune(search)) {
 			sLine, sOffset := doInternal(line, pos, len(line), []rune(search))
 			options = append(options, sLine...)
@@ -165,15 +176,15 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
 
 			var autocompleteAPI *config.API
 			var relatedNoun string
-			if arg.Name == "id" || arg.Name == "ids" {
+			if arg.Name == "id=" || arg.Name == "ids=" {
 				relatedNoun = apiFound.Noun
 				if apiFound.Verb != "list" {
 					relatedNoun += "s"
 				}
-			} else if arg.Name == "account" {
+			} else if arg.Name == "account=" {
 				relatedNoun = "accounts"
 			} else {
-				relatedNoun = strings.Replace(strings.Replace(arg.Name, "ids", "", -1), "id", "", -1) + "s"
+				relatedNoun = strings.Replace(strings.Replace(arg.Name, "ids=", "", -1), "id=", "", -1) + "s"
 			}
 			for _, related := range apiMap["list"] {
 				if relatedNoun == related.Noun {
@@ -231,7 +242,7 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int)
 				})
 				fmt.Println()
 				selectedOption := showSelector(autocompleteOptions)
-				if strings.HasSuffix(arg.Name, "id") || strings.HasSuffix(arg.Name, "ids") {
+				if strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=") {
 					selected = selectedOption.ID
 				} else {
 					selected = selectedOption.Name
diff --git a/cli/shell.go b/cli/shell.go
index 26ab8d9..cdf46ac 100644
--- a/cli/shell.go
+++ b/cli/shell.go
@@ -23,6 +23,7 @@ import (
 	"strings"
 
 	"cloudmonkey/config"
+
 	"github.com/mattn/go-shellwords"
 
 	"github.com/chzyer/readline"
diff --git a/cmd/api.go b/cmd/api.go
index facb25a..dd4400a 100644
--- a/cmd/api.go
+++ b/cmd/api.go
@@ -63,7 +63,7 @@ func init() {
 			for _, required := range api.RequiredArgs {
 				provided := false
 				for _, arg := range apiArgs {
-					if strings.HasPrefix(arg, required+"=") {
+					if strings.HasPrefix(arg, required) {
 						provided = true
 					}
 				}
diff --git a/cmd/command.go b/cmd/command.go
index 4f90387..7d5a2fe 100644
--- a/cmd/command.go
+++ b/cmd/command.go
@@ -25,7 +25,7 @@ import (
 type Command struct {
 	Name            string
 	Help            string
-	SubCommands     []string
+	SubCommands     map[string][]string
 	CustomCompleter func(input string, position int)
 	Handle          func(*Request) error
 }
diff --git a/cmd/set.go b/cmd/set.go
index a40e5ba..5c66f1b 100644
--- a/cmd/set.go
+++ b/cmd/set.go
@@ -24,12 +24,24 @@ import (
 
 func init() {
 	AddCommand(&Command{
-		Name:        "set",
-		Help:        "Configures options for cmk",
-		SubCommands: []string{"block", "output", "profile"},
+		Name: "set",
+		Help: "Configures options for cmk",
+		SubCommands: map[string][]string{
+			"asyncblock": {"true", "false"},
+			"timeout":    {"600", "1800", "3600"},
+			"output":     {"json", "text", "table", "xml"},
+			"profile":    {},
+			"url":        {},
+			"username":   {},
+			"password":   {},
+			"domain":     {},
+			"apikey":     {},
+			"secretkey":  {},
+			"verifycert": {"true", "false"},
+		},
 		Handle: func(r *Request) error {
 			if len(r.Args) < 1 {
-				fmt.Println("Please select one of the sub-commands: ", strings.Join(r.Command.SubCommands, ", "))
+				fmt.Println("Please provide one of the sub-commands: ", r.Command.SubCommands)
 				return nil
 			}
 			subCommand := r.Args[0]
diff --git a/config/cache.go b/config/cache.go
index 002ddf3..a037a95 100644
--- a/config/cache.go
+++ b/config/cache.go
@@ -130,7 +130,7 @@ func (c *Config) UpdateCache(response map[string]interface{}) interface{} {
 				sort.Strings(related)
 			}
 			apiArgs = append(apiArgs, &APIArg{
-				Name:        apiArg["name"].(string),
+				Name:        apiArg["name"].(string) + "=",
 				Type:        apiArg["type"].(string),
 				Required:    apiArg["required"].(bool),
 				Related:     related,
diff --git a/config/config.go b/config/config.go
index df6d4c1..16b2b64 100644
--- a/config/config.go
+++ b/config/config.go
@@ -99,6 +99,13 @@ func defaultConfig() *Config {
 	}
 }
 
+var profiles []string
+
+// GetProfiles returns list of available profiles
+func GetProfiles() []string {
+	return profiles
+}
+
 func reloadConfig(cfg *Config) *Config {
 
 	if _, err := os.Stat(cfg.Dir); err != nil {
@@ -155,6 +162,16 @@ func reloadConfig(cfg *Config) *Config {
 	}
 	// Save
 	conf.SaveTo(cfg.ConfigFile)
+
+	// Update available profiles list
+	profiles = []string{}
+	for _, profile := range conf.Sections() {
+		if profile.Name() == ini.DEFAULT_SECTION {
+			continue
+		}
+		profiles = append(profiles, profile.Name())
+	}
+
 	return cfg
 }
 

-- 
To stop receiving notification emails like this one, please contact
rohit@apache.org.