Skip to content

Commit d6abc40

Browse files
committed
tests passed
1 parent 79aa731 commit d6abc40

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

internal/util/tree/tree.go

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,15 @@ var (
6565
// PrintObjectTree prints the cluster status to stdout.
6666
// Note: this function is exposed only for usage in clusterctl and Cluster API E2E tests.
6767
func PrintObjectTree(tree *tree.ObjectTree, w io.Writer) {
68+
69+
cfg := getObjectTreeConfig()
6870
// Creates the output table
69-
cfg := getTableTreeConfig()
70-
tbl := tablewriter.NewTable(os.Stdin, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{ // Apply custom rendition
71+
tbl := tablewriter.NewTable(os.Stdin, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{
7172
Settings: tw.Settings{
7273
Separators: tw.SeparatorsNone, Lines: tw.LinesNone,
7374
},
7475
Borders: tw.BorderNone,
75-
}),)
76-
77-
// tbl.SetCenterSeparator("") - rendition - https://github.com/olekukonko/tablewriter/blob/e520aed3bd36fcbc5935d6c4f778c38627788d1d/tw/symbols.go#L885
78-
// tbl.SetRowSeparator("") - rendition - https://github.com/olekukonko/tablewriter/blob/e520aed3bd36fcbc5935d6c4f778c38627788d1d/tw/symbols.go#L885
76+
}))
7977

8078
// tbl := tablewriter.NewWriter(w)
8179
tbl.Header([]string{"NAME", "REPLICAS", "AVAILABLE", "READY", "UP TO DATE", "STATUS", "REASON", "SINCE", "MESSAGE"})
@@ -89,62 +87,56 @@ func PrintObjectTree(tree *tree.ObjectTree, w io.Writer) {
8987
// PrintObjectTreeV1Beta1 prints the cluster status to stdout.
9088
// Note: this function is exposed only for usage in clusterctl and Cluster API E2E tests.
9189
func PrintObjectTreeV1Beta1(tree *tree.ObjectTree) {
90+
cfg := getObjectTreeConfigV1Beta1()
91+
9292
// Creates the output table
93-
// tbl := tablewriter.NewWriter(os.Stdout)
94-
cfg := getTableTreeConfig()
95-
tbl := tablewriter.NewTable(os.Stdin, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{ // Apply custom rendition
93+
tbl := tablewriter.NewTable(os.Stdin, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{
9694
Settings: tw.Settings{
97-
Lines: tw.Lines{ShowHeaderLine: tw.Off},
95+
Separators: tw.SeparatorsNone, Lines: tw.LinesNone,
9896
},
99-
Borders: tw.Border{Top: tw.Off, Bottom: tw.Off, Left: tw.Off, Right: tw.Off},
100-
}),)
97+
Borders: tw.BorderNone,
98+
}))
10199
tbl.Header([]string{"NAME", "READY", "SEVERITY", "REASON", "SINCE", "MESSAGE"})
102100

103-
// tbl.SetRowSeparator("") // different - rendition - https://github.com/olekukonko/tablewriter/blob/e520aed3bd36fcbc5935d6c4f778c38627788d1d/tw/symbols.go#L885
104-
105-
// tbl.SetBorder(false) // different - rendition
106-
107-
// formatTableTreeV1Beta1(tbl)
108101
// Add row for the root object, the cluster, and recursively for all the nodes representing the cluster status.
109102
addObjectRowV1Beta1("", tbl, tree, tree.GetRoot())
110103

111104
// Prints the output table
112105
tbl.Render()
113106
}
114107

115-
// formats the table with required attributes.
116-
func getTableTreeConfig() (tablewriter.Config) {
108+
// Creates custom configuration for the table for the object tree.
109+
func getObjectTreeConfig() tablewriter.Config {
117110
cfg := tablewriter.Config{
118111
Row: tw.CellConfig{
119112
Formatting: tw.CellFormatting{AutoWrap: tw.WrapNone},
120-
Alignment: tw.CellAlignment{Global: tw.AlignLeft},
121-
Padding: tw.CellPadding{Global: tw.Padding{Left: " ", Right: " "}},
113+
Alignment: tw.CellAlignment{Global: tw.AlignLeft},
114+
Padding: tw.CellPadding{Global: tw.Padding{Left: "", Right: " "}},
122115
},
123116
Header: tw.CellConfig{
124117
Alignment: tw.CellAlignment{Global: tw.AlignLeft},
125118
},
126-
Behavior: tw.Behavior{TrimSpace: tw.On},
119+
Behavior: tw.Behavior{TrimSpace: tw.Off},
127120
}
128121

129122
return cfg
130123
}
131124

132-
// // formats the table with required attributes.
133-
// func formatTableTreeV1Beta1(tbl *tablewriter.Table) {
134-
// // tbl.SetAutoWrapText(false)
135-
// // tbl.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
136-
// // tbl.SetAlignment(tablewriter.ALIGN_LEFT)
137-
138-
// // tbl.SetCenterSeparator("")
139-
// // tbl.SetColumnSeparator("")
140-
// // tbl.SetRowSeparator("") // different - rendition
141-
142-
// // tbl.SetHeaderLine(false)
143-
// // tbl.SetBorder(false) // different - rendition
144-
// // tbl.SetTablePadding(" ")
145-
// // tbl.SetNoWhiteSpace(true)
146-
// }
125+
func getObjectTreeConfigV1Beta1() tablewriter.Config {
126+
cfg := tablewriter.Config{
127+
Row: tw.CellConfig{
128+
Formatting: tw.CellFormatting{AutoWrap: tw.WrapNone},
129+
Alignment: tw.CellAlignment{Global: tw.AlignLeft},
130+
Padding: tw.CellPadding{Global: tw.Padding{Left: "", Right: " "}},
131+
},
132+
Header: tw.CellConfig{
133+
Alignment: tw.CellAlignment{Global: tw.AlignLeft},
134+
},
135+
Behavior: tw.Behavior{TrimSpace: tw.Off},
136+
}
147137

138+
return cfg
139+
}
148140
// addObjectRow add a row for a given object, and recursively for all the object's children.
149141
// NOTE: each row name gets a prefix, that generates a tree view like representation.
150142
func addObjectRow(prefix string, tbl *tablewriter.Table, objectTree *tree.ObjectTree, obj ctrlclient.Object) {

internal/util/tree/tree_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/fatih/color"
2626
"github.com/olekukonko/tablewriter"
27+
"github.com/olekukonko/tablewriter/tw"
2728
. "github.com/onsi/gomega"
2829
gtype "github.com/onsi/gomega/types"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -280,10 +281,15 @@ func Test_V1Beta1TreePrefix(t *testing.T) {
280281
g := NewWithT(t)
281282
var output bytes.Buffer
282283

283-
// Creates the output table
284-
tbl := tablewriter.NewWriter(&output)
284+
cfg := getObjectTreeConfigV1Beta1()
285285

286-
formatTableTreeV1Beta1(tbl)
286+
// Creates the output table
287+
tbl := tablewriter.NewTable(&output, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{
288+
Settings: tw.Settings{
289+
Separators: tw.SeparatorsNone, Lines: tw.LinesNone,
290+
},
291+
Borders: tw.BorderNone,
292+
}))
287293

288294
// Add row for the root object, the cluster, and recursively for all the nodes representing the cluster status.
289295
addObjectRowV1Beta1("", tbl, tt.objectTree, tt.objectTree.GetRoot())
@@ -511,10 +517,16 @@ func Test_TreePrefix(t *testing.T) {
511517
g := NewWithT(t)
512518
var output bytes.Buffer
513519

520+
cfg := getObjectTreeConfig()
514521
// Creates the output table
515-
tbl := tablewriter.NewWriter(&output)
516-
517-
formatTableTree(tbl)
522+
tbl := tablewriter.NewTable(&output, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{
523+
Settings: tw.Settings{
524+
Separators: tw.SeparatorsNone, Lines: tw.LinesNone,
525+
},
526+
Borders: tw.BorderNone,
527+
}))
528+
529+
// tbl := tablewriter.NewWriter(w)
518530

519531
// Add row for the root object, the cluster, and recursively for all the nodes representing the cluster status.
520532
addObjectRow("", tbl, tt.objectTree, tt.objectTree.GetRoot())

0 commit comments

Comments
 (0)