Skip to content

Commit

Permalink
lxc/publish: Complete snapshot names
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
(cherry picked from commit 6786970433f10fcd8687d8a3e50315444ef95965)
Signed-off-by: Kadin Sayani <[email protected]>
License: Apache-2.0
  • Loading branch information
stgraber authored and kadinsayani committed Aug 26, 2024
1 parent 6bd9934 commit 684974b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
44 changes: 43 additions & 1 deletion lxc/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"regexp"
"strings"

"github.com/canonical/lxd/lxd/instance/instancetype"
"github.com/spf13/cobra"

"github.com/canonical/lxd/lxd/instance/instancetype"
"github.com/canonical/lxd/shared"
)

func (g *cmdGlobal) cmpClusterGroupNames(toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -297,6 +299,46 @@ func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDi
return results, cmpDirectives
}

func (g *cmdGlobal) cmpInstancesAndSnapshots(toComplete string) ([]string, cobra.ShellCompDirective) {
results := []string{}
cmpDirectives := cobra.ShellCompDirectiveNoFileComp

resources, _ := g.ParseServers(toComplete)

if len(resources) > 0 {
resource := resources[0]

if strings.Contains(resource.name, shared.SnapshotDelimiter) {
instName := strings.SplitN(resource.name, shared.SnapshotDelimiter, 2)[0]
snapshots, _ := resource.server.GetInstanceSnapshotNames(instName)
for _, snapshot := range snapshots {
results = append(results, fmt.Sprintf("%s/%s", instName, snapshot))
}
} else {
instances, _ := resource.server.GetInstanceNames("")
for _, instance := range instances {
var name string

if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = instance
} else {
name = fmt.Sprintf("%s:%s", resource.remote, instance)
}

results = append(results, name)
}
}
}

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

return results, cmpDirectives
}

func (g *cmdGlobal) cmpInstanceNamesFromRemote(toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string

Expand Down
2 changes: 1 addition & 1 deletion lxc/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *cmdPublish) 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)
return c.global.cmpInstancesAndSnapshots(toComplete)
}

if len(args) == 1 {
Expand Down

0 comments on commit 684974b

Please sign in to comment.