@@ -22,6 +22,11 @@ type cmdClusterGroup struct {
22
22
cluster * cmdCluster
23
23
}
24
24
25
+ type clusterGroupColumn struct {
26
+ Name string
27
+ Data func (api.ClusterGroup ) string
28
+ }
29
+
25
30
// Cluster management including assignment, creation, deletion, editing, listing, removal, renaming, and showing details.
26
31
func (c * cmdClusterGroup ) Command () * cobra.Command {
27
32
cmd := & cobra.Command {}
@@ -439,7 +444,8 @@ type cmdClusterGroupList struct {
439
444
global * cmdGlobal
440
445
cluster * cmdCluster
441
446
442
- flagFormat string
447
+ flagFormat string
448
+ flagColumns string
443
449
}
444
450
445
451
// Command returns a cobra command to list all the cluster groups in a specified format.
@@ -449,7 +455,26 @@ func (c *cmdClusterGroupList) Command() *cobra.Command {
449
455
cmd .Aliases = []string {"ls" }
450
456
cmd .Short = i18n .G ("List all the cluster groups" )
451
457
cmd .Long = cli .FormatSection (i18n .G ("Description" ), i18n .G (
452
- `List all the cluster groups` ))
458
+ `List all the cluster groups
459
+
460
+ Default column layout: ndm
461
+
462
+ == Columns ==
463
+ The -c option takes a comma separated list of arguments that control
464
+ which instance attributes to output when displaying in table or csv
465
+ format.
466
+
467
+ Column arguments are either pre-defined shorthand chars (see below),
468
+ or (extended) config keys.
469
+
470
+ Commas between consecutive shorthand chars are optional.
471
+
472
+ Pre-defined column shorthand chars:
473
+ n - Name
474
+ d - Description
475
+ m - Member` ))
476
+
477
+ cmd .Flags ().StringVarP (& c .flagColumns , "columns" , "c" , defaultClusterGroupColumns , i18n .G ("Columns" )+ "``" )
453
478
cmd .Flags ().StringVarP (& c .flagFormat , "format" , "f" , "table" , i18n .G ("Format (csv|json|table|yaml|compact)" )+ "``" )
454
479
455
480
cmd .RunE = c .Run
@@ -465,6 +490,48 @@ func (c *cmdClusterGroupList) Command() *cobra.Command {
465
490
return cmd
466
491
}
467
492
493
+ const defaultClusterGroupColumns = "ndm"
494
+
495
+ func (c * cmdClusterGroupList ) parseColumns () ([]clusterGroupColumn , error ) {
496
+ columnsShorthandMap := map [rune ]clusterGroupColumn {
497
+ 'n' : {i18n .G ("NAME" ), c .clusterGroupNameColumnData },
498
+ 'm' : {i18n .G ("MEMBERS" ), c .membersColumnData },
499
+ 'd' : {i18n .G ("DESCRIPTION" ), c .descriptionColumnData },
500
+ }
501
+
502
+ columnList := strings .Split (c .flagColumns , "," )
503
+ columns := []clusterGroupColumn {}
504
+
505
+ for _ , columnEntry := range columnList {
506
+ if columnEntry == "" {
507
+ return nil , fmt .Errorf (i18n .G ("Empty column entry (redundant, leading or trailing command) in '%s'" ), c .flagColumns )
508
+ }
509
+
510
+ for _ , columnRune := range columnEntry {
511
+ column , ok := columnsShorthandMap [columnRune ]
512
+ if ! ok {
513
+ return nil , fmt .Errorf (i18n .G ("Unknown column shorthand char '%c' in '%s'" ), columnRune , columnEntry )
514
+ }
515
+
516
+ columns = append (columns , column )
517
+ }
518
+ }
519
+
520
+ return columns , nil
521
+ }
522
+
523
+ func (c * cmdClusterGroupList ) clusterGroupNameColumnData (group api.ClusterGroup ) string {
524
+ return group .Name
525
+ }
526
+
527
+ func (c * cmdClusterGroupList ) descriptionColumnData (group api.ClusterGroup ) string {
528
+ return group .Description
529
+ }
530
+
531
+ func (c * cmdClusterGroupList ) membersColumnData (group api.ClusterGroup ) string {
532
+ return fmt .Sprintf ("%d" , len (group .Members ))
533
+ }
534
+
468
535
// Run executes the command to list all the cluster groups, their descriptions, and number of members.
469
536
func (c * cmdClusterGroupList ) Run (cmd * cobra.Command , args []string ) error {
470
537
// Quick checks.
@@ -501,19 +568,28 @@ func (c *cmdClusterGroupList) Run(cmd *cobra.Command, args []string) error {
501
568
return err
502
569
}
503
570
571
+ // Parse column flags.
572
+ columns , err := c .parseColumns ()
573
+ if err != nil {
574
+ return err
575
+ }
576
+
504
577
// Render the table
505
578
data := [][]string {}
506
579
for _ , group := range groups {
507
- line := []string {group .Name , group .Description , fmt .Sprintf ("%d" , len (group .Members ))}
580
+ line := []string {}
581
+ for _ , column := range columns {
582
+ line = append (line , column .Data (group ))
583
+ }
584
+
508
585
data = append (data , line )
509
586
}
510
587
511
588
sort .Sort (cli .SortColumnsNaturally (data ))
512
589
513
- header := []string {
514
- i18n .G ("NAME" ),
515
- i18n .G ("DESCRIPTION" ),
516
- i18n .G ("MEMBERS" ),
590
+ header := []string {}
591
+ for _ , column := range columns {
592
+ header = append (header , column .Name )
517
593
}
518
594
519
595
return cli .RenderTable (c .flagFormat , header , data , groups )
0 commit comments