Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incus: Tweak completion logic #902

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions cmd/incus/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"

"github.com/lxc/incus/v6/internal/instance"
"github.com/lxc/incus/v6/shared/api"
)

func (g *cmdGlobal) cmpClusterGroupNames(toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -291,17 +292,14 @@ func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDi
if len(resources) > 0 {
resource := resources[0]

instances, _ := resource.server.GetInstanceNames("container")
vms, _ := resource.server.GetInstanceNames("virtual-machine")
instances = append(instances, vms...)

for _, instance := range instances {
instances, _ := resource.server.GetInstanceNames(api.InstanceTypeAny)
for _, instName := range instances {
var name string

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

results = append(results, name)
Expand All @@ -317,6 +315,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, instance.SnapshotDelimiter) {
instName := strings.SplitN(resource.name, instance.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(api.InstanceTypeAny)
for _, instName := range instances {
var name string

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

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) {
results := []string{}

Expand Down
2 changes: 1 addition & 1 deletion cmd/incus/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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
Loading