Skip to content

Commit 9a5c478

Browse files
author
lijianguo
committed
chore(workflow-cli):pretty volumes list and resources list Printing
1 parent 992945e commit 9a5c478

4 files changed

Lines changed: 50 additions & 17 deletions

File tree

cmd/resources.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,16 @@ func (d *DryccCmd) ResourceUnbind(appID string, name string) error {
191191
func printResources(d *DryccCmd, appID string, resources api.Resources, wOut io.Writer) {
192192

193193
fmt.Fprintf(wOut, "=== %s resources\n", appID)
194+
resourceNames := make([]string, len(resources))
194195

195196
for _, resource := range resources {
196-
fmt.Fprintf(wOut, "%s\t%s\n", resource.Name, resource.Plan)
197+
resourceNames = append(resourceNames, resource.Name)
198+
}
199+
lenResourceNames := sliceMaxLen(resourceNames) + 5
200+
201+
for _, resource := range resources {
202+
spaces := strings.Repeat(" ", lenResourceNames-len(resource.Name))
203+
fmt.Fprintf(wOut, "%s%s%s\n", resource.Name, spaces, resource.Plan)
197204
}
198205
}
199206

@@ -208,8 +215,8 @@ func printResourceDetail(d *DryccCmd, appID string, resource api.Resource, wOut
208215
for key, value := range resource.Options {
209216
optionsMap[key+":"] = fmt.Sprintf("%v", value)
210217
}
211-
lenDataMap := maxLen(dataMap)
212-
lenOptionsMap := maxLen(optionsMap)
218+
lenDataMap := mapMaxLen(dataMap)
219+
lenOptionsMap := mapMaxLen(optionsMap)
213220
tempArray := []int{lenDataMap, lenOptionsMap, len("binding:")}
214221
max := maxNum(tempArray...) + 5
215222
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
226233
}
227234
}
228235

229-
func maxLen(msg map[string]string) int {
236+
func mapMaxLen(msg map[string]string) int {
230237
// find the longest key so we know how much padding to use
231238
max := 0
232239
for key := range msg {
@@ -237,6 +244,17 @@ func maxLen(msg map[string]string) int {
237244
return max
238245
}
239246

247+
func sliceMaxLen(msgs []string) int {
248+
// find the longest member so we know how much padding to use
249+
max := 0
250+
for _, msg := range msgs {
251+
if len(msg) > max {
252+
max = len(msg)
253+
}
254+
}
255+
return max
256+
}
257+
240258
func maxNum(tempArray ...int) int {
241259
// find the longest num so we know how much padding to use
242260
max := 0

cmd/resources_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestResourcesList(t *testing.T) {
7373
assert.NoErr(t, err)
7474

7575
assert.Equal(t, b.String(), `=== example-go resources
76-
mysql mysql:5.6
76+
mysql mysql:5.6
7777
`, "output")
7878
}
7979

cmd/volumes.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package cmd
22

33
import (
44
"fmt"
5+
"github.com/drycc/pkg/prettyprint"
56
"io"
67
"regexp"
8+
"strings"
79

810
"github.com/drycc/controller-sdk-go/api"
911
"github.com/drycc/controller-sdk-go/volumes"
@@ -39,11 +41,29 @@ func printVolumes(d *DryccCmd, appID string, volumes api.Volumes, wOut io.Writer
3941

4042
fmt.Fprintf(wOut, "=== %s volumes\n", appID)
4143

44+
var max int
4245
for _, volume := range volumes {
43-
fmt.Fprintf(wOut, "--- %s\t%s\n", volume.Name, volume.Size)
44-
for k, v := range volume.Path {
45-
fmt.Fprintf(wOut, "%s\t\t%s\n", k, v)
46+
if max < (len(volume.Name) + 4){
47+
max = len(volume.Name) + 4
4648
}
49+
for key, _ := range volume.Path {
50+
if max < len(key){
51+
max = len(key)
52+
}
53+
}
54+
}
55+
max = max + 5
56+
57+
for _, volume := range volumes {
58+
nameSpaces := strings.Repeat(" ", max-len(volume.Name)-4)
59+
fmt.Fprintf(wOut, "--- %s%s%s\n", volume.Name, nameSpaces, volume.Size)
60+
61+
pathMap := make(map[string]string)
62+
for key, value := range volume.Path {
63+
pathMap[key] = fmt.Sprintf("%v", value)
64+
}
65+
lenDataMap := mapMaxLen(pathMap)
66+
d.Print(prettyprint.PrettyTabs(pathMap, max-lenDataMap))
4767
}
4868
}
4969

cmd/volumes_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestVolumesList(t *testing.T) {
3434
"app": "example-go",
3535
"name": "myvolume",
3636
"size": "500M",
37-
"path": {},
37+
"path": {"cmd": "/data/cmd1", "cmd123": "/data/cmd123"},
3838
"created": "2020-08-26T00:00:00UTC",
3939
"updated": "2020-08-26T00:00:00UTC"
4040
}
@@ -46,7 +46,9 @@ func TestVolumesList(t *testing.T) {
4646
assert.NoErr(t, err)
4747

4848
assert.Equal(t, b.String(), `=== example-go volumes
49-
--- myvolume 500M
49+
--- myvolume 500M
50+
cmd /data/cmd1
51+
cmd123 /data/cmd123
5052
`, "output")
5153
}
5254

@@ -132,10 +134,6 @@ func TestVolumesMount(t *testing.T) {
132134

133135
assert.Equal(t, testutil.StripProgress(b.String()), `Mounting volume... done
134136
`, "output")
135-
//
136-
//=== example-go Volumes
137-
//--- myvolume 500M
138-
//cmd /data/cmd1
139137
}
140138

141139
func TestVolumesUnmount(t *testing.T) {
@@ -175,7 +173,4 @@ func TestVolumesUnmount(t *testing.T) {
175173

176174
assert.Equal(t, testutil.StripProgress(b.String()), `Unmounting volume... done
177175
`, "output")
178-
179-
//=== example-go Volumes
180-
//--- myvolume 500M
181176
}

0 commit comments

Comments
 (0)