Skip to content

Commit

Permalink
incus/image: add dynamic command line completions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcstephens committed Feb 10, 2024
1 parent 24f4e6b commit 2e50674
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/incus/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@ import (
"github.com/spf13/cobra"
)

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

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 {
for _, alias := range image.Aliases {
var name string

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

results = append(results, name)
}
}

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

return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDirective) {
results := []string{}

Expand Down
8 changes: 8 additions & 0 deletions cmd/incus/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ incus create images:ubuntu/22.04 u1 < config.yaml
cmd.Flags().BoolVar(&c.flagEmpty, "empty", false, i18n.G("Create an empty instance"))
cmd.Flags().BoolVar(&c.flagVM, "vm", false, i18n.G("Create a virtual machine"))

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
101 changes: 101 additions & 0 deletions cmd/incus/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ It requires the source to be an alias and for it to be public.`))
cmd.Flags().StringArrayVarP(&c.flagProfile, "profile", "p", nil, i18n.G("Profile to apply to the new image")+"``")
cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpImages(toComplete)
}

if len(args) == 1 {
return c.global.cmpRemotes(false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -317,6 +329,10 @@ func (c *cmdImageDelete) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return c.global.cmpImages(toComplete)
}

return cmd
}

Expand Down Expand Up @@ -374,6 +390,14 @@ incus image edit <image> < image.yaml

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpImages(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -498,6 +522,14 @@ The output target is optional and defaults to the working directory.`))
cmd.Flags().BoolVar(&c.flagVM, "vm", false, i18n.G("Query virtual machine images"))
cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpImages(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -659,6 +691,18 @@ Directory import is only available on Linux and must be performed as root.`))
cmd.Flags().StringArrayVar(&c.flagAliases, "alias", nil, i18n.G("New aliases to add to the image")+"``")
cmd.RunE = c.Run

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

if len(args) == 1 {
return c.global.cmpRemotes(false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -889,6 +933,14 @@ func (c *cmdImageInfo) Command() *cobra.Command {
cmd.Flags().BoolVar(&c.flagVM, "vm", false, i18n.G("Query virtual machine images"))
cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpImages(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -1047,6 +1099,14 @@ Column shorthand chars:
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
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 @@ -1342,6 +1402,10 @@ func (c *cmdImageRefresh) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return c.global.cmpImages(toComplete)
}

return cmd
}

Expand Down Expand Up @@ -1423,6 +1487,14 @@ func (c *cmdImageShow) Command() *cobra.Command {
cmd.Flags().BoolVar(&c.flagVM, "vm", false, i18n.G("Query virtual machine images"))
cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpImages(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -1481,6 +1553,19 @@ func (c *cmdImageGetProp) 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 c.global.cmpImages(toComplete)
}

if len(args) == 1 {
// individual image prop could complete here
return nil, cobra.ShellCompDirectiveNoFileComp
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -1533,6 +1618,14 @@ func (c *cmdImageSetProp) 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 c.global.cmpImages(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -1589,6 +1682,14 @@ func (c *cmdImageUnsetProp) 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 c.global.cmpImages(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/incus/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ incus launch images:ubuntu/22.04 v1 --vm -c limits.cpu=4 -c limits.memory=4GiB
cmd.Flags().StringVar(&c.flagConsole, "console", "", i18n.G("Immediately attach to the console")+"``")
cmd.Flags().Lookup("console").NoOptDefVal = "console"

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 2e50674

Please sign in to comment.