@@ -339,6 +339,11 @@ type cmdNetworkIntegrationGet struct {
339
339
flagIsProperty bool
340
340
}
341
341
342
+ type networkIntegrationColumn struct {
343
+ Name string
344
+ Data func (api.NetworkIntegration ) string
345
+ }
346
+
342
347
// Command returns a cobra command for inclusion.
343
348
func (c * cmdNetworkIntegrationGet ) Command () * cobra.Command {
344
349
cmd := & cobra.Command {}
@@ -398,7 +403,8 @@ type cmdNetworkIntegrationList struct {
398
403
global * cmdGlobal
399
404
networkIntegration * cmdNetworkIntegration
400
405
401
- flagFormat string
406
+ flagFormat string
407
+ flagColumns string
402
408
}
403
409
404
410
// Command returns a cobra command for inclusion.
@@ -408,14 +414,81 @@ func (c *cmdNetworkIntegrationList) Command() *cobra.Command {
408
414
cmd .Aliases = []string {"ls" }
409
415
cmd .Short = i18n .G ("List network integrations" )
410
416
cmd .Long = cli .FormatSection (i18n .G ("Description" ), i18n .G (
411
- `List network integrations` ))
417
+ `List network integrations
418
+
419
+ Default column layout: ndtu
420
+
421
+ == Columns ==
422
+ The -c option takes a comma separated list of arguments that control
423
+ which network zone attributes to output when displaying in table or csv
424
+ format.
425
+
426
+ Column arguments are either pre-defined shorthand chars (see below),
427
+ or (extended) config keys.
428
+
429
+ Commas between consecutive shorthand chars are optional.
430
+
431
+ Pre-defined column shorthand chars:
432
+ n - Name
433
+ d - Description
434
+ t - Type
435
+ u - Used by` ))
436
+
412
437
cmd .Flags ().StringVarP (& c .flagFormat , "format" , "f" , "table" , i18n .G ("Format (csv|json|table|yaml|compact)" )+ "``" )
438
+ cmd .Flags ().StringVarP (& c .flagColumns , "columns" , "c" , defaultNetworkIntegrationColumns , i18n .G ("Columns" )+ "``" )
413
439
414
440
cmd .RunE = c .Run
415
441
416
442
return cmd
417
443
}
418
444
445
+ const defaultNetworkIntegrationColumns = "ndtu"
446
+
447
+ func (c * cmdNetworkIntegrationList ) parseColumns () ([]networkIntegrationColumn , error ) {
448
+ columnsShorthandMap := map [rune ]networkIntegrationColumn {
449
+ 'n' : {i18n .G ("NAME" ), c .nameColumnData },
450
+ 'd' : {i18n .G ("DESCRIPTION" ), c .descriptionColumnData },
451
+ 't' : {i18n .G ("TYPE" ), c .typeColumnData },
452
+ 'u' : {i18n .G ("USED BY" ), c .usedByColumnData },
453
+ }
454
+
455
+ columnList := strings .Split (c .flagColumns , "," )
456
+ columns := []networkIntegrationColumn {}
457
+
458
+ for _ , columnEntry := range columnList {
459
+ if columnEntry == "" {
460
+ return nil , fmt .Errorf (i18n .G ("Empty column entry (redundant, leading or trailing command) in '%s'" ), c .flagColumns )
461
+ }
462
+
463
+ for _ , columnRune := range columnEntry {
464
+ column , ok := columnsShorthandMap [columnRune ]
465
+ if ! ok {
466
+ return nil , fmt .Errorf (i18n .G ("Unknown column shorthand char '%c' in '%s'" ), columnRune , columnEntry )
467
+ }
468
+
469
+ columns = append (columns , column )
470
+ }
471
+ }
472
+
473
+ return columns , nil
474
+ }
475
+
476
+ func (c * cmdNetworkIntegrationList ) nameColumnData (integration api.NetworkIntegration ) string {
477
+ return integration .Name
478
+ }
479
+
480
+ func (c * cmdNetworkIntegrationList ) descriptionColumnData (integration api.NetworkIntegration ) string {
481
+ return integration .Description
482
+ }
483
+
484
+ func (c * cmdNetworkIntegrationList ) typeColumnData (integration api.NetworkIntegration ) string {
485
+ return integration .Type
486
+ }
487
+
488
+ func (c * cmdNetworkIntegrationList ) usedByColumnData (integration api.NetworkIntegration ) string {
489
+ return fmt .Sprintf ("%d" , len (integration .UsedBy ))
490
+ }
491
+
419
492
// Run actually performs the action.
420
493
func (c * cmdNetworkIntegrationList ) Run (cmd * cobra.Command , args []string ) error {
421
494
conf := c .global .conf
@@ -445,19 +518,27 @@ func (c *cmdNetworkIntegrationList) Run(cmd *cobra.Command, args []string) error
445
518
return err
446
519
}
447
520
521
+ // Parse column flags.
522
+ columns , err := c .parseColumns ()
523
+ if err != nil {
524
+ return err
525
+ }
526
+
448
527
data := [][]string {}
449
528
for _ , networkIntegration := range networkIntegrations {
450
- strUsedBy := fmt .Sprintf ("%d" , len (networkIntegration .UsedBy ))
451
- data = append (data , []string {networkIntegration .Name , networkIntegration .Description , networkIntegration .Type , strUsedBy })
529
+ line := []string {}
530
+ for _ , column := range columns {
531
+ line = append (line , column .Data (networkIntegration ))
532
+ }
533
+
534
+ data = append (data , line )
452
535
}
453
536
454
537
sort .Sort (cli .SortColumnsNaturally (data ))
455
538
456
- header := []string {
457
- i18n .G ("NAME" ),
458
- i18n .G ("DESCRIPTION" ),
459
- i18n .G ("TYPE" ),
460
- i18n .G ("USED BY" ),
539
+ header := []string {}
540
+ for _ , column := range columns {
541
+ header = append (header , column .Name )
461
542
}
462
543
463
544
return cli .RenderTable (c .flagFormat , header , data , networkIntegrations )
0 commit comments