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/10/21 19:17:32 UTC

[cloudstack-cloudmonkey] 04/05: cmk: implement command line flags support and usage doc

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

commit 783a0996c3277c6ef684e66e76b2e30195c0286d
Author: Rohit Yadav <ro...@apache.org>
AuthorDate: Mon Oct 22 00:45:20 2018 +0530

    cmk: implement command line flags support and usage doc
    
    Signed-off-by: Rohit Yadav <ro...@apache.org>
---
 cmd/command.go | 11 ++++++++---
 cmd/set.go     |  2 +-
 cmk.go         | 32 ++++++++++++++++++++++++++++++--
 3 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/cmd/command.go b/cmd/command.go
index 179dd74..5788ade 100644
--- a/cmd/command.go
+++ b/cmd/command.go
@@ -56,14 +56,19 @@ func AddCommand(cmd *Command) {
 func PrintUsage() {
 	commandHelp := ""
 	for _, cmd := range commands {
-		commandHelp += fmt.Sprintf("%s\t\t%s\n", cmd.Name, cmd.Help)
+		commandHelp += fmt.Sprintf("  %-8s  %s\n", cmd.Name, cmd.Help)
 	}
-	fmt.Printf(`Usage: cmk [options] [commands]
+	fmt.Printf(`usage: cmk [flags] [commands|apis] [-h]
 
 CloudMonkey (cmk) 🐵 is a command line interface for Apache CloudStack.
 
+Allowed flags:
+  -h        Show this help message or API doc when specified after an API
+  -v        Print version
+  -o        API response output format: json, text, table, column, csv
+  -p        Server profile
+
 Default commands:
 %s
-Try cmk [help] or cmk [action api] -h
 `, commandHelp)
 }
diff --git a/cmd/set.go b/cmd/set.go
index 4bc29e3..c500190 100644
--- a/cmd/set.go
+++ b/cmd/set.go
@@ -47,7 +47,7 @@ func init() {
 			}
 			subCommand := r.Args[0]
 			value := strings.Join(r.Args[1:], " ")
-			r.Config.UpdateConfig(subCommand, value)
+			r.Config.UpdateConfig(subCommand, value, true)
 
 			if subCommand == "profile" && r.Config.HasShell {
 				fmt.Println("Loaded server profile:", r.Config.Core.ProfileName)
diff --git a/cmk.go b/cmk.go
index 7175a2d..19d50b2 100644
--- a/cmk.go
+++ b/cmk.go
@@ -18,16 +18,44 @@
 package main
 
 import (
+	"flag"
 	"fmt"
 	"os"
 
 	"github.com/apache/cloudstack-cloudmonkey/cli"
+	"github.com/apache/cloudstack-cloudmonkey/cmd"
 	"github.com/apache/cloudstack-cloudmonkey/config"
 )
 
+func init() {
+	flag.Usage = func() {
+		cmd.PrintUsage()
+	}
+}
+
 func main() {
-	args := os.Args[1:]
-	cli.SetConfig(config.NewConfig())
+	outputFormat := flag.String("o", "", "output format: json, text, table, column, csv")
+	showVersion := flag.Bool("v", false, "show version")
+	profile := flag.String("p", "", "server profile")
+	flag.Parse()
+
+	cfg := config.NewConfig()
+
+	if *showVersion {
+		fmt.Println(cfg.Name(), cfg.Version())
+		os.Exit(0)
+	}
+
+	if *outputFormat != "" {
+		cfg.UpdateConfig("output", *outputFormat, false)
+	}
+
+	if *profile != "" {
+		cfg.UpdateConfig("profile", *profile, false)
+	}
+
+	cli.SetConfig(cfg)
+	args := flag.Args()
 	if len(args) > 0 {
 		if err := cli.ExecCmd(args); err != nil {
 			fmt.Println("🙈 Error:", err)