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 list allocations add column flag #1126

Merged
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
140 changes: 111 additions & 29 deletions cmd/incus/network_allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"sort"
"strings"

"github.com/spf13/cobra"

Expand All @@ -17,38 +19,38 @@ type cmdNetworkListAllocations struct {
flagFormat string
flagProject string
flagAllProjects bool
flagColumns string
}

func (c *cmdNetworkListAllocations) pretty(allocs []api.NetworkAllocations) error {
header := []string{
i18n.G("USED BY"),
i18n.G("ADDRESS"),
i18n.G("TYPE"),
i18n.G("NAT"),
i18n.G("HARDWARE ADDRESS"),
}

data := [][]string{}
for _, alloc := range allocs {
row := []string{
alloc.UsedBy,
alloc.Address,
alloc.Type,
fmt.Sprint(alloc.NAT),
alloc.Hwaddr,
}

data = append(data, row)
}

return cli.RenderTable(c.flagFormat, header, data, allocs)
type networkAllocationColumn struct {
Name string
Data func(api.NetworkAllocations) string
}

func (c *cmdNetworkListAllocations) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("list-allocations")
cmd.Short = i18n.G("List network allocations in use")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("List network allocations in use"))
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`List network allocations in use
Default column layout: uatnm

== Columns ==
The -c option takes a comma separated list of arguments that control
which instance 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:
u - Used by
a - Address
t - Type
n - NAT
m - Mac Address`))

// Workaround for subcommand usage errors. See: https://github.com/spf13/cobra/issues/706
cmd.Args = cobra.MaximumNArgs(1)
Expand All @@ -57,9 +59,68 @@ func (c *cmdNetworkListAllocations) Command() *cobra.Command {
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.Flags().StringVarP(&c.flagProject, "project", "p", api.ProjectDefaultName, i18n.G("Run again a specific project"))
cmd.Flags().BoolVar(&c.flagAllProjects, "all-projects", false, i18n.G("Run against all projects"))
cmd.Flags().StringVarP(&c.flagColumns, "columns", "c", defaultNetworkAllocationColumns, i18n.G("Columns")+"``")

return cmd
}

const defaultNetworkAllocationColumns = "uatnm"

func (c *cmdNetworkListAllocations) parseColumns() ([]networkAllocationColumn, error) {
columnsShorthandMap := map[rune]networkAllocationColumn{
'u': {i18n.G("USED BY"), c.usedByColumnData},
'a': {i18n.G("ADDRESS"), c.addressColumnData},
't': {i18n.G("TYPE"), c.typeColumnData},
'n': {i18n.G("NAT"), c.natColumnData},
'm': {i18n.G("MAC ADDRESS"), c.macAddressColumnData},
}

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

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 *cmdNetworkListAllocations) usedByColumnData(alloc api.NetworkAllocations) string {
return alloc.UsedBy
}

func (c *cmdNetworkListAllocations) addressColumnData(alloc api.NetworkAllocations) string {
return alloc.Address
}

func (c *cmdNetworkListAllocations) typeColumnData(alloc api.NetworkAllocations) string {
return alloc.Type
}

func (c *cmdNetworkListAllocations) natColumnData(alloc api.NetworkAllocations) string {
strNat := "NO"
if alloc.NAT {
strNat = "YES"
}

return strNat
}

func (c *cmdNetworkListAllocations) macAddressColumnData(alloc api.NetworkAllocations) string {
return alloc.Hwaddr
}

func (c *cmdNetworkListAllocations) Run(cmd *cobra.Command, args []string) error {
remote := ""
if len(args) > 0 {
Expand All @@ -74,19 +135,40 @@ func (c *cmdNetworkListAllocations) Run(cmd *cobra.Command, args []string) error
resource := resources[0]
server := resource.server.UseProject(c.flagProject)

var addresses []api.NetworkAllocations
if c.flagAllProjects {
addresses, err := server.GetNetworkAllocationsAllProjects()
addresses, err = server.GetNetworkAllocationsAllProjects()
if err != nil {
return err
}
} else {
addresses, err = server.GetNetworkAllocations()
if err != nil {
return err
}

return c.pretty(addresses)
}

addresses, err := server.GetNetworkAllocations()
columns, err := c.parseColumns()
if err != nil {
return err
}

return c.pretty(addresses)
data := [][]string{}
for _, address := range addresses {
line := []string{}
for _, column := range columns {
line = append(line, column.Data(address))
}

data = append(data, line)
}

sort.Sort(cli.SortColumnsNaturally(data))

header := []string{}
for _, column := range columns {
header = append(header, column.Name)
}

return cli.RenderTable(c.flagFormat, header, data, addresses)
}
80 changes: 50 additions & 30 deletions po/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: LXD\n"
"Report-Msgid-Bugs-To: lxc-devel@lists.linuxcontainers.org\n"
"POT-Creation-Date: 2024-08-14 19:53-0400\n"
"POT-Creation-Date: 2024-08-16 00:23-0400\n"
"PO-Revision-Date: 2024-06-02 16:41+0000\n"
"Last-Translator: Tobias Gerold <tobias@g3ro.eu>\n"
"Language-Team: German <https://hosted.weblate.org/projects/incus/cli/de/>\n"
Expand Down Expand Up @@ -794,7 +794,7 @@ msgstr "Ein Name für den Client muss angegeben werden"
msgid "A cluster member name must be provided"
msgstr "der Name des Ursprung Containers muss angegeben werden"

#: cmd/incus/network_allocations.go:25
#: cmd/incus/network_allocations.go:72
msgid "ADDRESS"
msgstr "ADRESSE"

Expand Down Expand Up @@ -1581,11 +1581,12 @@ msgstr ""
#: cmd/incus/cluster.go:150 cmd/incus/cluster_group.go:477
#: cmd/incus/config_trust.go:421 cmd/incus/image.go:1093
#: cmd/incus/image_alias.go:180 cmd/incus/list.go:134 cmd/incus/network.go:1072
#: cmd/incus/network.go:1269 cmd/incus/network_forward.go:115
#: cmd/incus/network_zone.go:115 cmd/incus/operation.go:138
#: cmd/incus/profile.go:725 cmd/incus/project.go:527 cmd/incus/remote.go:750
#: cmd/incus/storage.go:687 cmd/incus/storage_volume.go:1557
#: cmd/incus/storage_volume.go:2548 cmd/incus/warning.go:93
#: cmd/incus/network.go:1269 cmd/incus/network_allocations.go:62
#: cmd/incus/network_forward.go:115 cmd/incus/network_zone.go:115
#: cmd/incus/operation.go:138 cmd/incus/profile.go:725 cmd/incus/project.go:527
#: cmd/incus/remote.go:750 cmd/incus/storage.go:687
#: cmd/incus/storage_volume.go:1557 cmd/incus/storage_volume.go:2548
#: cmd/incus/warning.go:93
msgid "Columns"
msgstr "Spalten"

Expand Down Expand Up @@ -2221,7 +2222,7 @@ msgstr ""
#: cmd/incus/network_acl.go:608 cmd/incus/network_acl.go:747
#: cmd/incus/network_acl.go:804 cmd/incus/network_acl.go:861
#: cmd/incus/network_acl.go:876 cmd/incus/network_acl.go:1013
#: cmd/incus/network_allocations.go:51 cmd/incus/network_forward.go:28
#: cmd/incus/network_allocations.go:34 cmd/incus/network_forward.go:28
#: cmd/incus/network_forward.go:91 cmd/incus/network_forward.go:248
#: cmd/incus/network_forward.go:324 cmd/incus/network_forward.go:427
#: cmd/incus/network_forward.go:512 cmd/incus/network_forward.go:622
Expand Down Expand Up @@ -2679,11 +2680,11 @@ msgstr "Alternatives config Verzeichnis."
#: cmd/incus/cluster.go:187 cmd/incus/cluster_group.go:507
#: cmd/incus/config_trust.go:447 cmd/incus/image.go:1133
#: cmd/incus/image_alias.go:217 cmd/incus/list.go:633 cmd/incus/network.go:1113
#: cmd/incus/network.go:1303 cmd/incus/network_forward.go:147
#: cmd/incus/network_zone.go:147 cmd/incus/operation.go:166
#: cmd/incus/profile.go:763 cmd/incus/project.go:565 cmd/incus/remote.go:773
#: cmd/incus/storage.go:722 cmd/incus/storage_volume.go:1700
#: cmd/incus/warning.go:236
#: cmd/incus/network.go:1303 cmd/incus/network_allocations.go:83
#: cmd/incus/network_forward.go:147 cmd/incus/network_zone.go:147
#: cmd/incus/operation.go:166 cmd/incus/profile.go:763 cmd/incus/project.go:565
#: cmd/incus/remote.go:773 cmd/incus/storage.go:722
#: cmd/incus/storage_volume.go:1700 cmd/incus/warning.go:236
#, c-format
msgid "Empty column entry (redundant, leading or trailing command) in '%s'"
msgstr ""
Expand Down Expand Up @@ -3432,7 +3433,7 @@ msgstr ""
#: cmd/incus/config_trust.go:422 cmd/incus/config_trust.go:586
#: cmd/incus/image.go:1094 cmd/incus/image_alias.go:179 cmd/incus/list.go:135
#: cmd/incus/network.go:1073 cmd/incus/network.go:1268
#: cmd/incus/network_acl.go:97 cmd/incus/network_allocations.go:57
#: cmd/incus/network_acl.go:97 cmd/incus/network_allocations.go:59
#: cmd/incus/network_forward.go:114 cmd/incus/network_integration.go:412
#: cmd/incus/network_load_balancer.go:93 cmd/incus/network_peer.go:84
#: cmd/incus/network_zone.go:113 cmd/incus/network_zone.go:859
Expand Down Expand Up @@ -3693,10 +3694,6 @@ msgstr ""
msgid "Group ID to run the command as (default 0)"
msgstr ""

#: cmd/incus/network_allocations.go:28
msgid "HARDWARE ADDRESS"
msgstr ""

#: cmd/incus/network.go:1288
msgid "HOSTNAME"
msgstr ""
Expand Down Expand Up @@ -4668,10 +4665,33 @@ msgstr ""
msgid "List network ACLs across all projects"
msgstr "Herunterfahren des Containers erzwingen."

#: cmd/incus/network_allocations.go:50 cmd/incus/network_allocations.go:51
#: cmd/incus/network_allocations.go:33
msgid "List network allocations in use"
msgstr ""

#: cmd/incus/network_allocations.go:34
msgid ""
"List network allocations in use\n"
"Default column layout: uatnm\n"
"\n"
"== Columns ==\n"
"The -c option takes a comma separated list of arguments that control\n"
"which instance attributes to output when displaying in table or csv\n"
"format.\n"
"\n"
"Column arguments are either pre-defined shorthand chars (see below),\n"
"or (extended) config keys.\n"
"\n"
"Commas between consecutive shorthand chars are optional.\n"
"\n"
"Pre-defined column shorthand chars:\n"
" u - Used by\n"
" a - Address\n"
" t - Type\n"
" n - NAT\n"
" m - Mac Address"
msgstr ""

#: cmd/incus/network_integration.go:409 cmd/incus/network_integration.go:410
#, fuzzy
msgid "List network integrations"
Expand Down Expand Up @@ -4931,7 +4951,7 @@ msgstr "kann nicht zum selben Container Namen kopieren"
msgid "Lower devices"
msgstr "kann nicht zum selben Container Namen kopieren"

#: cmd/incus/network.go:1289
#: cmd/incus/network.go:1289 cmd/incus/network_allocations.go:75
msgid "MAC ADDRESS"
msgstr ""

Expand Down Expand Up @@ -5608,7 +5628,7 @@ msgstr "der Name des Ursprung Containers muss angegeben werden"
msgid "NAME"
msgstr ""

#: cmd/incus/network_allocations.go:27
#: cmd/incus/network_allocations.go:74
msgid "NAT"
msgstr ""

Expand Down Expand Up @@ -6752,15 +6772,15 @@ msgstr ""
msgid "Run a local API proxy for the remote"
msgstr ""

#: cmd/incus/network_allocations.go:58
#: cmd/incus/network_allocations.go:60
msgid "Run again a specific project"
msgstr ""

#: cmd/incus/action.go:161
msgid "Run against all instances"
msgstr ""

#: cmd/incus/network_allocations.go:59
#: cmd/incus/network_allocations.go:61
#, fuzzy
msgid "Run against all projects"
msgstr "Fehlerhafte Profil URL %s"
Expand Down Expand Up @@ -7748,7 +7768,7 @@ msgstr ""

#: cmd/incus/config_trust.go:432 cmd/incus/image.go:1123
#: cmd/incus/image_alias.go:207 cmd/incus/list.go:589 cmd/incus/network.go:1094
#: cmd/incus/network.go:1291 cmd/incus/network_allocations.go:26
#: cmd/incus/network.go:1291 cmd/incus/network_allocations.go:73
#: cmd/incus/network_integration.go:459 cmd/incus/network_peer.go:157
#: cmd/incus/operation.go:150 cmd/incus/storage_volume.go:1670
#: cmd/incus/warning.go:216
Expand Down Expand Up @@ -8215,7 +8235,7 @@ msgid "USB devices:"
msgstr "kann nicht zum selben Container Namen kopieren"

#: cmd/incus/network.go:1099 cmd/incus/network_acl.go:170
#: cmd/incus/network_allocations.go:24 cmd/incus/network_integration.go:460
#: cmd/incus/network_allocations.go:71 cmd/incus/network_integration.go:460
#: cmd/incus/network_zone.go:135 cmd/incus/profile.go:748
#: cmd/incus/project.go:556 cmd/incus/storage.go:712
#: cmd/incus/storage_volume.go:1674
Expand Down Expand Up @@ -8259,11 +8279,11 @@ msgstr "Unbekannter Befehl %s für Abbild"
#: cmd/incus/cluster.go:193 cmd/incus/cluster_group.go:513
#: cmd/incus/config_trust.go:455 cmd/incus/image.go:1141
#: cmd/incus/image_alias.go:223 cmd/incus/list.go:648 cmd/incus/network.go:1119
#: cmd/incus/network.go:1309 cmd/incus/network_forward.go:153
#: cmd/incus/network_zone.go:153 cmd/incus/operation.go:172
#: cmd/incus/profile.go:769 cmd/incus/project.go:571 cmd/incus/remote.go:779
#: cmd/incus/storage.go:728 cmd/incus/storage_volume.go:1708
#: cmd/incus/warning.go:244
#: cmd/incus/network.go:1309 cmd/incus/network_allocations.go:89
#: cmd/incus/network_forward.go:153 cmd/incus/network_zone.go:153
#: cmd/incus/operation.go:172 cmd/incus/profile.go:769 cmd/incus/project.go:571
#: cmd/incus/remote.go:779 cmd/incus/storage.go:728
#: cmd/incus/storage_volume.go:1708 cmd/incus/warning.go:244
#, c-format
msgid "Unknown column shorthand char '%c' in '%s'"
msgstr ""
Expand Down
Loading
Loading