Skip to content

Commit

Permalink
Adds dynamic completions for auth contexts (#1044)
Browse files Browse the repository at this point in the history
* Add dynamic completion for the `--context` flag

* Add dynamic completion to `auth` sub commands
  • Loading branch information
scotchneat committed Oct 20, 2021
1 parent 1d5c983 commit 68e1d64
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
26 changes: 24 additions & 2 deletions commands/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,20 @@ You can provide a name to this initialization via the `+"`"+`--context`+"`"+` fl
If the `+"`"+`--context`+"`"+` flag is not specified, a default authentication context will be created during initialization.
If doctl is never initialized, you will need to specify an API token whenever you use a `+"`"+`doctl`+"`"+` command via the `+"`"+`--access-token`+"`"+` flag.`, Writer, false)
cmdBuilderWithInit(cmd, RunAuthSwitch, "switch", "Switches between authentication contexts", `This command allows you to switch between accounts with authentication contexts you've already created.
cmdAuthSwitch := cmdBuilderWithInit(cmd, RunAuthSwitch, "switch", "Switches between authentication contexts", `This command allows you to switch between accounts with authentication contexts you've already created.
To see a list of available authentication contexts, call `+"`"+`doctl auth list`+"`"+`.
For details on creating an authentication context, see the help for `+"`"+`doctl auth init`+"`"+`.`, Writer, false)
cmdBuilderWithInit(cmd, RunAuthRemove, "remove", "Remove authentication contexts ", `This command allows you to remove authentication contexts you've already created.
cmdAuthSwitch.AddValidArgsFunc(authContextListValidArgsFunc)

cmdAuthRemove := cmdBuilderWithInit(cmd, RunAuthRemove, "remove", "Remove authentication contexts ", `This command allows you to remove authentication contexts you've already created.
To see a list of available authentication contexts, call `+"`"+`doctl auth list`+"`"+`.
For details on creating an authentication context, see the help for `+"`"+`doctl auth init`+"`"+`.`, Writer, false)
cmdAuthRemove.AddValidArgsFunc(authContextListValidArgsFunc)

cmdAuthList := cmdBuilderWithInit(cmd, RunAuthList, "list", "List available authentication contexts", `List named authentication contexts that you created with `+"`"+`doctl auth init`+"`"+`.
To switch between the contexts use `+"`"+`doctl switch <name>`+"`"+`, where `+"`"+`<name>`+"`"+` is one of the contexts listed.
Expand Down Expand Up @@ -272,3 +276,21 @@ func defaultConfigFileWriter() (io.WriteCloser, error) {

return f, nil
}

func getAuthContextList() []string {
contexts := []string{"default"}
cfgContexts := viper.GetStringMap("auth-contexts")

for k := range cfgContexts {
contexts = append(contexts, k)
}

return contexts
}

func authContextListValidArgsFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return getAuthContextList(), cobra.ShellCompDirectiveNoFileComp
}
14 changes: 14 additions & 0 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package commands

import (
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -46,6 +47,19 @@ func (c *Command) ChildCommands() []*Command {
return c.childCommands
}

type ValidArgsFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)

// AddValidArgsFunc sets the function to run for dynamic completions
// ValidArgsFunc and ValidArgs are mutually exclusive. This function will
// return an error if ValidArgs is already set.
func (c *Command) AddValidArgsFunc(fn ValidArgsFunc) error {
if len(c.Command.ValidArgs) == 0 {
c.Command.ValidArgsFunction = fn
return nil
}
return errors.New("unable to add ValidArgsFunction when ValidArgs is already set")
}

// CmdBuilder builds a new command.
func CmdBuilder(parent *Command, cr CmdRunner, cliText, shortdesc string, longdesc string, out io.Writer, options ...cmdOption) *Command {
return cmdBuilderWithInit(parent, cr, cliText, shortdesc, longdesc, out, true, options...)
Expand Down
4 changes: 4 additions & 0 deletions commands/doit.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func init() {
viper.BindPFlag("output", rootPFlagSet.Lookup(doctl.ArgOutput))

rootPFlagSet.StringVarP(&Context, doctl.ArgContext, "", "", "Specify a custom authentication context name")
DoitCmd.RegisterFlagCompletionFunc(doctl.ArgContext, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return getAuthContextList(), cobra.ShellCompDirectiveNoFileComp
})

rootPFlagSet.BoolVarP(&Trace, "trace", "", false, "Show a log of network activity while performing a command")
rootPFlagSet.BoolVarP(&Verbose, doctl.ArgVerbose, "v", false, "Enable verbose output")

Expand Down

0 comments on commit 68e1d64

Please sign in to comment.