Skip to content

Commit

Permalink
Completions: Fix completions for config keys (#14055)
Browse files Browse the repository at this point in the history
`cmpInstanceAllKeys()` was only grabbing config keys that apply to any
instance, but we also want to grab/complete keys that are specific to
containers or VM's.
  • Loading branch information
tomponline authored Sep 6, 2024
2 parents be65fe0 + 487d88a commit 21d7562
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
29 changes: 27 additions & 2 deletions lxc/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,33 @@ func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirec
return results, cmpDirectives
}

func (g *cmdGlobal) cmpInstanceAllKeys() ([]string, cobra.ShellCompDirective) {
keys := []string{}
func (g *cmdGlobal) cmpInstanceAllKeys(instanceName string) ([]string, cobra.ShellCompDirective) {
resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]
client := resource.server

instanceNameOnly, _, err := client.GetInstance(instanceName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var keys []string
instanceType := instanceNameOnly.Type

if instanceType == "container" {
for k := range instancetype.InstanceConfigKeysContainer {
keys = append(keys, k)
}
} else if instanceType == "virtual-machine" {
for k := range instancetype.InstanceConfigKeysVM {
keys = append(keys, k)
}
}

for k := range instancetype.InstanceConfigKeysAny {
keys = append(keys, k)
}
Expand Down
6 changes: 3 additions & 3 deletions lxc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (c *cmdConfigGet) command() *cobra.Command {
}

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -557,7 +557,7 @@ lxc config set core.https_address=[::]:8443
}

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -906,7 +906,7 @@ func (c *cmdConfigUnset) command() *cobra.Command {
}

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
2 changes: 1 addition & 1 deletion lxc/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ For backward compatibility, a single configuration key may still be set with:
}

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down

0 comments on commit 21d7562

Please sign in to comment.