Skip to content

Commit

Permalink
lxc/completion: Improve instance and remote names completion
Browse files Browse the repository at this point in the history
The completion functions for instance and remote names now only return
names prefixed by the string to be completed. This allows the caller
of these functions to improve their decisions about their own
completion behaviour.

Signed-off-by: montag451 <[email protected]>
(cherry picked from commit 42abaca45508aeab25f99da170e87101b2a327ab)
Signed-off-by: Kadin Sayani <[email protected]>
License: Apache-2.0
  • Loading branch information
montag451 authored and kadinsayani committed Jan 7, 2025
1 parent 72baca0 commit 4e3035e
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 38 deletions.
6 changes: 3 additions & 3 deletions lxc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (c *cmdClusterList) command() *cobra.Command {

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

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

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

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
4 changes: 2 additions & 2 deletions lxc/cluster_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ lxc cluster group create g1 < config.yaml

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

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
42 changes: 27 additions & 15 deletions lxc/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (g *cmdGlobal) cmpClusterGroups(toComplete string) ([]string, cobra.ShellCo
}

if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(false)
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (g *cmdGlobal) cmpClusterMembers(toComplete string) ([]string, cobra.ShellC
}

if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(false)
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirec
}

if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(true)
remotes, directives := g.cmpRemotes(toComplete, true)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand Down Expand Up @@ -586,12 +586,16 @@ func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDi
name = resource.remote + ":" + instance
}

if !strings.HasPrefix(name, toComplete) {
continue
}

results = append(results, name)
}
}

if !strings.Contains(toComplete, ":") {
remotes, _ := g.cmpRemotes(false)
remotes, _ := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
}

Expand Down Expand Up @@ -652,7 +656,7 @@ func (g *cmdGlobal) cmpInstancesAction(toComplete string, action string, flagFor
}

if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(false)
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand Down Expand Up @@ -695,7 +699,7 @@ func (g *cmdGlobal) cmpInstancesAndSnapshots(toComplete string) ([]string, cobra
}

if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(false)
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand Down Expand Up @@ -778,7 +782,7 @@ func (g *cmdGlobal) cmpNetworkACLs(toComplete string) ([]string, cobra.ShellComp
}

if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(false)
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand Down Expand Up @@ -943,7 +947,7 @@ func (g *cmdGlobal) cmpNetworks(toComplete string) ([]string, cobra.ShellCompDir
}

if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(false)
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand Down Expand Up @@ -1139,7 +1143,7 @@ func (g *cmdGlobal) cmpNetworkZones(toComplete string) ([]string, cobra.ShellCom
}

if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(false)
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand Down Expand Up @@ -1240,7 +1244,7 @@ func (g *cmdGlobal) cmpProfiles(toComplete string, includeRemotes bool) ([]strin
}

if includeRemotes && !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(false)
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand Down Expand Up @@ -1303,7 +1307,7 @@ func (g *cmdGlobal) cmpProjects(toComplete string) ([]string, cobra.ShellCompDir
}

if !strings.Contains(toComplete, ":") {
remotes, directives := g.cmpRemotes(false)
remotes, directives := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
cmpDirectives |= directives
}
Expand All @@ -1313,18 +1317,26 @@ func (g *cmdGlobal) cmpProjects(toComplete string) ([]string, cobra.ShellCompDir

// cmpRemotes provides shell completion for remotes.
// It takes a boolean specifying whether to include all remotes or not and returns a list of remotes along with a shell completion directive.
func (g *cmdGlobal) cmpRemotes(includeAll bool) ([]string, cobra.ShellCompDirective) {
results := make([]string, 0, len(g.conf.Remotes))
func (g *cmdGlobal) cmpRemotes(toComplete string, includeAll bool) ([]string, cobra.ShellCompDirective) {
results := []string{}

for remoteName, rc := range g.conf.Remotes {
if remoteName == "local" && g.conf.DefaultRemote == "local" || remoteName == g.conf.DefaultRemote || (!includeAll && rc.Protocol != "lxd" && rc.Protocol != "") {
continue
}

if !strings.HasPrefix(remoteName, toComplete) {
continue
}

results = append(results, remoteName+":")
}

return results, cobra.ShellCompDirectiveNoSpace
if len(results) > 0 {
return results, cobra.ShellCompDirectiveNoSpace
}

return results, cobra.ShellCompDirectiveNoFileComp
}

// cmpRemoteNames provides shell completion for remote names.
Expand Down Expand Up @@ -1434,7 +1446,7 @@ func (g *cmdGlobal) cmpStoragePools(toComplete string, noSpace bool) ([]string,
}

if !strings.Contains(toComplete, ":") {
remotes, _ := g.cmpRemotes(false)
remotes, _ := g.cmpRemotes(toComplete, false)
results = append(results, remotes...)
}

Expand Down
2 changes: 1 addition & 1 deletion lxc/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ The pull transfer mode is the default as it is compatible with all LXD versions.
}

if len(args) == 1 {
return c.global.cmpRemotes(false)
return c.global.cmpRemotes(toComplete, false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
7 changes: 4 additions & 3 deletions lxc/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ It requires the source to be an alias and for it to be public.`))
}

if len(args) == 1 {
return c.global.cmpRemotes(false)
return c.global.cmpRemotes(toComplete, false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -711,7 +711,7 @@ Descriptive properties can be set by providing key=value pairs. Example: os=Ubun
}

if len(args) == 1 {
return c.global.cmpRemotes(false)
return c.global.cmpRemotes(toComplete, false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -1371,7 +1371,7 @@ func (c *cmdImageList) run(cmd *cobra.Command, args []string) error {

serverFilters, clientFilters := getServerSupportedFilters(filters, api.Image{})

var allImages, images []api.Image
var allImages []api.Image
if c.flagAllProjects {
allImages, err = remoteServer.GetImagesAllProjectsWithFilter(serverFilters)
if err != nil {
Expand All @@ -1394,6 +1394,7 @@ func (c *cmdImageList) run(cmd *cobra.Command, args []string) error {
}
}

images := make([]api.Image, 0, len(allImages))
for _, image := range allImages {
if !c.imageShouldShow(clientFilters, &image) {
continue
Expand Down
2 changes: 1 addition & 1 deletion lxc/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ lxc list -c ns,user.comment:comment

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
2 changes: 1 addition & 1 deletion lxc/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ lxc move <instance>/<old snapshot name> <instance>/<new snapshot name>
}

if len(args) == 1 {
return c.global.cmpRemotes(false)
return c.global.cmpRemotes(toComplete, false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
4 changes: 2 additions & 2 deletions lxc/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ lxc network create bar network=baz --type ovn
return nil, cobra.ShellCompDirectiveNoFileComp
}

return c.global.cmpRemotes(false)
return c.global.cmpRemotes(toComplete, false)
}

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

return c.global.cmpRemotes(false)
return c.global.cmpRemotes(toComplete, false)
}

return cmd
Expand Down
2 changes: 1 addition & 1 deletion lxc/network_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *cmdNetworkACLList) command() *cobra.Command {

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
2 changes: 1 addition & 1 deletion lxc/network_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *cmdNetworkZoneList) command() *cobra.Command {

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
6 changes: 3 additions & 3 deletions lxc/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (c *cmdProfileCopy) command() *cobra.Command {
}

if len(args) == 1 {
return c.global.cmpRemotes(false)
return c.global.cmpRemotes(toComplete, false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -361,7 +361,7 @@ lxc profile create p1 < config.yaml

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

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
4 changes: 2 additions & 2 deletions lxc/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ lxc project create p1 < config.yaml

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

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
2 changes: 1 addition & 1 deletion lxc/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *cmdPublish) command() *cobra.Command {
}

if len(args) == 1 {
return c.global.cmpRemotes(false)
return c.global.cmpRemotes(toComplete, false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
4 changes: 2 additions & 2 deletions lxc/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ lxc storage create s1 dir < config.yaml

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

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down

0 comments on commit 4e3035e

Please sign in to comment.