Skip to content

Commit

Permalink
lxc: Improved cli completions (from Incus) (#13959)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomponline authored Sep 5, 2024
2 parents fe5115b + 9bce14a commit 71381e6
Show file tree
Hide file tree
Showing 76 changed files with 55,039 additions and 52,449 deletions.
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Separate commits should be used for:
- API structure (`shared/api: Add XYZ` for changes to `shared/api/`)
- Go client package (`client: Add XYZ` for changes to `client/`)
- CLI (`lxc/<command>: Change XYZ` for changes to `lxc/`)
- Scripts (`scripts: Update bash completion for XYZ` for changes to `scripts/`)
- LXD daemon (`lxd/<package>: Add support for XYZ` for changes to `lxd/`)
- Tests (`tests: Add test for XYZ` for changes to `tests/`)

Expand Down
27 changes: 22 additions & 5 deletions lxc/action.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -32,6 +33,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 +59,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 @@ -77,6 +86,10 @@ func (c *cmdRestart) command() *cobra.Command {
The opposite of "lxc pause" is "lxc start".`))

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

return cmd
}

Expand All @@ -98,6 +111,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 Expand Up @@ -144,7 +161,7 @@ func (c *cmdAction) Command(action string) *cobra.Command {
func (c *cmdAction) doActionAll(action string, resource remoteResource) error {
if resource.name != "" {
// both --all and instance name given.
return fmt.Errorf(i18n.G("Both --all and instance name given"))
return errors.New(i18n.G("Both --all and instance name given"))
}

remote := resource.remote
Expand Down Expand Up @@ -216,7 +233,7 @@ func (c *cmdAction) doAction(action string, conf *config.Config, nameArg string)
}

if action == "stop" && c.flagForce && c.flagConsole != "" {
return fmt.Errorf(i18n.G("--console can't be used while forcing instance shutdown"))
return errors.New(i18n.G("--console can't be used while forcing instance shutdown"))
}

remote, name, err := conf.ParseRemote(nameArg)
Expand Down Expand Up @@ -321,7 +338,7 @@ func (c *cmdAction) run(cmd *cobra.Command, args []string) error {
for _, resource := range resources {
// We don't allow instance names with --all.
if resource.name != "" {
return fmt.Errorf(i18n.G("Both --all and instance name given"))
return errors.New(i18n.G("Both --all and instance name given"))
}

// See if we can use the bulk API.
Expand Down Expand Up @@ -365,11 +382,11 @@ func (c *cmdAction) run(cmd *cobra.Command, args []string) error {

if c.flagConsole != "" {
if c.flagAll {
return fmt.Errorf(i18n.G("--console can't be used with --all"))
return errors.New(i18n.G("--console can't be used with --all"))
}

if len(names) != 1 {
return fmt.Errorf(i18n.G("--console only works with a single instance"))
return errors.New(i18n.G("--console only works with a single instance"))
}
}

Expand Down
150 changes: 148 additions & 2 deletions lxc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ func (c *cmdClusterList) 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.cmpRemotes(false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -209,6 +217,14 @@ func (c *cmdClusterShow) 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.cmpClusterMembers(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -258,6 +274,14 @@ func (c *cmdClusterInfo) 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.cmpClusterMembers(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -309,6 +333,18 @@ func (c *cmdClusterGet) command() *cobra.Command {
cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Get the key as a cluster property"))
cmd.RunE = c.run

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -370,6 +406,14 @@ func (c *cmdClusterSet) command() *cobra.Command {
cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Set the key as a cluster property"))
cmd.RunE = c.run

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -442,6 +486,18 @@ func (c *cmdClusterUnset) command() *cobra.Command {
cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Unset the key as a cluster property"))
cmd.RunE = c.run

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

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -474,6 +530,14 @@ func (c *cmdClusterRename) 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.cmpClusterMembers(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -526,6 +590,14 @@ func (c *cmdClusterRemove) command() *cobra.Command {
cmd.Flags().BoolVarP(&c.flagForce, "force", "f", false, i18n.G("Force removing a member, even if degraded"))
cmd.Flags().BoolVar(&c.flagNonInteractive, "yes", false, i18n.G("Don't require user confirmation for using --force"))

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -614,6 +686,14 @@ func (c *cmdClusterEnable) 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.cmpRemotes(false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -685,7 +765,7 @@ type cmdClusterEdit struct {

func (c *cmdClusterEdit) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("edit", i18n.G("[<remote>:]<cluster member>"))
cmd.Use = usage("edit", i18n.G("[<remote>:]<member>"))
cmd.Short = i18n.G("Edit cluster member configurations as YAML")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Edit cluster member configurations as YAML`))
Expand All @@ -695,6 +775,14 @@ func (c *cmdClusterEdit) 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.cmpClusterMembers(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -800,13 +888,21 @@ type cmdClusterAdd struct {

func (c *cmdClusterAdd) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("add", i18n.G("[[<remote>:]<name>]"))
cmd.Use = usage("add", i18n.G("[[<remote>:]<member>]"))
cmd.Short = i18n.G("Request a join token for adding a cluster member")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(`Request a join token for adding a cluster member`))
cmd.Flags().StringVar(&c.flagName, "name", "", i18n.G("Cluster member name (alternative to passing it as an argument)")+"``")

cmd.RunE = c.run

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -883,6 +979,14 @@ func (c *cmdClusterListTokens) 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.cmpRemotes(false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -983,6 +1087,15 @@ func (c *cmdClusterRevokeToken) command() *cobra.Command {
cmd.Long = cli.FormatSection(i18n.G("Description"), cmd.Short)

cmd.RunE = c.run

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -1063,6 +1176,23 @@ func (c *cmdClusterUpdateCertificate) command() *cobra.Command {
i18n.G("Update cluster certificate with PEM certificate and key read from input files."))

cmd.RunE = c.run

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

if len(args) == 1 {
return nil, cobra.ShellCompDirectiveDefault
}

if len(args) == 2 {
return nil, cobra.ShellCompDirectiveDefault
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -1171,6 +1301,14 @@ func (c *cmdClusterEvacuate) command() *cobra.Command {
cmd.Flags().BoolVar(&c.action.flagForce, "force", false, i18n.G(`Force evacuation without user confirmation`)+"``")
cmd.Flags().StringVar(&c.action.flagAction, "action", "", i18n.G(`Force a particular evacuation action`)+"``")

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand All @@ -1192,6 +1330,14 @@ func (c *cmdClusterRestore) command() *cobra.Command {

cmd.Flags().BoolVar(&c.action.flagForce, "force", false, i18n.G(`Force restoration without user confirmation`)+"``")

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

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down
Loading

0 comments on commit 71381e6

Please sign in to comment.