Skip to content

Commit

Permalink
lxc/completion: Add completions for network forward port target addre…
Browse files Browse the repository at this point in the history
…sses

Closes #14697.

This commit adds `cmpNetworkForwardPortTargetAddresses` to provide
target address completions for `lxc network forward port add`.

Signed-off-by: Kadin Sayani <[email protected]>
  • Loading branch information
kadinsayani committed Jan 6, 2025
1 parent 9c77430 commit 4247d3c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lxc/completion.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"net"
"regexp"
"strings"

Expand Down Expand Up @@ -844,6 +845,48 @@ func (g *cmdGlobal) cmpNetworkForwards(networkName string) ([]string, cobra.Shel
return results, cmpDirectives
}

// cmpNetworkForwardPortTargetAddresses provides shell completion for network forward port target addresses.
// It takes a network name and listen address to determine whether to return ipv4 or ipv6 target addresses and returns a list of target addresses.
func (g *cmdGlobal) cmpNetworkForwardPortTargetAddresses(networkName string, listenAddress string) ([]string, cobra.ShellCompDirective) {
cmpDirectives := cobra.ShellCompDirectiveNoFileComp

resources, _ := g.ParseServers(networkName)
if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]
instances, err := resource.server.GetInstancesFull(api.InstanceTypeAny)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

results := []string{}
listenAddressIsIP4 := net.ParseIP(listenAddress).To4() != nil
for _, instance := range instances {
if instance.IsActive() && instance.State != nil && instance.State.Network != nil {
for _, network := range instance.State.Network {
if network.Type == "loopback" {
continue
}

results = make([]string, 0, len(network.Addresses))
for _, address := range network.Addresses {
if shared.ValueInSlice(address.Scope, []string{"link", "local"}) {
continue
}

if (listenAddressIsIP4 && address.Family == "inet") || (!listenAddressIsIP4 && address.Family == "inet6") {
results = append(results, address.Address)
}
}
}
}
}

return results, cmpDirectives
}

// cmpNetworkLoadBalancers provides shell completion for network load balancers.
// It takes a network name and returns a list of network load balancers along with a shell completion directive.
func (g *cmdGlobal) cmpNetworkLoadBalancers(networkName string) ([]string, cobra.ShellCompDirective) {
Expand Down
8 changes: 8 additions & 0 deletions lxc/network_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,10 @@ func (c *cmdNetworkForwardPort) commandAdd() *cobra.Command {
return []string{"tcp", "udp"}, cobra.ShellCompDirectiveNoFileComp
}

if len(args) == 4 {
return c.global.cmpNetworkForwardPortTargetAddresses(args[0], args[1])
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

Expand Down Expand Up @@ -986,6 +990,10 @@ func (c *cmdNetworkForwardPort) commandRemove() *cobra.Command {
return []string{"tcp", "udp"}, cobra.ShellCompDirectiveNoFileComp
}

if len(args) == 4 {
return c.global.cmpNetworkForwardPortTargetAddresses(args[0], args[1])
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

Expand Down

0 comments on commit 4247d3c

Please sign in to comment.