diff --git a/lxc/completion.go b/lxc/completion.go index d115eba5c9bb..d00ba3f96802 100644 --- a/lxc/completion.go +++ b/lxc/completion.go @@ -211,6 +211,18 @@ func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirec } func (g *cmdGlobal) cmpInstanceAllKeys(instanceName string) ([]string, cobra.ShellCompDirective) { + var keys []string + cmpDirectives := cobra.ShellCompDirectiveNoFileComp + + _, instanceNameOnly, _ := strings.Cut(instanceName, ":") + if instanceNameOnly == "" { + serverKeys, directives := g.cmpServerAllKeys(instanceName) + keys = append(keys, serverKeys...) + cmpDirectives = directives + + return keys, cmpDirectives + } + resources, err := g.ParseServers(instanceName) if err != nil || len(resources) == 0 { return nil, cobra.ShellCompDirectiveError @@ -219,13 +231,12 @@ func (g *cmdGlobal) cmpInstanceAllKeys(instanceName string) ([]string, cobra.She resource := resources[0] client := resource.server - instanceNameOnly, _, err := client.GetInstance(instanceName) + instance, _, err := client.GetInstance(instanceNameOnly) if err != nil { return nil, cobra.ShellCompDirectiveError } - var keys []string - instanceType := instanceNameOnly.Type + instanceType := instance.Type if instanceType == "container" { for k := range instancetype.InstanceConfigKeysContainer { @@ -241,6 +252,41 @@ func (g *cmdGlobal) cmpInstanceAllKeys(instanceName string) ([]string, cobra.She keys = append(keys, k) } + return keys, cmpDirectives +} + +func (g *cmdGlobal) cmpServerAllKeys(instanceName string) ([]string, cobra.ShellCompDirective) { + var keys []string + + resources, err := g.ParseServers(instanceName) + if err != nil || len(resources) == 0 { + return nil, cobra.ShellCompDirectiveError + } + + resource := resources[0] + client := resource.server + + config, err := client.GetMetadataConfiguration() + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + + metadata := (*config)["metadata"].(map[string]any) + configs := metadata["configs"].(map[string]any) + server := configs["server"].(map[string]any) + for _, field := range server { + fieldMap := field.(map[string]any) + keyArray := fieldMap["keys"].([]any) + + for _, keyObj := range keyArray { + keyMap := keyObj.(map[string]any) + + for k := range keyMap { + keys = append(keys, k) + } + } + } + return keys, cobra.ShellCompDirectiveNoFileComp } @@ -937,6 +983,10 @@ func (g *cmdGlobal) cmpRemotes(includeAll bool) ([]string, cobra.ShellCompDirect continue } + if remoteName == "local" || rc.Protocol == "simplestreams" { + continue + } + results = append(results, fmt.Sprintf("%s:", remoteName)) } diff --git a/lxc/config.go b/lxc/config.go index 837c1a3616e8..60bb7023f26e 100644 --- a/lxc/config.go +++ b/lxc/config.go @@ -400,7 +400,11 @@ func (c *cmdConfigGet) command() *cobra.Command { cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return c.global.cmpInstances(toComplete) + if strings.Contains(toComplete, ".") { + return c.global.cmpServerAllKeys(toComplete) + } else { + return c.global.cmpInstances(toComplete) + } } if len(args) == 1 { @@ -553,7 +557,11 @@ lxc config set core.https_address=[::]:8443 cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return c.global.cmpInstances(toComplete) + if strings.Contains(toComplete, ".") { + return c.global.cmpServerAllKeys(toComplete) + } else { + return c.global.cmpInstances(toComplete) + } } if len(args) == 1 { @@ -902,7 +910,11 @@ func (c *cmdConfigUnset) command() *cobra.Command { cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return c.global.cmpInstances(toComplete) + if strings.Contains(toComplete, ".") { + return c.global.cmpServerAllKeys(toComplete) + } else { + return c.global.cmpInstances(toComplete) + } } if len(args) == 1 {