From d327fbce3c861a25ea8b81867089fa7573c7787c Mon Sep 17 00:00:00 2001 From: montag451 Date: Tue, 3 Dec 2024 18:53:23 +0100 Subject: [PATCH 1/2] incus: Add a function to complete image fingerprints Signed-off-by: montag451 --- cmd/incus/completion.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/cmd/incus/completion.go b/cmd/incus/completion.go index 58f895498a2..309b9794874 100644 --- a/cmd/incus/completion.go +++ b/cmd/incus/completion.go @@ -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 @@ -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 { From ffc7ce4ce8edbd63c7148c6c9af72d3e91d0862b Mon Sep 17 00:00:00 2001 From: montag451 Date: Tue, 3 Dec 2024 18:54:11 +0100 Subject: [PATCH 2/2] incus: Add completion for `image alias` subcommands Signed-off-by: montag451 --- cmd/incus/image_alias.go | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/cmd/incus/image_alias.go b/cmd/incus/image_alias.go index e6bdf7a26c8..4bb5b0c5348 100644 --- a/cmd/incus/image_alias.go +++ b/cmd/incus/image_alias.go @@ -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 } @@ -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 } @@ -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 } @@ -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 }