Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network integration add column flag #1130

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading