From 9a5c478564c4f3bb81f0c405919315fad2cc31a2 Mon Sep 17 00:00:00 2001
From: lijianguo <lijianguo@qq.com>
Date: Sat, 19 Sep 2020 21:17:25 +0800
Subject: [PATCH] chore(workflow-cli):pretty volumes list and resources list
 Printing

---
 cmd/resources.go      | 26 ++++++++++++++++++++++----
 cmd/resources_test.go |  2 +-
 cmd/volumes.go        | 26 +++++++++++++++++++++++---
 cmd/volumes_test.go   | 13 ++++---------
 4 files changed, 50 insertions(+), 17 deletions(-)

diff --git a/cmd/resources.go b/cmd/resources.go
index a03595053..26e4db329 100644
--- a/cmd/resources.go
+++ b/cmd/resources.go
@@ -191,9 +191,16 @@ func (d *DryccCmd) ResourceUnbind(appID string, name string) error {
 func printResources(d *DryccCmd, appID string, resources api.Resources, wOut io.Writer) {
 
 	fmt.Fprintf(wOut, "=== %s resources\n", appID)
+	resourceNames := make([]string, len(resources))
 
 	for _, resource := range resources {
-		fmt.Fprintf(wOut, "%s\t%s\n", resource.Name, resource.Plan)
+		resourceNames = append(resourceNames, resource.Name)
+	}
+	lenResourceNames := sliceMaxLen(resourceNames) + 5
+
+	for _, resource := range resources {
+		spaces := strings.Repeat(" ", lenResourceNames-len(resource.Name))
+		fmt.Fprintf(wOut, "%s%s%s\n", resource.Name, spaces, resource.Plan)
 	}
 }
 
@@ -208,8 +215,8 @@ func printResourceDetail(d *DryccCmd, appID string, resource api.Resource, wOut
 	for key, value := range resource.Options {
 		optionsMap[key+":"] = fmt.Sprintf("%v", value)
 	}
-	lenDataMap := maxLen(dataMap)
-	lenOptionsMap := maxLen(optionsMap)
+	lenDataMap := mapMaxLen(dataMap)
+	lenOptionsMap := mapMaxLen(optionsMap)
 	tempArray := []int{lenDataMap, lenOptionsMap, len("binding:")}
 	max := maxNum(tempArray...) + 5
 	d.Print(fmt.Sprintf("plan:%s%s\n", strings.Repeat(" ", max-len("plan:")), resource.Plan))
@@ -226,7 +233,7 @@ func printResourceDetail(d *DryccCmd, appID string, resource api.Resource, wOut
 	}
 }
 
-func maxLen(msg map[string]string) int {
+func mapMaxLen(msg map[string]string) int {
 	// find the longest key so we know how much padding to use
 	max := 0
 	for key := range msg {
@@ -237,6 +244,17 @@ func maxLen(msg map[string]string) int {
 	return max
 }
 
+func sliceMaxLen(msgs []string) int {
+	// find the longest member so we know how much padding to use
+	max := 0
+	for _, msg := range msgs {
+		if len(msg) > max {
+			max = len(msg)
+		}
+	}
+	return max
+}
+
 func maxNum(tempArray ...int) int {
 	// find the longest num so we know how much padding to use
 	max := 0
diff --git a/cmd/resources_test.go b/cmd/resources_test.go
index 7083a58f8..6e6032605 100644
--- a/cmd/resources_test.go
+++ b/cmd/resources_test.go
@@ -73,7 +73,7 @@ func TestResourcesList(t *testing.T) {
 	assert.NoErr(t, err)
 
 	assert.Equal(t, b.String(), `=== example-go resources
-mysql	mysql:5.6
+mysql     mysql:5.6
 `, "output")
 }
 
diff --git a/cmd/volumes.go b/cmd/volumes.go
index 06efb2704..28d85b9b0 100644
--- a/cmd/volumes.go
+++ b/cmd/volumes.go
@@ -2,8 +2,10 @@ package cmd
 
 import (
 	"fmt"
+	"github.com/drycc/pkg/prettyprint"
 	"io"
 	"regexp"
+	"strings"
 
 	"github.com/drycc/controller-sdk-go/api"
 	"github.com/drycc/controller-sdk-go/volumes"
@@ -39,11 +41,29 @@ func printVolumes(d *DryccCmd, appID string, volumes api.Volumes, wOut io.Writer
 
 	fmt.Fprintf(wOut, "=== %s volumes\n", appID)
 
+	var max int
 	for _, volume := range volumes {
-		fmt.Fprintf(wOut, "--- %s\t%s\n", volume.Name, volume.Size)
-		for k, v := range volume.Path {
-			fmt.Fprintf(wOut, "%s\t\t%s\n", k, v)
+		if max < (len(volume.Name) + 4){
+			max = len(volume.Name) + 4
 		}
+		for key, _ := range volume.Path {
+			if max < len(key){
+				max = len(key)
+			}
+		}
+	}
+	max = max + 5
+
+	for _, volume := range volumes {
+		nameSpaces := strings.Repeat(" ", max-len(volume.Name)-4)
+		fmt.Fprintf(wOut, "--- %s%s%s\n", volume.Name, nameSpaces, volume.Size)
+
+		pathMap := make(map[string]string)
+		for key, value := range volume.Path {
+			pathMap[key] = fmt.Sprintf("%v", value)
+		}
+		lenDataMap := mapMaxLen(pathMap)
+		d.Print(prettyprint.PrettyTabs(pathMap, max-lenDataMap))
 	}
 }
 
diff --git a/cmd/volumes_test.go b/cmd/volumes_test.go
index 7a6324b52..f1a8db4b4 100644
--- a/cmd/volumes_test.go
+++ b/cmd/volumes_test.go
@@ -34,7 +34,7 @@ func TestVolumesList(t *testing.T) {
 			"app": "example-go",
 			"name": "myvolume",
 			"size": "500M",
-			"path": {},
+			"path": {"cmd": "/data/cmd1", "cmd123": "/data/cmd123"},
 			"created": "2020-08-26T00:00:00UTC",
 			"updated": "2020-08-26T00:00:00UTC"
 		}
@@ -46,7 +46,9 @@ func TestVolumesList(t *testing.T) {
 	assert.NoErr(t, err)
 
 	assert.Equal(t, b.String(), `=== example-go volumes
---- myvolume	500M
+--- myvolume     500M
+cmd              /data/cmd1
+cmd123           /data/cmd123
 `, "output")
 }
 
@@ -132,10 +134,6 @@ func TestVolumesMount(t *testing.T) {
 
 	assert.Equal(t, testutil.StripProgress(b.String()), `Mounting volume... done
 `, "output")
-	//
-	//=== example-go Volumes
-	//--- myvolume   500M
-	//cmd /data/cmd1
 }
 
 func TestVolumesUnmount(t *testing.T) {
@@ -175,7 +173,4 @@ func TestVolumesUnmount(t *testing.T) {
 
 	assert.Equal(t, testutil.StripProgress(b.String()), `Unmounting volume... done
 `, "output")
-
-	//=== example-go Volumes
-	//--- myvolume   500M
 }
