Skip to content

Commit eb5c4d1

Browse files
committed
adding create table
1 parent 4b573fc commit eb5c4d1

File tree

2 files changed

+53
-69
lines changed

2 files changed

+53
-69
lines changed

internal/util/tree/tree.go

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,50 +62,9 @@ var (
6262
cyan = color.New(color.FgCyan)
6363
)
6464

65-
// PrintObjectTree prints the cluster status to stdout.
66-
// Note: this function is exposed only for usage in clusterctl and Cluster API E2E tests.
67-
func PrintObjectTree(tree *tree.ObjectTree, w io.Writer) {
68-
cfg := getObjectTreeConfig()
69-
// Creates the output table
70-
tbl := tablewriter.NewTable(os.Stdin, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{
71-
Settings: tw.Settings{
72-
Separators: tw.SeparatorsNone, Lines: tw.LinesNone,
73-
},
74-
Borders: tw.BorderNone,
75-
}))
76-
77-
// tbl := tablewriter.NewWriter(w)
78-
tbl.Header([]string{"NAME", "REPLICAS", "AVAILABLE", "READY", "UP TO DATE", "STATUS", "REASON", "SINCE", "MESSAGE"})
79-
80-
addObjectRow("", tbl, tree, tree.GetRoot())
81-
82-
// Prints the output table
83-
tbl.Render()
84-
}
85-
86-
// PrintObjectTreeV1Beta1 prints the cluster status to stdout.
87-
// Note: this function is exposed only for usage in clusterctl and Cluster API E2E tests.
88-
func PrintObjectTreeV1Beta1(tree *tree.ObjectTree) {
89-
cfg := getObjectTreeConfigV1Beta1()
90-
91-
// Creates the output table
92-
tbl := tablewriter.NewTable(os.Stdin, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{
93-
Settings: tw.Settings{
94-
Separators: tw.SeparatorsNone, Lines: tw.LinesNone,
95-
},
96-
Borders: tw.BorderNone,
97-
}))
98-
tbl.Header([]string{"NAME", "READY", "SEVERITY", "REASON", "SINCE", "MESSAGE"})
99-
100-
// Add row for the root object, the cluster, and recursively for all the nodes representing the cluster status.
101-
addObjectRowV1Beta1("", tbl, tree, tree.GetRoot())
102-
103-
// Prints the output table
104-
tbl.Render()
105-
}
106-
107-
// Creates custom configuration for the table for the object tree.
108-
func getObjectTreeConfig() tablewriter.Config {
65+
// CreateObjectTree creates a new tablewriter.Table for the object tree.
66+
// Returns a new tablewriter.Table for the object tree.
67+
func CreateObjectTree(w io.Writer) *tablewriter.Table {
10968
cfg := tablewriter.Config{
11069
Row: tw.CellConfig{
11170
Formatting: tw.CellFormatting{AutoWrap: tw.WrapNone},
@@ -118,10 +77,20 @@ func getObjectTreeConfig() tablewriter.Config {
11877
Behavior: tw.Behavior{TrimSpace: tw.Off},
11978
}
12079

121-
return cfg
80+
// Creates the output table
81+
tbl := tablewriter.NewTable(w, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{
82+
Settings: tw.Settings{
83+
Separators: tw.SeparatorsNone, Lines: tw.LinesNone,
84+
},
85+
Borders: tw.BorderNone,
86+
}))
87+
88+
return tbl
12289
}
12390

124-
func getObjectTreeConfigV1Beta1() tablewriter.Config {
91+
// CreateObjectTreeV1Beta1 creates a new tablewriter.Table for the object tree.
92+
// Returns a new tablewriter.Table for the object tree.
93+
func CreateObjectTreeV1Beta1(w io.Writer) *tablewriter.Table {
12594
cfg := tablewriter.Config{
12695
Row: tw.CellConfig{
12796
Formatting: tw.CellFormatting{AutoWrap: tw.WrapNone},
@@ -133,8 +102,41 @@ func getObjectTreeConfigV1Beta1() tablewriter.Config {
133102
},
134103
Behavior: tw.Behavior{TrimSpace: tw.Off},
135104
}
105+
// Creates the output table
106+
tbl := tablewriter.NewTable(w, tablewriter.WithConfig(cfg), tablewriter.WithRendition(tw.Rendition{
107+
Settings: tw.Settings{
108+
Separators: tw.SeparatorsNone, Lines: tw.LinesNone,
109+
},
110+
Borders: tw.BorderNone,
111+
}))
112+
113+
return tbl
114+
}
115+
116+
// PrintObjectTree prints the cluster status to stdout.
117+
// Note: this function is exposed only for usage in clusterctl and Cluster API E2E tests.
118+
func PrintObjectTree(tree *tree.ObjectTree, w io.Writer) {
119+
tbl := CreateObjectTree(w)
136120

137-
return cfg
121+
tbl.Header([]string{"NAME", "REPLICAS", "AVAILABLE", "READY", "UP TO DATE", "STATUS", "REASON", "SINCE", "MESSAGE"})
122+
123+
addObjectRow("", tbl, tree, tree.GetRoot())
124+
125+
// Prints the output table
126+
tbl.Render()
127+
}
128+
129+
// PrintObjectTreeV1Beta1 prints the cluster status to stdout.
130+
// Note: this function is exposed only for usage in clusterctl and Cluster API E2E tests.
131+
func PrintObjectTreeV1Beta1(tree *tree.ObjectTree) {
132+
tbl := CreateObjectTreeV1Beta1(os.Stdin)
133+
tbl.Header([]string{"NAME", "READY", "SEVERITY", "REASON", "SINCE", "MESSAGE"})
134+
135+
// Add row for the root object, the cluster, and recursively for all the nodes representing the cluster status.
136+
addObjectRowV1Beta1("", tbl, tree, tree.GetRoot())
137+
138+
// Prints the output table
139+
tbl.Render()
138140
}
139141

140142
// addObjectRow add a row for a given object, and recursively for all the object's children.

internal/util/tree/tree_test.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"testing"
2424

2525
"github.com/fatih/color"
26-
"github.com/olekukonko/tablewriter"
27-
"github.com/olekukonko/tablewriter/tw"
26+
// "github.com/olekukonko/tablewriter"
27+
// "github.com/olekukonko/tablewriter/tw".
2828
. "github.com/onsi/gomega"
2929
gtype "github.com/onsi/gomega/types"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -280,16 +280,7 @@ func Test_V1Beta1TreePrefix(t *testing.T) {
280280
t.Run(tt.name, func(t *testing.T) {
281281
g := NewWithT(t)
282282
var output bytes.Buffer
283-
284-
cfg := getObjectTreeConfigV1Beta1()
285-
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-
}))
283+
tbl := CreateObjectTreeV1Beta1(&output)
293284

294285
// Add row for the root object, the cluster, and recursively for all the nodes representing the cluster status.
295286
addObjectRowV1Beta1("", tbl, tt.objectTree, tt.objectTree.GetRoot())
@@ -517,16 +508,7 @@ func Test_TreePrefix(t *testing.T) {
517508
g := NewWithT(t)
518509
var output bytes.Buffer
519510

520-
cfg := getObjectTreeConfig()
521-
// Creates the output table
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)
511+
tbl := CreateObjectTree(&output)
530512

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

0 commit comments

Comments
 (0)