Skip to content

Commit

Permalink
Merge pull request #1457 from montag451/image-alias-comp
Browse files Browse the repository at this point in the history
incus: Add completion for `image alias` subcommands
  • Loading branch information
stgraber authored Dec 3, 2024
2 parents 7fb0785 + ffc7ce4 commit 5ea6a42
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cmd/incus/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ import (
"github.com/lxc/incus/v6/shared/api"
)

func (g *cmdGlobal) appendCompletion(comps []string, comp, toComplete, remote string) []string {
if remote != g.conf.DefaultRemote || strings.Contains(toComplete, g.conf.DefaultRemote) {
comp = fmt.Sprintf("%s:%s", remote, comp)
}

if !strings.HasPrefix(comp, toComplete) {
return comps
}

return append(comps, comp)
}

func (g *cmdGlobal) cmpClusterGroupNames(toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
cmpDirectives := cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -241,6 +253,34 @@ func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirec
return results, cmpDirectives
}

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

if strings.Contains(toComplete, ":") {
remote = strings.Split(toComplete, ":")[0]
} else {
remote = g.conf.DefaultRemote
}

remoteServer, _ := g.conf.GetImageServer(remote)

images, _ := remoteServer.GetImages()

for _, image := range images {
results = g.appendCompletion(results, image.Fingerprint, toComplete, remote)
}

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

return results, cmpDirectives
}

func (g *cmdGlobal) cmpInstanceAllKeys() ([]string, cobra.ShellCompDirective) {
keys := []string{}
for k := range instance.InstanceConfigKeysAny {
Expand Down
46 changes: 46 additions & 0 deletions cmd/incus/image_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ func (c *cmdImageAliasCreate) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 1 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

if len(args) == 0 {
return c.global.cmpRemotes(toComplete, true)
}

remote, _, found := strings.Cut(args[0], ":")
if found {
toComplete = remote + ":" + toComplete
}

fingerprints, directives := c.global.cmpImageFingerprints(toComplete)
for i, f := range fingerprints {
fingerprints[i], _ = strings.CutPrefix(f, remote+":")
}

return fingerprints, directives
}

return cmd
}

Expand Down Expand Up @@ -114,6 +136,14 @@ func (c *cmdImageAliasDelete) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

return c.global.cmpImages(toComplete)
}

return cmd
}

Expand Down Expand Up @@ -181,6 +211,14 @@ Pre-defined column shorthand chars:

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

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

return cmd
}

Expand Down Expand Up @@ -336,6 +374,14 @@ func (c *cmdImageAliasRename) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

return c.global.cmpImages(toComplete)
}

return cmd
}

Expand Down

0 comments on commit 5ea6a42

Please sign in to comment.