Skip to content

Commit

Permalink
Merge pull request #1130 from HassanAlsamahi/network-integration-add-…
Browse files Browse the repository at this point in the history
…column-flag

Network integration add column flag
  • Loading branch information
stgraber authored Aug 17, 2024
2 parents 016a635 + 986046e commit e17b2d1
Show file tree
Hide file tree
Showing 12 changed files with 867 additions and 489 deletions.
99 changes: 90 additions & 9 deletions cmd/incus/network_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ type cmdNetworkIntegrationGet struct {
flagIsProperty bool
}

type networkIntegrationColumn struct {
Name string
Data func(api.NetworkIntegration) string
}

// Command returns a cobra command for inclusion.
func (c *cmdNetworkIntegrationGet) Command() *cobra.Command {
cmd := &cobra.Command{}
Expand Down Expand Up @@ -398,7 +403,8 @@ type cmdNetworkIntegrationList struct {
global *cmdGlobal
networkIntegration *cmdNetworkIntegration

flagFormat string
flagFormat string
flagColumns string
}

// Command returns a cobra command for inclusion.
Expand All @@ -408,14 +414,81 @@ func (c *cmdNetworkIntegrationList) Command() *cobra.Command {
cmd.Aliases = []string{"ls"}
cmd.Short = i18n.G("List network integrations")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`List network integrations`))
`List network integrations
Default column layout: ndtu
== Columns ==
The -c option takes a comma separated list of arguments that control
which network zone attributes to output when displaying in table or csv
format.
Column arguments are either pre-defined shorthand chars (see below),
or (extended) config keys.
Commas between consecutive shorthand chars are optional.
Pre-defined column shorthand chars:
n - Name
d - Description
t - Type
u - Used by`))

cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.Flags().StringVarP(&c.flagColumns, "columns", "c", defaultNetworkIntegrationColumns, i18n.G("Columns")+"``")

cmd.RunE = c.Run

return cmd
}

const defaultNetworkIntegrationColumns = "ndtu"

func (c *cmdNetworkIntegrationList) parseColumns() ([]networkIntegrationColumn, error) {
columnsShorthandMap := map[rune]networkIntegrationColumn{
'n': {i18n.G("NAME"), c.nameColumnData},
'd': {i18n.G("DESCRIPTION"), c.descriptionColumnData},
't': {i18n.G("TYPE"), c.typeColumnData},
'u': {i18n.G("USED BY"), c.usedByColumnData},
}

columnList := strings.Split(c.flagColumns, ",")
columns := []networkIntegrationColumn{}

for _, columnEntry := range columnList {
if columnEntry == "" {
return nil, fmt.Errorf(i18n.G("Empty column entry (redundant, leading or trailing command) in '%s'"), c.flagColumns)
}

for _, columnRune := range columnEntry {
column, ok := columnsShorthandMap[columnRune]
if !ok {
return nil, fmt.Errorf(i18n.G("Unknown column shorthand char '%c' in '%s'"), columnRune, columnEntry)
}

columns = append(columns, column)
}
}

return columns, nil
}

func (c *cmdNetworkIntegrationList) nameColumnData(integration api.NetworkIntegration) string {
return integration.Name
}

func (c *cmdNetworkIntegrationList) descriptionColumnData(integration api.NetworkIntegration) string {
return integration.Description
}

func (c *cmdNetworkIntegrationList) typeColumnData(integration api.NetworkIntegration) string {
return integration.Type
}

func (c *cmdNetworkIntegrationList) usedByColumnData(integration api.NetworkIntegration) string {
return fmt.Sprintf("%d", len(integration.UsedBy))
}

// Run actually performs the action.
func (c *cmdNetworkIntegrationList) Run(cmd *cobra.Command, args []string) error {
conf := c.global.conf
Expand Down Expand Up @@ -445,19 +518,27 @@ func (c *cmdNetworkIntegrationList) Run(cmd *cobra.Command, args []string) error
return err
}

// Parse column flags.
columns, err := c.parseColumns()
if err != nil {
return err
}

data := [][]string{}
for _, networkIntegration := range networkIntegrations {
strUsedBy := fmt.Sprintf("%d", len(networkIntegration.UsedBy))
data = append(data, []string{networkIntegration.Name, networkIntegration.Description, networkIntegration.Type, strUsedBy})
line := []string{}
for _, column := range columns {
line = append(line, column.Data(networkIntegration))
}

data = append(data, line)
}

sort.Sort(cli.SortColumnsNaturally(data))

header := []string{
i18n.G("NAME"),
i18n.G("DESCRIPTION"),
i18n.G("TYPE"),
i18n.G("USED BY"),
header := []string{}
for _, column := range columns {
header = append(header, column.Name)
}

return cli.RenderTable(c.flagFormat, header, data, networkIntegrations)
Expand Down
Loading

0 comments on commit e17b2d1

Please sign in to comment.