Skip to content

Commit

Permalink
lxc: add completions for network peers
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Stephens <[email protected]>
(cherry picked from commit 459e9e315373517e0b26fd0ee7bbb91306aabe59)
Signed-off-by: Kadin Sayani <[email protected]>
License: Apache-2.0
  • Loading branch information
adamcstephens authored and kadinsayani committed Sep 4, 2024
1 parent 0e0c58a commit 49f1aa7
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lxc/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,50 @@ func (g *cmdGlobal) cmpNetworkLoadBalancers(networkName string) ([]string, cobra
return results, cmpDirectives
}

func (g *cmdGlobal) cmpNetworkPeerConfigs(networkName string, peerName string) ([]string, cobra.ShellCompDirective) {
var results []string
cmpDirectives := cobra.ShellCompDirectiveNoFileComp

resources, _ := g.ParseServers(networkName)

if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]

peer, _, err := resource.server.GetNetworkPeer(resource.name, peerName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

for k := range peer.Config {
results = append(results, k)
}

return results, cmpDirectives
}

func (g *cmdGlobal) cmpNetworkPeers(networkName string) ([]string, cobra.ShellCompDirective) {
var results []string
cmpDirectives := cobra.ShellCompDirectiveNoFileComp

resources, _ := g.ParseServers(networkName)

if len(resources) <= 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]

results, err := resource.server.GetNetworkPeerNames(networkName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

return results, cmpDirectives
}

func (g *cmdGlobal) cmpNetworks(toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
Expand Down
99 changes: 99 additions & 0 deletions lxc/network_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ func (c *cmdNetworkPeerList) command() *cobra.Command {
cmd.RunE = c.run
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworks(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -159,6 +167,18 @@ func (c *cmdNetworkPeerShow) command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Show network peer configurations"))
cmd.RunE = c.run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworks(toComplete)
}

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -216,6 +236,14 @@ func (c *cmdNetworkPeerCreate) command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Create new network peering"))
cmd.RunE = c.run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworks(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -333,6 +361,23 @@ func (c *cmdNetworkPeerGet) command() *cobra.Command {
cmd.RunE = c.run

cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Get the key as a network peer property"))

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworks(toComplete)
}

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -405,6 +450,19 @@ For backward compatibility, a single configuration key may still be set with:
cmd.RunE = c.run

cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Set the key as a network peer property"))

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworks(toComplete)
}

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -490,6 +548,23 @@ func (c *cmdNetworkPeerUnset) command() *cobra.Command {
cmd.RunE = c.run

cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Unset the key as a network peer property"))

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworks(toComplete)
}

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -519,6 +594,18 @@ func (c *cmdNetworkPeerEdit) command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Edit network peer configurations as YAML"))
cmd.RunE = c.run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworks(toComplete)
}

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -644,6 +731,18 @@ func (c *cmdNetworkPeerDelete) command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G("Delete network peerings"))
cmd.RunE = c.run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpNetworks(toComplete)
}

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down

0 comments on commit 49f1aa7

Please sign in to comment.