-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from ekristen/shell-completions
feat: shell auto completion, new command version, completion
- Loading branch information
Showing
10 changed files
with
195 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# CLI Shell Completion | ||
|
||
The CLI supports shell completion for bash, and zsh. The completion script can be generated by running the | ||
following command: | ||
|
||
```console | ||
$ aws-nuke completion | ||
``` | ||
|
||
By default, the shell is `bash` unless it can detect the shell you are using. You may specify the shell by using the | ||
`--shell` flag. | ||
|
||
The command will not install the completion script for you, but it will output the script to the console. You can | ||
redirect the output to a file and source it in your shell to enable completion. | ||
|
||
Command and flag completion is supported, however for flags that require a value, the completion will not provide a list | ||
of possible values. | ||
|
||
!!! warning | ||
For flag completion to work you often need to supply only the first `-` and press tab, depending on your shell | ||
configuration `--` followed by a tag will execute the command. | ||
|
||
## Examples | ||
|
||
!!! note | ||
The following are examples of commands you can run depending on your operating system and shell configuration. | ||
|
||
### bash | ||
|
||
```console | ||
aws-nuke completion --shell bash > /etc/bash_completion.d/aws-nuke | ||
``` | ||
|
||
### zsh | ||
|
||
```console | ||
aws-nuke completion --shell zsh > /usr/share/zsh/site-functions/_aws-nuke | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package completion | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"os" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/ekristen/aws-nuke/v3/pkg/commands/global" | ||
"github.com/ekristen/aws-nuke/v3/pkg/common" | ||
) | ||
|
||
//go:embed files/* | ||
var files embed.FS | ||
|
||
func execute(c *cli.Context) error { | ||
var autocomplete []byte | ||
var err error | ||
switch c.String("shell") { | ||
case "bash": | ||
autocomplete, err = files.ReadFile("files/bash_autocomplete") | ||
case "zsh": | ||
autocomplete, err = files.ReadFile("files/zsh_autocomplete") | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(string(autocomplete)) | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
shellValue := "bash" | ||
shellActual := os.Getenv("SHELL") | ||
if strings.Contains(shellActual, "zsh") { | ||
shellValue = "zsh" | ||
} | ||
|
||
flags := []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "shell", | ||
Usage: "shell to generate completion script for", | ||
Value: shellValue, | ||
Action: func(c *cli.Context, val string) error { | ||
validShells := []string{"bash", "zsh"} | ||
if !slices.Contains(validShells, val) { | ||
return fmt.Errorf("unsupported shell %s", val) | ||
} | ||
|
||
return nil | ||
}, | ||
}, | ||
} | ||
|
||
cmd := &cli.Command{ | ||
Name: "completion", | ||
Usage: "generate shell completion script", | ||
Description: "generate shell completion script", | ||
Flags: append(flags, global.Flags()...), | ||
Before: global.Before, | ||
Action: execute, | ||
} | ||
|
||
common.RegisterCommand(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#! /bin/bash | ||
|
||
# Macs have bash3 for which the bash-completion package doesn't include | ||
# _init_completion. This is a minimal version of that function. | ||
_cli_init_completion() { | ||
COMPREPLY=() | ||
_get_comp_words_by_ref "$@" cur prev words cword | ||
} | ||
|
||
_cli_bash_autocomplete() { | ||
if [[ "${COMP_WORDS[0]}" != "source" ]]; then | ||
local cur opts base words | ||
COMPREPLY=() | ||
cur="${COMP_WORDS[COMP_CWORD]}" | ||
if declare -F _init_completion >/dev/null 2>&1; then | ||
_init_completion -n "=:" || return | ||
else | ||
_cli_init_completion -n "=:" || return | ||
fi | ||
words=("${words[@]:0:$cword}") | ||
if [[ "$cur" == "-"* ]]; then | ||
requestComp="${words[*]} ${cur} --generate-bash-completion" | ||
else | ||
requestComp="${words[*]} --generate-bash-completion" | ||
fi | ||
opts=$(eval "${requestComp}" 2>/dev/null) | ||
COMPREPLY=($(compgen -W "${opts}" -- ${cur})) | ||
return 0 | ||
fi | ||
} | ||
|
||
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete aws-nuke |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#compdef aws-nuke | ||
|
||
_cli_zsh_autocomplete() { | ||
local -a opts | ||
local cur | ||
cur=${words[-1]} | ||
if [[ "$cur" == "-"* ]]; then | ||
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}") | ||
else | ||
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}") | ||
fi | ||
|
||
if [[ "${opts[1]}" != "" ]]; then | ||
_describe 'values' opts | ||
else | ||
_files | ||
fi | ||
} | ||
|
||
compdef _cli_zsh_autocomplete aws-nuke |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/ekristen/aws-nuke/v3/pkg/commands/global" | ||
"github.com/ekristen/aws-nuke/v3/pkg/common" | ||
) | ||
|
||
func execute(c *cli.Context) error { | ||
fmt.Println(common.AppVersion.Name, "version", common.AppVersion.Summary) | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
cmd := &cli.Command{ | ||
Name: "version", | ||
Usage: "displays the version", | ||
Description: "displays the version of aws-nuke", | ||
Flags: global.Flags(), | ||
Before: global.Before, | ||
Action: execute, | ||
} | ||
|
||
common.RegisterCommand(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters