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 00:03:28 UTC
[cloudstack-cloudmonkey] branch master updated: help: fix help to
work with -h and help
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 15697d8 help: fix help to work with -h and help <api>
15697d8 is described below
commit 15697d888550fb6439467c33196e4530c6299fa7
Author: Rohit Yadav <ro...@apache.org>
AuthorDate: Sat Apr 14 05:32:58 2018 +0530
help: fix help to work with -h and help <api>
Signed-off-by: Rohit Yadav <ro...@apache.org>
---
cli/selector.go | 10 +++++-----
cmd/api.go | 34 +++++++---------------------------
cmd/help.go | 50 ++++++++++++++++++++++++++++++++++++++++++++------
cmd/network.go | 1 -
cmd/shell.go | 44 --------------------------------------------
config/cache.go | 2 +-
6 files changed, 57 insertions(+), 84 deletions(-)
diff --git a/cli/selector.go b/cli/selector.go
index 104e16e..19933f9 100644
--- a/cli/selector.go
+++ b/cli/selector.go
@@ -59,10 +59,10 @@ func showSelector(options []selectOption) selectOption {
defer optionSelector.unlock()
templates := &promptui.SelectTemplates{
- Label: "{{ . }}",
- Active: "โถ {{ .Name | cyan }} ({{ .ID | red }})",
- Inactive: " {{ .Name | cyan }} ({{ .ID | red }})",
- Selected: "๐Selected: {{ .Name | cyan }} ({{ .ID | red }})",
+ Label: "{{ . | magenta }}",
+ Active: "โถ {{ .Name | cyan }} ({{ .ID | green }})",
+ Inactive: " {{ .Name }} ({{ .ID }})",
+ Selected: "Selected option: {{ .Name | cyan }} ({{ .ID | green }})",
Details: `
--------- Current Selection ----------
{{ "ID:" | faint }} {{ .ID }}
@@ -79,7 +79,7 @@ func showSelector(options []selectOption) selectOption {
}
prompt := promptui.Select{
- Label: "Use the arrow keys to navigate: โ โ โ โ press / to toggle ๐search",
+ Label: "Use the arrow keys to navigate: โ โ โ โ press / to toggle ๐ search",
Items: options,
Templates: templates,
Size: 5,
diff --git a/cmd/api.go b/cmd/api.go
index e185a74..facb25a 100644
--- a/cmd/api.go
+++ b/cmd/api.go
@@ -47,37 +47,18 @@ func init() {
apiArgs = r.Args[2:]
}
+ for _, arg := range r.Args {
+ if arg == "-h" {
+ r.Args[0] = apiName
+ return helpCommand.Handle(r)
+ }
+ }
+
api := r.Config.GetCache()[apiName]
if api == nil {
return errors.New("unknown command or API requested")
}
- if strings.Contains(strings.Join(apiArgs, " "), "-h") {
- fmt.Printf("\033[34m%s\033[0m [async=%v] %s\n", api.Name, api.Async, api.Description)
- if len(api.RequiredArgs) > 0 {
- fmt.Println("Required params:", strings.Join(api.RequiredArgs, ", "))
- }
- 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[35m%-24s\033[0m \033[36m%-8s\033[0m ", arg.Name, arg.Type)
- info := []rune(arg.Description)
- for i, r := range info {
- fmt.Printf("%s", string(r))
- if i > 0 && i%45 == 0 {
- fmt.Println()
- for i := 0; i < 34; i++ {
- fmt.Printf(" ")
- }
- }
- }
- fmt.Println()
- }
- return nil
- }
-
var missingArgs []string
for _, required := range api.RequiredArgs {
provided := false
@@ -104,5 +85,4 @@ func init() {
return nil
},
}
- AddCommand(apiCommand)
}
diff --git a/cmd/help.go b/cmd/help.go
index d038522..dc31e51 100644
--- a/cmd/help.go
+++ b/cmd/help.go
@@ -17,18 +17,56 @@
package cmd
+import (
+ "errors"
+ "fmt"
+ "strings"
+)
+
+var helpCommand *Command
+
func init() {
- AddCommand(&Command{
+ helpCommand = &Command{
Name: "help",
Help: "Help",
Handle: func(r *Request) error {
- if len(r.Args) < 1 {
+ if len(r.Args) < 1 || r.Args[0] == "-h" {
PrintUsage()
return nil
}
- //TODO: check it's not other commands?
- r.Args = append(r.Args, "-h")
- return apiCommand.Handle(r)
+
+ api := r.Config.GetCache()[strings.ToLower(r.Args[0])]
+ if api == nil {
+ return errors.New("unknown command or API requested")
+ }
+
+ fmt.Printf("\033[34m%s\033[0m: %s\n", api.Name, api.Description)
+ if api.Async {
+ fmt.Println("This API is \033[35masynchronous\033[0m.")
+ }
+ if len(api.RequiredArgs) > 0 {
+ fmt.Println("Required params:", strings.Join(api.RequiredArgs, ", "))
+ }
+ 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)
+ info := []rune(arg.Description)
+ for i, r := range info {
+ fmt.Printf("%s", string(r))
+ if i > 0 && i%40 == 0 {
+ fmt.Println()
+ for i := 0; i < 34; i++ {
+ fmt.Printf(" ")
+ }
+ }
+ }
+ fmt.Println()
+ }
+ return nil
},
- })
+ }
+ AddCommand(helpCommand)
}
diff --git a/cmd/network.go b/cmd/network.go
index 0eb973d..d40b7bd 100644
--- a/cmd/network.go
+++ b/cmd/network.go
@@ -84,7 +84,6 @@ func NewAPIRequest(r *Request, api string, args []string) (map[string]interface{
apiURL := fmt.Sprintf("%s?%s", r.Config.Core.ActiveProfile.URL, encodedParams)
- //fmt.Println("[debug] Requesting: ", apiURL)
response, err := http.Get(apiURL)
if err != nil {
fmt.Println("Error:", err)
diff --git a/cmd/shell.go b/cmd/shell.go
deleted file mode 100644
index 7bbbfcd..0000000
--- a/cmd/shell.go
+++ /dev/null
@@ -1,44 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-
-package cmd
-
-import (
- "errors"
- "fmt"
- "os/exec"
- "strings"
-)
-
-func init() {
- AddCommand(&Command{
- Name: "shell",
- Help: "Drops into a shell",
- Handle: func(r *Request) error {
- cmd := strings.TrimSpace(strings.Join(r.Args, " "))
- if len(cmd) < 1 {
- return errors.New("no shell command provided")
- }
- out, err := exec.Command("bash", "-c", cmd).Output()
- if err == nil {
- fmt.Println(string(out))
- return nil
- }
- return errors.New("failed to execute command, " + err.Error())
- },
- })
-}
diff --git a/config/cache.go b/config/cache.go
index 8a4501a..002ddf3 100644
--- a/config/cache.go
+++ b/config/cache.go
@@ -78,7 +78,7 @@ func (c *Config) GetCache() map[string]*API {
func LoadCache(c *Config) {
cache, err := ioutil.ReadFile(c.CacheFile)
if err != nil {
- fmt.Println("Please run sync. Failed to read the cache file: " + c.CacheFile)
+ fmt.Println("Please run sync, failed to read the cache file: " + c.CacheFile)
return
}
var data map[string]interface{}
--
To stop receiving notification emails like this one, please contact
rohit@apache.org.