Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incus/image: add dynamic command line completions #477

Merged
merged 2 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 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 All @@ -23,7 +59,7 @@ func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDi
for _, instance := range instances {
var name string

if resource.remote == g.conf.DefaultRemote {
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = instance
} else {
name = fmt.Sprintf("%s:%s", resource.remote, instance)
Expand All @@ -34,7 +70,7 @@ func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDi
}

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

Expand All @@ -57,7 +93,7 @@ func (g *cmdGlobal) cmpNetworks(toComplete string) ([]string, cobra.ShellCompDir
for _, network := range networks {
var name string

if resource.remote == g.conf.DefaultRemote {
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = network.Name
} else {
name = fmt.Sprintf("%s:%s", resource.remote, network.Name)
Expand All @@ -68,7 +104,7 @@ func (g *cmdGlobal) cmpNetworks(toComplete string) ([]string, cobra.ShellCompDir
}

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

Expand Down Expand Up @@ -167,7 +203,7 @@ func (g *cmdGlobal) cmpProfiles(toComplete string) ([]string, cobra.ShellCompDir
for _, profile := range profiles {
var name string

if resource.remote == g.conf.DefaultRemote {
if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = profile
} else {
name = fmt.Sprintf("%s:%s", resource.remote, profile)
Expand All @@ -178,18 +214,18 @@ func (g *cmdGlobal) cmpProfiles(toComplete string) ([]string, cobra.ShellCompDir
}

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

return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpRemotes() ([]string, cobra.ShellCompDirective) {
func (g *cmdGlobal) cmpRemotes(includeAll bool) ([]string, cobra.ShellCompDirective) {
results := []string{}

for remoteName, rc := range g.conf.Remotes {
if rc.Protocol != "incus" && rc.Protocol != "" {
if !includeAll && rc.Protocol != "incus" && rc.Protocol != "" {
continue
}

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
4 changes: 2 additions & 2 deletions cmd/incus/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ incus network create bar network=baz --type ovn
return nil, cobra.ShellCompDirectiveNoFileComp
}

return c.global.cmpRemotes()
return c.global.cmpRemotes(false)
}

return cmd
Expand Down Expand Up @@ -1015,7 +1015,7 @@ func (c *cmdNetworkList) Command() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
}

return c.global.cmpRemotes()
return c.global.cmpRemotes(false)
}

return cmd
Expand Down
Loading