Skip to content

Commit e17b2d1

Browse files
authored
Merge pull request #1130 from HassanAlsamahi/network-integration-add-column-flag
Network integration add column flag
2 parents 016a635 + 986046e commit e17b2d1

File tree

12 files changed

+867
-489
lines changed

12 files changed

+867
-489
lines changed

cmd/incus/network_integration.go

+90-9
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ type cmdNetworkIntegrationGet struct {
339339
flagIsProperty bool
340340
}
341341

342+
type networkIntegrationColumn struct {
343+
Name string
344+
Data func(api.NetworkIntegration) string
345+
}
346+
342347
// Command returns a cobra command for inclusion.
343348
func (c *cmdNetworkIntegrationGet) Command() *cobra.Command {
344349
cmd := &cobra.Command{}
@@ -398,7 +403,8 @@ type cmdNetworkIntegrationList struct {
398403
global *cmdGlobal
399404
networkIntegration *cmdNetworkIntegration
400405

401-
flagFormat string
406+
flagFormat string
407+
flagColumns string
402408
}
403409

404410
// Command returns a cobra command for inclusion.
@@ -408,14 +414,81 @@ func (c *cmdNetworkIntegrationList) Command() *cobra.Command {
408414
cmd.Aliases = []string{"ls"}
409415
cmd.Short = i18n.G("List network integrations")
410416
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+
412437
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")+"``")
413439

414440
cmd.RunE = c.Run
415441

416442
return cmd
417443
}
418444

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+
419492
// Run actually performs the action.
420493
func (c *cmdNetworkIntegrationList) Run(cmd *cobra.Command, args []string) error {
421494
conf := c.global.conf
@@ -445,19 +518,27 @@ func (c *cmdNetworkIntegrationList) Run(cmd *cobra.Command, args []string) error
445518
return err
446519
}
447520

521+
// Parse column flags.
522+
columns, err := c.parseColumns()
523+
if err != nil {
524+
return err
525+
}
526+
448527
data := [][]string{}
449528
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)
452535
}
453536

454537
sort.Sort(cli.SortColumnsNaturally(data))
455538

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)
461542
}
462543

463544
return cli.RenderTable(c.flagFormat, header, data, networkIntegrations)

0 commit comments

Comments
 (0)