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: add completions for profiles and instance actions #542

Merged
merged 2 commits into from
Feb 27, 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
20 changes: 20 additions & 0 deletions cmd/incus/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (c *cmdStart) Command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Start instances`))

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

return cmd
}

Expand All @@ -54,6 +58,10 @@ func (c *cmdPause) Command() *cobra.Command {
`Pause instances`))
cmd.Aliases = []string{"freeze"}

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

return cmd
}

Expand All @@ -76,6 +84,10 @@ func (c *cmdResume) Command() *cobra.Command {
`Resume instances`))
cmd.Aliases = []string{"unfreeze"}

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

return cmd
}

Expand All @@ -99,6 +111,10 @@ func (c *cmdRestart) Command() *cobra.Command {

The opposite of "incus pause" is "incus start".`))

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

return cmd
}

Expand All @@ -120,6 +136,10 @@ func (c *cmdStop) Command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Stop instances`))

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

return cmd
}

Expand Down
43 changes: 41 additions & 2 deletions cmd/incus/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ func (g *cmdGlobal) cmpInstanceAllKeys() ([]string, cobra.ShellCompDirective) {
return keys, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpInstanceSnapshots(instanceName string) ([]string, cobra.ShellCompDirective) {
resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]
client := resource.server

snapshots, err := client.GetInstanceSnapshotNames(instanceName)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

return snapshots, cobra.ShellCompDirectiveNoFileComp
}

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

Expand Down Expand Up @@ -200,7 +217,29 @@ func (g *cmdGlobal) cmpNetworkProfiles(networkName string) ([]string, cobra.Shel
return results, cobra.ShellCompDirectiveError
}

func (g *cmdGlobal) cmpProfiles(toComplete string) ([]string, cobra.ShellCompDirective) {
func (g *cmdGlobal) cmpProfileConfigs(profileName string) ([]string, cobra.ShellCompDirective) {
resources, err := g.ParseServers(profileName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]
client := resource.server

profile, _, err := client.GetProfile(resource.name)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var configs []string
for c := range profile.Config {
configs = append(configs, c)
}

return configs, cobra.ShellCompDirectiveNoFileComp
}

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

resources, _ := g.ParseServers(toComplete)
Expand All @@ -223,7 +262,7 @@ func (g *cmdGlobal) cmpProfiles(toComplete string) ([]string, cobra.ShellCompDir
}
}

if !strings.Contains(toComplete, ":") {
if includeRemotes && !strings.Contains(toComplete, ":") {
remotes, _ := g.cmpRemotes(false)
results = append(results, remotes...)
}
Expand Down
12 changes: 12 additions & 0 deletions cmd/incus/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ The pull transfer mode is the default as it is compatible with all server versio
cmd.Flags().BoolVar(&c.flagRefresh, "refresh", false, i18n.G("Perform an incremental copy"))
cmd.Flags().BoolVar(&c.flagAllowInconsistent, "allow-inconsistent", false, i18n.G("Ignore copy errors for volatile files"))

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/incus/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (c *cmdDelete) Command() *cobra.Command {
cmd.Flags().BoolVarP(&c.flagForce, "force", "f", false, i18n.G("Force the removal of running instances"))
cmd.Flags().BoolVarP(&c.flagInteractive, "interactive", "i", false, i18n.G("Require user confirmation"))

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

return cmd
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/incus/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ Mode defaults to non-interactive, interactive mode is selected if both stdin AND
cmd.Flags().Uint32Var(&c.flagGroup, "group", 0, i18n.G("Group ID to run the command as (default 0)")+"``")
cmd.Flags().StringVar(&c.flagCwd, "cwd", "", i18n.G("Directory to run the command in (default /root)")+"``")

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/incus/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ incus info [<remote>:] [--resources]
cmd.Flags().BoolVar(&c.flagResources, "resources", false, i18n.G("Show the resources available to the server"))
cmd.Flags().StringVar(&c.flagTarget, "target", "", i18n.G("Cluster member name")+"``")

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down
12 changes: 12 additions & 0 deletions cmd/incus/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ incus move <instance>/<old snapshot name> <instance>/<new snapshot name>
cmd.Flags().StringVar(&c.flagTargetProject, "target-project", "", i18n.G("Copy to a project different from the source")+"``")
cmd.Flags().BoolVar(&c.flagAllowInconsistent, "allow-inconsistent", false, i18n.G("Ignore copy errors for volatile files"))

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/incus/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (c *cmdNetworkAttachProfile) Command() *cobra.Command {
}

if len(args) == 1 {
return c.global.cmpProfiles(args[0])
return c.global.cmpProfiles(args[0], false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand Down
Loading
Loading