Skip to content

Commit 163dda5

Browse files
Merge pull request #2 from swisscom/feature/tables
Add proper tables
2 parents 72b01a8 + bd530b6 commit 163dda5

8 files changed

Lines changed: 147 additions & 57 deletions

backups.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,24 @@ func (p *AppCloudPlugin) Backups(c plugin.CliConnection, serviceInstanceName str
4444
fmt.Print(greenBold("OK\n\n"))
4545

4646
backups := res.Resources
47-
if len(backups) == 0 {
48-
fmt.Println("No backups found")
49-
return nil
50-
}
51-
52-
fmt.Println(bold(" created at GUID last operation"))
53-
for i, b := range backups {
54-
var newestRestoreDate time.Time
55-
overallStatus := b.Entity.Status
56-
57-
for _, r := range b.Entity.Restores {
58-
if r.Metadata.CreatedAt.After(newestRestoreDate) {
59-
overallStatus = fmt.Sprintf("RESTORE %s", r.Entity.Status)
60-
newestRestoreDate = r.Metadata.CreatedAt
47+
if len(backups) > 0 {
48+
table := NewTable([]string{"created at", "GUID", "last operation"})
49+
for _, b := range backups {
50+
var newestRestoreDate time.Time
51+
overallStatus := b.Entity.Status
52+
53+
for _, r := range b.Entity.Restores {
54+
if r.Metadata.CreatedAt.After(newestRestoreDate) {
55+
overallStatus = fmt.Sprintf("RESTORE %s", r.Entity.Status)
56+
newestRestoreDate = r.Metadata.CreatedAt
57+
}
6158
}
62-
}
6359

64-
fmt.Printf("#%v %s %s %s\n", i, b.Metadata.CreatedAt.Format(time.RFC3339), b.Metadata.GUID, formatStatus(overallStatus))
60+
table.Add(b.Metadata.CreatedAt.Format(time.RFC3339), b.Metadata.GUID, formatStatus(overallStatus))
61+
}
62+
table.Print()
63+
} else {
64+
fmt.Println("No backups found")
6565
}
6666
return nil
6767
}

billing_account_invitations.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ func (p *AppCloudPlugin) BillingAccountInvitations(c plugin.CliConnection, billi
4242

4343
fmt.Println(greenBold("OK\n\n"))
4444

45-
if len(res.Resources) == 0 {
45+
invitations := res.Resources
46+
if len(invitations) > 0 {
47+
table := NewTable([]string{"invitee", "roles", "status"})
48+
for _, inv := range res.Resources {
49+
table.Add(inv.Entity.Invitee, strings.Join(inv.Entity.Roles, ","), inv.Entity.Status)
50+
}
51+
table.Print()
52+
} else {
4653
fmt.Println("No invitations found")
47-
return nil
48-
}
49-
50-
fmt.Println(bold("Invitee roles status"))
51-
for _, inv := range res.Resources {
52-
fmt.Printf("%s %s %s\n", inv.Entity.Invitee, inv.Entity.Roles, inv.Entity.Status)
5354
}
5455
return nil
5556
}

invitations.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ func (p *AppCloudPlugin) Invitations(c plugin.CliConnection) error {
2222

2323
fmt.Print(greenBold("OK\n\n"))
2424

25-
if len(invitations) == 0 {
25+
if len(invitations) > 0 {
26+
table := NewTable([]string{"GUID", "entity type", "entity"})
27+
for _, inv := range invitations {
28+
entityType, entityName := invitationEntityTypeAndName(inv)
29+
table.Add(inv.Metadata.GUID, formatEntityType(entityType), entityName)
30+
}
31+
table.Print()
32+
} else {
2633
fmt.Println("No invitations found")
27-
return nil
28-
}
29-
30-
fmt.Println(bold("GUID entity type entity"))
31-
for _, inv := range invitations {
32-
entityType, entityName := invitationEntityTypeAndName(inv)
33-
fmt.Printf("%s %s %s\n", inv.Metadata.GUID, formatEntityType(entityType), entityName)
3434
}
3535
return nil
3636
}

org_invitations.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ func (p *AppCloudPlugin) OrgInvitations(c plugin.CliConnection, orgName string)
4242

4343
fmt.Println(greenBold("OK\n\n"))
4444

45-
if len(res.Resources) == 0 {
45+
invitations := res.Resources
46+
if len(invitations) > 0 {
47+
table := NewTable([]string{"invitee", "roles", "status"})
48+
for _, inv := range res.Resources {
49+
table.Add(inv.Entity.Invitee, strings.Join(inv.Entity.Roles, ","), inv.Entity.Status)
50+
}
51+
table.Print()
52+
} else {
4653
fmt.Println("No invitations found")
47-
return nil
48-
}
49-
50-
fmt.Println(bold("Invitee status roles"))
51-
for _, inv := range res.Resources {
52-
fmt.Printf("%s %s %s\n", inv.Entity.Invitee, inv.Entity.Status, inv.Entity.Roles)
5354
}
5455
return nil
5556
}

service_events.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ func (p *AppCloudPlugin) ServiceEvents(c plugin.CliConnection, serviceInstanceNa
4444
fmt.Print(greenBold("OK\n\n"))
4545

4646
events := res.Resources
47-
if len(events) == 0 {
47+
if len(events) > 0 {
48+
table := NewTable([]string{"time", "event", "actor"})
49+
for _, e := range events {
50+
table.Add(e.Metadata.CreatedAt.Format(time.RFC3339), e.Entity.Type, e.Entity.ActorName)
51+
}
52+
table.Print()
53+
} else {
4854
fmt.Println("No events found")
49-
return nil
50-
}
51-
52-
fmt.Println(bold("time event"))
53-
for _, e := range events {
54-
fmt.Printf("%s %s %s\n", cyanBold(e.Metadata.CreatedAt.Format(time.RFC3339)), e.Entity.Type, e.Entity.ActorName)
5555
}
5656
return nil
5757
}

space_invitations.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ func (p *AppCloudPlugin) SpaceInvitations(c plugin.CliConnection, spaceName stri
4242

4343
fmt.Println(greenBold("OK\n\n"))
4444

45-
if len(res.Resources) == 0 {
45+
invitations := res.Resources
46+
if len(invitations) > 0 {
47+
table := NewTable([]string{"invitee", "roles", "status"})
48+
for _, inv := range res.Resources {
49+
table.Add(inv.Entity.Invitee, strings.Join(inv.Entity.Roles, ","), inv.Entity.Status)
50+
}
51+
table.Print()
52+
} else {
4653
fmt.Println("No invitations found")
47-
return nil
48-
}
49-
50-
fmt.Println(bold("Invitee roles status"))
51-
for _, inv := range res.Resources {
52-
fmt.Printf("%s %s %s\n", inv.Entity.Invitee, inv.Entity.Roles, inv.Entity.Status)
5354
}
5455
return nil
5556
}

ssl_certificates.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ func (p *AppCloudPlugin) SSLCertificates(c plugin.CliConnection) error {
4242

4343
fmt.Println(greenBold("OK\n\n"))
4444

45-
if len(res.Resources) == 0 {
45+
if len(res.Resources) > 0 {
46+
table := NewTable([]string{"full domain name", "status"})
47+
for _, cert := range res.Resources {
48+
table.Add(cert.Entity.FullDomainName, formatStatus(cert.Entity.Status))
49+
}
50+
table.Print()
51+
} else {
4652
fmt.Println("No SSL certificates found")
4753
}
4854

49-
fmt.Println(bold("full domain name"))
50-
for _, cert := range res.Resources {
51-
fmt.Printf("%s (%s)\n", cert.Entity.FullDomainName, formatStatus(cert.Entity.Status))
52-
}
5355
return nil
5456
}

table.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"unicode/utf8"
7+
)
8+
9+
// Table is a table in the console.
10+
type Table interface {
11+
Add(row ...string)
12+
Print()
13+
}
14+
15+
// PrintableTable is a printable table in the console.
16+
type PrintableTable struct {
17+
headers []string
18+
headerPrinted bool
19+
maxSizes []int
20+
rows [][]string
21+
}
22+
23+
// NewTable creates a new table.
24+
func NewTable(headers []string) Table {
25+
return &PrintableTable{
26+
headers: headers,
27+
maxSizes: make([]int, len(headers)),
28+
}
29+
}
30+
31+
// Add adds a new row to a table.
32+
func (t *PrintableTable) Add(row ...string) {
33+
t.rows = append(t.rows, row)
34+
}
35+
36+
// Print prints a table to stdout.
37+
func (t *PrintableTable) Print() {
38+
for _, row := range append(t.rows, t.headers) {
39+
t.calculateMaxSize(row)
40+
}
41+
42+
if t.headerPrinted == false {
43+
t.printHeader()
44+
t.headerPrinted = true
45+
}
46+
47+
for _, line := range t.rows {
48+
t.printRow(line)
49+
}
50+
51+
t.rows = [][]string{}
52+
}
53+
54+
func (t *PrintableTable) calculateMaxSize(row []string) {
55+
for index, value := range row {
56+
cellLength := utf8.RuneCountInString(value)
57+
if t.maxSizes[index] < cellLength {
58+
t.maxSizes[index] = cellLength
59+
}
60+
}
61+
}
62+
63+
func (t *PrintableTable) printHeader() {
64+
output := ""
65+
for col, value := range t.headers {
66+
output = output + t.cellValue(col, value)
67+
}
68+
fmt.Println(bold(output))
69+
}
70+
71+
func (t *PrintableTable) printRow(row []string) {
72+
output := ""
73+
for columnIndex, value := range row {
74+
output = output + t.cellValue(columnIndex, value)
75+
}
76+
fmt.Println(output)
77+
}
78+
79+
func (t *PrintableTable) cellValue(col int, value string) string {
80+
padding := ""
81+
if col < len(t.headers)-1 {
82+
padding = strings.Repeat(" ", t.maxSizes[col]-utf8.RuneCountInString(value))
83+
}
84+
return fmt.Sprintf("%s%s ", value, padding)
85+
}

0 commit comments

Comments
 (0)