@@ -115,39 +115,45 @@ func CreateObjectTreeV1Beta1(w io.Writer) *tablewriter.Table {
115
115
116
116
// PrintObjectTree prints the cluster status to stdout.
117
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 ) {
118
+ func PrintObjectTree (tree * tree.ObjectTree , w io.Writer ) error {
119
119
tbl := CreateObjectTree (w )
120
120
121
121
tbl .Header ([]string {"NAME" , "REPLICAS" , "AVAILABLE" , "READY" , "UP TO DATE" , "STATUS" , "REASON" , "SINCE" , "MESSAGE" })
122
122
123
- addObjectRow ("" , tbl , tree , tree .GetRoot ())
123
+ if err := addObjectRow ("" , tbl , tree , tree .GetRoot ()); err != nil {
124
+ return fmt .Errorf ("failed to add object rows: %w" , err )
125
+ }
124
126
125
127
// Prints the output table
126
128
if err := tbl .Render (); err != nil {
127
- fmt .Printf ("Error rendering table: %v" , err )
128
- os .Exit (1 )
129
+ return fmt .Errorf ("failed to render table: %w" , err )
129
130
}
131
+
132
+ return nil
130
133
}
131
134
132
135
// PrintObjectTreeV1Beta1 prints the cluster status to stdout.
133
136
// Note: this function is exposed only for usage in clusterctl and Cluster API E2E tests.
134
- func PrintObjectTreeV1Beta1 (tree * tree.ObjectTree ) {
137
+ func PrintObjectTreeV1Beta1 (tree * tree.ObjectTree ) error {
135
138
tbl := CreateObjectTreeV1Beta1 (os .Stdin )
136
139
tbl .Header ([]string {"NAME" , "READY" , "SEVERITY" , "REASON" , "SINCE" , "MESSAGE" })
137
140
138
141
// Add row for the root object, the cluster, and recursively for all the nodes representing the cluster status.
139
- addObjectRowV1Beta1 ("" , tbl , tree , tree .GetRoot ())
142
+ if err := addObjectRowV1Beta1 ("" , tbl , tree , tree .GetRoot ()); err != nil {
143
+ return fmt .Errorf ("failed to add object rows: %w" , err )
144
+ }
140
145
141
146
// Prints the output table
142
147
if err := tbl .Render (); err != nil {
143
- fmt .Printf ("Error rendering table: %v" , err )
144
- os .Exit (1 )
148
+ return fmt .Errorf ("failed to render table: %w" , err )
145
149
}
150
+
151
+ return nil
146
152
}
147
153
148
154
// addObjectRow add a row for a given object, and recursively for all the object's children.
149
155
// NOTE: each row name gets a prefix, that generates a tree view like representation.
150
- func addObjectRow (prefix string , tbl * tablewriter.Table , objectTree * tree.ObjectTree , obj ctrlclient.Object ) {
156
+ func addObjectRow (prefix string , tbl * tablewriter.Table , objectTree * tree.ObjectTree , obj ctrlclient.Object ) error {
151
157
// Get a row descriptor for a given object.
152
158
// With v1beta2, the return value of this func adapt to the object represented in the line.
153
159
rowDescriptor := newRowDescriptor (obj )
@@ -195,8 +201,7 @@ func addObjectRow(prefix string, tbl *tablewriter.Table, objectTree *tree.Object
195
201
rowDescriptor .reason ,
196
202
rowDescriptor .age ,
197
203
msg0 }); err != nil {
198
- fmt .Printf ("Error appending row: %v" , err )
199
- os .Exit (1 )
204
+ return fmt .Errorf ("failed to append main row: %w" , err )
200
205
}
201
206
202
207
multilinePrefix := getRootMultiLineObjectPrefix (obj , objectTree )
@@ -211,23 +216,28 @@ func addObjectRow(prefix string, tbl *tablewriter.Table, objectTree *tree.Object
211
216
"" ,
212
217
"" ,
213
218
m }); err != nil {
214
- fmt .Printf ("Error appending row: %v" , err )
215
- os .Exit (1 )
219
+ return fmt .Errorf ("failed to append multiline row: %w" , err )
216
220
}
217
221
}
218
222
219
223
// If it is required to show all the conditions for the object, add a row for each object's conditions.
220
224
if tree .IsShowConditionsObject (obj ) {
221
- addOtherConditions (prefix , tbl , objectTree , obj )
225
+ if err := addOtherConditions (prefix , tbl , objectTree , obj ); err != nil {
226
+ return fmt .Errorf ("failed to add other conditions: %w" , err )
227
+ }
222
228
}
223
229
224
230
// Add a row for each object's children, taking care of updating the tree view prefix.
225
231
childrenObj := objectTree .GetObjectsByParent (obj .GetUID ())
226
232
childrenObj = orderChildrenObjects (childrenObj )
227
233
228
234
for i , child := range childrenObj {
229
- addObjectRow (getChildPrefix (prefix , i , len (childrenObj )), tbl , objectTree , child )
235
+ if err := addObjectRow (getChildPrefix (prefix , i , len (childrenObj )), tbl , objectTree , child ); err != nil {
236
+ return fmt .Errorf ("failed to add child object row: %w" , err )
237
+ }
230
238
}
239
+
240
+ return nil
231
241
}
232
242
233
243
func orderChildrenObjects (childrenObj []ctrlclient.Object ) []ctrlclient.Object {
@@ -247,7 +257,7 @@ func orderChildrenObjects(childrenObj []ctrlclient.Object) []ctrlclient.Object {
247
257
248
258
// addObjectRowV1Beta1 add a row for a given object, and recursively for all the object's children.
249
259
// NOTE: each row name gets a prefix, that generates a tree view like representation.
250
- func addObjectRowV1Beta1 (prefix string , tbl * tablewriter.Table , objectTree * tree.ObjectTree , obj ctrlclient.Object ) {
260
+ func addObjectRowV1Beta1 (prefix string , tbl * tablewriter.Table , objectTree * tree.ObjectTree , obj ctrlclient.Object ) error {
251
261
// Gets the descriptor for the object's ready condition, if any.
252
262
readyDescriptor := v1beta1ConditionDescriptor {readyColor : gray }
253
263
if ready := tree .GetV1Beta1ReadyCondition (obj ); ready != nil {
@@ -278,13 +288,14 @@ func addObjectRowV1Beta1(prefix string, tbl *tablewriter.Table, objectTree *tree
278
288
readyDescriptor .readyColor .Sprint (readyDescriptor .reason ),
279
289
readyDescriptor .age ,
280
290
readyDescriptor .message }); err != nil {
281
- fmt .Printf ("Error appending row: %v" , err )
282
- os .Exit (1 )
291
+ return fmt .Errorf ("failed to append main row: %w" , err )
283
292
}
284
293
285
294
// If it is required to show all the conditions for the object, add a row for each object's conditions.
286
295
if tree .IsShowConditionsObject (obj ) {
287
- addOtherConditionsV1Beta1 (prefix , tbl , objectTree , obj )
296
+ if err := addOtherConditionsV1Beta1 (prefix , tbl , objectTree , obj ); err != nil {
297
+ return fmt .Errorf ("failed to add other conditions: %w" , err )
298
+ }
288
299
}
289
300
290
301
// Add a row for each object's children, taking care of updating the tree view prefix.
@@ -303,12 +314,16 @@ func addObjectRowV1Beta1(prefix string, tbl *tablewriter.Table, objectTree *tree
303
314
sort .Slice (childrenObj , printBefore )
304
315
305
316
for i , child := range childrenObj {
306
- addObjectRowV1Beta1 (getChildPrefix (prefix , i , len (childrenObj )), tbl , objectTree , child )
317
+ if err := addObjectRowV1Beta1 (getChildPrefix (prefix , i , len (childrenObj )), tbl , objectTree , child ); err != nil {
318
+ return fmt .Errorf ("failed to add child object row: %w" , err )
319
+ }
307
320
}
321
+
322
+ return nil
308
323
}
309
324
310
325
// addOtherConditions adds a row for each object condition.
311
- func addOtherConditions (prefix string , tbl * tablewriter.Table , objectTree * tree.ObjectTree , obj ctrlclient.Object ) {
326
+ func addOtherConditions (prefix string , tbl * tablewriter.Table , objectTree * tree.ObjectTree , obj ctrlclient.Object ) error {
312
327
// Add a row for each other condition, taking care of updating the tree view prefix.
313
328
// In this case the tree prefix get a filler, to indent conditions from objects, and eventually a
314
329
// and additional pipe if the object has children that should be presented after the conditions.
@@ -355,8 +370,7 @@ func addOtherConditions(prefix string, tbl *tablewriter.Table, objectTree *tree.
355
370
reason ,
356
371
age ,
357
372
msg0 }); err != nil {
358
- fmt .Printf ("Error appending row: %v" , err )
359
- os .Exit (1 )
373
+ return fmt .Errorf ("failed to append condition row: %w" , err )
360
374
}
361
375
362
376
for _ , m := range msg [1 :] {
@@ -370,16 +384,17 @@ func addOtherConditions(prefix string, tbl *tablewriter.Table, objectTree *tree.
370
384
"" ,
371
385
"" ,
372
386
m }); err != nil {
373
- fmt .Printf ("Error appending row: %v" , err )
374
- os .Exit (1 )
387
+ return fmt .Errorf ("failed to append multiline condition row: %w" , err )
375
388
}
376
389
}
377
390
}
391
+
392
+ return nil
378
393
}
379
394
380
395
// addOtherConditionsV1Beta1 adds a row for each object condition except the ready condition,
381
396
// which is already represented on the object's main row.
382
- func addOtherConditionsV1Beta1 (prefix string , tbl * tablewriter.Table , objectTree * tree.ObjectTree , obj ctrlclient.Object ) {
397
+ func addOtherConditionsV1Beta1 (prefix string , tbl * tablewriter.Table , objectTree * tree.ObjectTree , obj ctrlclient.Object ) error {
383
398
// Add a row for each other condition, taking care of updating the tree view prefix.
384
399
// In this case the tree prefix get a filler, to indent conditions from objects, and eventually a
385
400
// and additional pipe if the object has children that should be presented after the conditions.
@@ -401,10 +416,11 @@ func addOtherConditionsV1Beta1(prefix string, tbl *tablewriter.Table, objectTree
401
416
otherDescriptor .readyColor .Sprint (otherDescriptor .reason ),
402
417
otherDescriptor .age ,
403
418
otherDescriptor .message }); err != nil {
404
- fmt .Printf ("Error appending row: %v" , err )
405
- os .Exit (1 )
419
+ return fmt .Errorf ("failed to append other condition row: %w" , err )
406
420
}
407
421
}
422
+
423
+ return nil
408
424
}
409
425
410
426
// getChildPrefix return the tree view prefix for a row representing a child object.
0 commit comments