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 e83ce7f cmk: fix presentational issues e83ce7f is described below commit e83ce7f96896b48ca13eb0c691caecd1fc8660d4 Author: Rohit Yadav <ro...@apache.org> AuthorDate: Mon Apr 16 21:20:16 2018 +0530 cmk: fix presentational issues Signed-off-by: Rohit Yadav <ro...@apache.org> --- cli/completer.go | 10 +++++---- cmd/api.go | 2 +- cmd/help.go | 8 +++++-- cmd/network.go | 5 ++--- config/config.go | 65 ++++++++++++++++++++++++++++++++++---------------------- 5 files changed, 55 insertions(+), 35 deletions(-) diff --git a/cli/completer.go b/cli/completer.go index 8ade3f6..2444fd2 100644 --- a/cli/completer.go +++ b/cli/completer.go @@ -174,17 +174,18 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int) return } + argName := strings.Replace(arg.Name, "=", "", -1) var autocompleteAPI *config.API var relatedNoun string - if arg.Name == "id=" || arg.Name == "ids=" { + if argName == "id" || argName == "ids" { relatedNoun = apiFound.Noun if apiFound.Verb != "list" { relatedNoun += "s" } - } else if arg.Name == "account=" { + } else if argName == "account" { relatedNoun = "accounts" } else { - relatedNoun = strings.Replace(strings.Replace(arg.Name, "ids=", "", -1), "id=", "", -1) + "s" + relatedNoun = strings.Replace(strings.Replace(argName, "ids", "", -1), "id", "", -1) + "s" } for _, related := range apiMap["list"] { if relatedNoun == related.Noun { @@ -202,7 +203,9 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int) if autocompleteAPI.Noun == "templates" { autocompleteAPIArgs = append(autocompleteAPIArgs, "templatefilter=all") } + fmt.Printf("\nFetching options, please wait...") response, _ := cmd.NewAPIRequest(r, autocompleteAPI.Name, autocompleteAPIArgs) + fmt.Printf("\r") var autocompleteOptions []selectOption for _, v := range response { @@ -240,7 +243,6 @@ func (t *autoCompleter) Do(line []rune, pos int) (options [][]rune, offset int) sort.Slice(autocompleteOptions, func(i, j int) bool { return autocompleteOptions[i].Name < autocompleteOptions[j].Name }) - fmt.Println() selectedOption := showSelector(autocompleteOptions) if strings.HasSuffix(arg.Name, "id=") || strings.HasSuffix(arg.Name, "ids=") { selected = selectedOption.ID diff --git a/cmd/api.go b/cmd/api.go index 47a0f0f..215e58c 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -68,7 +68,7 @@ func init() { } } if !provided { - missingArgs = append(missingArgs, required) + missingArgs = append(missingArgs, strings.Replace(required, "=", "", -1)) } } diff --git a/cmd/help.go b/cmd/help.go index dc31e51..d027510 100644 --- a/cmd/help.go +++ b/cmd/help.go @@ -45,14 +45,18 @@ func init() { fmt.Println("This API is \033[35masynchronous\033[0m.") } if len(api.RequiredArgs) > 0 { - fmt.Println("Required params:", strings.Join(api.RequiredArgs, ", ")) + fmt.Printf("Required params: ") + for _, requiredArg := range api.RequiredArgs { + fmt.Printf("%s, ", strings.Replace(requiredArg, "=", "", -1)) + } + fmt.Println() } if len(api.Args) > 0 { fmt.Printf("%-24s %-8s %s\n", "API Params", "Type", "Description") fmt.Printf("%-24s %-8s %s\n", "==========", "====", "===========") } for _, arg := range api.Args { - fmt.Printf("\033[36m%-24s\033[0m \033[32m%-8s\033[0m ", arg.Name, arg.Type) + fmt.Printf("\033[36m%-24s\033[0m \033[32m%-8s\033[0m ", strings.Replace(arg.Name, "=", "", -1), arg.Type) info := []rune(arg.Description) for i, r := range info { fmt.Printf("%s", string(r)) diff --git a/cmd/network.go b/cmd/network.go index 1a57494..19cac73 100644 --- a/cmd/network.go +++ b/cmd/network.go @@ -110,6 +110,7 @@ func NewAPIRequest(r *Request, api string, args []string) (map[string]interface{ var client *http.Client var encodedParams string var err error + if len(r.Config.ActiveProfile.APIKey) > 0 && len(r.Config.ActiveProfile.SecretKey) > 0 { apiKey := r.Config.ActiveProfile.APIKey secretKey := r.Config.ActiveProfile.SecretKey @@ -142,10 +143,8 @@ func NewAPIRequest(r *Request, api string, args []string) (map[string]interface{ return nil, errors.New("failed to authenticate to make API call") } - apiURL := fmt.Sprintf("%s?%s", r.Config.ActiveProfile.URL, encodedParams) - client.Timeout = time.Duration(time.Duration(r.Config.Core.Timeout) * time.Second) - response, err := client.Get(apiURL) + response, err := client.Get(fmt.Sprintf("%s?%s", r.Config.ActiveProfile.URL, encodedParams)) if err != nil { fmt.Println("Error:", err) return nil, err diff --git a/config/config.go b/config/config.go index 97fa6da..fe18837 100644 --- a/config/config.go +++ b/config/config.go @@ -73,29 +73,39 @@ func getDefaultConfigDir() string { return path.Join(home, ".cmk") } +func defaultCoreConfig() Core { + return Core{ + AsyncBlock: false, + Timeout: 1800, + Output: JSON, + ProfileName: "local", + } +} + +func defaultProfile() ServerProfile { + return ServerProfile{ + URL: "http://localhost:8080/client/api", + Username: "admin", + Password: "password", + Domain: "/", + APIKey: "", + SecretKey: "", + VerifyCert: false, + } +} + func defaultConfig() *Config { configDir := getDefaultConfigDir() + defaultCoreConfig := defaultCoreConfig() + defaultProfile := defaultProfile() return &Config{ - Dir: configDir, - ConfigFile: path.Join(configDir, "config"), - CacheFile: path.Join(configDir, "cache"), - HistoryFile: path.Join(configDir, "history"), - LogFile: path.Join(configDir, "log"), - Core: &Core{ - AsyncBlock: false, - Timeout: 1800, - Output: JSON, - ProfileName: "local", - }, - ActiveProfile: &ServerProfile{ - URL: "http://localhost:8080/client/api", - Username: "admin", - Password: "password", - Domain: "/", - APIKey: "", - SecretKey: "", - VerifyCert: false, - }, + Dir: configDir, + ConfigFile: path.Join(configDir, "config"), + CacheFile: path.Join(configDir, "cache"), + HistoryFile: path.Join(configDir, "history"), + LogFile: path.Join(configDir, "log"), + Core: &defaultCoreConfig, + ActiveProfile: &defaultProfile, } } @@ -114,10 +124,11 @@ func reloadConfig(cfg *Config) *Config { // Save on missing config if _, err := os.Stat(cfg.ConfigFile); err != nil { - defaultConf := defaultConfig() + defaultCoreConfig := defaultCoreConfig() + defaultProfile := defaultProfile() conf := ini.Empty() - conf.Section(ini.DEFAULT_SECTION).ReflectFrom(defaultConf.Core) - conf.Section(cfg.Core.ProfileName).ReflectFrom(defaultConf.ActiveProfile) + conf.Section(ini.DEFAULT_SECTION).ReflectFrom(&defaultCoreConfig) + conf.Section(defaultCoreConfig.ProfileName).ReflectFrom(&defaultProfile) conf.SaveTo(cfg.ConfigFile) } @@ -133,8 +144,10 @@ func reloadConfig(cfg *Config) *Config { core, err := conf.GetSection(ini.DEFAULT_SECTION) if core == nil { + defaultCore := defaultCoreConfig() section, _ := conf.NewSection(ini.DEFAULT_SECTION) - section.ReflectFrom(&defaultConfig().Core) + section.ReflectFrom(&defaultCore) + cfg.Core = &defaultCore } else { // Write if cfg.Core != nil { @@ -148,8 +161,10 @@ func reloadConfig(cfg *Config) *Config { profile, err := conf.GetSection(cfg.Core.ProfileName) if profile == nil { + activeProfile := defaultProfile() section, _ := conf.NewSection(cfg.Core.ProfileName) - section.ReflectFrom(&defaultConfig().ActiveProfile) + section.ReflectFrom(&activeProfile) + cfg.ActiveProfile = &activeProfile } else { // Write if cfg.ActiveProfile != nil { -- To stop receiving notification emails like this one, please contact ro...@apache.org.