From be73bad33fc245223fb2f1c73b92f1c12c6e698e Mon Sep 17 00:00:00 2001 From: Alberto Murillo Date: Wed, 22 Jun 2016 15:02:24 -0500 Subject: [PATCH] ciao-cli: reduce the output of ciao-cli instance list ciao-cli instance list output shows a detailed information for each instance that is running taking 1 line per property making the output difficult to digest by humans trying to seek fast the instance of interest. For example: Instance #1 UUID: 37089506-dfc4-411c-ae57-c261ef891a7f Status: running Private IP: 172.16.0.3 MAC Address: 02:00:ac:10:00:03 CN UUID: 1d1f0791-eb93-4947-893f-8b325433dea7 Image UUID: 73a86d7e-93c0-480e-9c41-ab42f69b7799 Tenant UUID: effa268c7350464797e2794b798658aa SSH IP: 192.168.10.180 SSH Port: 33003 This commit makes instance list print a table with the most relevant information of each instance as a field, taking 1 line per instance. For Example: # UUID Status Private IP SSH IP SSH PORT 1 37089506-dfc4-411c-ae57-c261ef891a7f running 172.16.0.3 192.168.10.180 33003 2 49e5d95a-1e40-4b3f-988a-183b607817c4 running 172.16.0.2 192.168.10.180 33002 Also adds the instance list -detail option to print the full information for each instance since this output can be consumed by scripts. Signed-off-by: Alberto Murillo --- ciao-cli/instance.go | 47 +++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/ciao-cli/instance.go b/ciao-cli/instance.go index 575c31d16..5458792db 100644 --- a/ciao-cli/instance.go +++ b/ciao-cli/instance.go @@ -24,6 +24,7 @@ import ( "fmt" "net/http" "os" + "text/tabwriter" "github.com/01org/ciao/payloads" ) @@ -279,6 +280,7 @@ type instanceListCommand struct { limit int cn string tenant string + detail bool } func (cmd *instanceListCommand) usage(...string) { @@ -294,6 +296,7 @@ The list flags are: } func (cmd *instanceListCommand) parseArgs(args []string) []string { + cmd.Flag.BoolVar(&cmd.detail, "detail", false, "Print detailed information about each instance") cmd.Flag.StringVar(&cmd.workload, "workload", "", "Workload UUID") cmd.Flag.StringVar(&cmd.cn, "cn", "", "Computer node to list instances from (default to all nodes when empty)") cmd.Flag.StringVar(&cmd.marker, "marker", "", "Show instance list starting from the next instance after marker") @@ -355,18 +358,40 @@ func (cmd *instanceListCommand) run(args []string) error { fatalf(err.Error()) } + w := new(tabwriter.Writer) + if !cmd.detail { + w.Init(os.Stdout, 0, 1, 1, ' ', 0) + fmt.Fprintln(w, "#\tUUID\tStatus\tPrivate IP\tSSH IP\tSSH PORT") + } + for i, server := range servers.Servers { - fmt.Printf("Instance #%d\n", i+1) - fmt.Printf("\tUUID: %s\n", server.ID) - fmt.Printf("\tStatus: %s\n", server.Status) - fmt.Printf("\tPrivate IP: %s\n", server.Addresses.Private[0].Addr) - fmt.Printf("\tMAC Address: %s\n", server.Addresses.Private[0].OSEXTIPSMACMacAddr) - fmt.Printf("\tCN UUID: %s\n", server.HostID) - fmt.Printf("\tImage UUID: %s\n", server.Image.ID) - fmt.Printf("\tTenant UUID: %s\n", server.TenantID) - if server.SSHIP != "" { - fmt.Printf("\tSSH IP: %s\n", server.SSHIP) - fmt.Printf("\tSSH Port: %d\n", server.SSHPort) + if !cmd.detail { + fmt.Fprintf(w, "%d", i+1) + fmt.Fprintf(w, "\t%s", server.ID) + fmt.Fprintf(w, "\t%s", server.Status) + fmt.Fprintf(w, "\t%s", server.Addresses.Private[0].Addr) + if server.SSHIP != "" { + fmt.Fprintf(w, "\t%s", server.SSHIP) + fmt.Fprintf(w, "\t%d\n", server.SSHPort) + } else { + fmt.Fprintf(w, "\tN/A") + fmt.Fprintf(w, "\tN/A\n") + } + + w.Flush() + } else { + fmt.Printf("Instance #%d\n", i+1) + fmt.Printf("\tUUID: %s\n", server.ID) + fmt.Printf("\tStatus: %s\n", server.Status) + fmt.Printf("\tPrivate IP: %s\n", server.Addresses.Private[0].Addr) + fmt.Printf("\tMAC Address: %s\n", server.Addresses.Private[0].OSEXTIPSMACMacAddr) + fmt.Printf("\tCN UUID: %s\n", server.HostID) + fmt.Printf("\tImage UUID: %s\n", server.Image.ID) + fmt.Printf("\tTenant UUID: %s\n", server.TenantID) + if server.SSHIP != "" { + fmt.Printf("\tSSH IP: %s\n", server.SSHIP) + fmt.Printf("\tSSH Port: %d\n", server.SSHPort) + } } } return nil