Skip to content

Commit

Permalink
lxc/completion: Add cmpImageFingerprintsFromRemote and completions …
Browse files Browse the repository at this point in the history
…for `image alias create`

Signed-off-by: montag451 <[email protected]>
(cherry picked from commit 41929ea4d6c78460cd1f555e8f0e814c79f32bf4)
Signed-off-by: Kadin Sayani <[email protected]>
License: Apache-2.0
  • Loading branch information
montag451 authored and kadinsayani committed Jan 7, 2025
1 parent 72baca0 commit 1e0ed31
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lxc/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,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) {
Expand Down
17 changes: 17 additions & 0 deletions lxc/image_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 1e0ed31

Please sign in to comment.