diff --git a/lxc/completion.go b/lxc/completion.go index bcbc00b729d8..328d5cb6bee9 100644 --- a/lxc/completion.go +++ b/lxc/completion.go @@ -223,6 +223,29 @@ func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirec return results, cmpDirectives } +// cmpImageFingerprintsFromRemote provides shell completion for image fingerprints. +// It takes a partial input string and a remote and returns image fingerprints for that remote along with a shell completion directive. +func (g *cmdGlobal) cmpImageFingerprintsFromRemote(toComplete string, remote string) ([]string, cobra.ShellCompDirective) { + if remote == "" { + remote = g.conf.DefaultRemote + } + + remoteServer, _ := g.conf.GetImageServer(remote) + + images, _ := remoteServer.GetImages() + + results := make([]string, 0, len(images)) + for _, image := range images { + if !strings.HasPrefix(image.Fingerprint, toComplete) { + continue + } + + results = append(results, image.Fingerprint) + } + + return results, cobra.ShellCompDirectiveNoFileComp +} + // cmpInstanceKeys provides shell completion for all instance configuration keys. // It takes an instance name to determine instance type and returns a list of all instance configuration keys along with a shell completion directive. func (g *cmdGlobal) cmpInstanceKeys(instanceName string) ([]string, cobra.ShellCompDirective) { diff --git a/lxc/image_alias.go b/lxc/image_alias.go index 42af3c7abc1c..1a0d2a4a3a26 100644 --- a/lxc/image_alias.go +++ b/lxc/image_alias.go @@ -62,6 +62,23 @@ 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 { + remote = "" + } + + return c.global.cmpImageFingerprintsFromRemote(toComplete, remote) + } + return cmd } @@ -109,6 +126,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 } @@ -158,6 +183,14 @@ Filters may be part of the image hash or part of the image alias name. 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 } @@ -257,6 +290,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 }