Skip to content

Commit

Permalink
Merge pull request #308 from ekristen/shell-completions
Browse files Browse the repository at this point in the history
feat: shell auto completion, new command version, completion
  • Loading branch information
ekristen authored Sep 18, 2024
2 parents 90d6dcf + c762e84 commit d76d6ef
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 21 deletions.
38 changes: 38 additions & 0 deletions docs/cli-completion.md
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
```
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stevenle/topsort v0.2.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ekristen/libnuke v0.18.0 h1:sF533oSET50FjgIkYV72rvgNzCzUR/AH2tQeKVim31g=
github.com/ekristen/libnuke v0.18.0/go.mod h1:riI1tjCf6r+et/9oUBd1vQeFmn2Sn6UeFUR0nWkMeYw=
github.com/ekristen/libnuke v0.19.0 h1:pXVxPlbKfYbP1iSwsNu67MQ8HNvZPEZIeKiyw/k8FWg=
github.com/ekristen/libnuke v0.19.0/go.mod h1:riI1tjCf6r+et/9oUBd1vQeFmn2Sn6UeFUR0nWkMeYw=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
Expand Down Expand Up @@ -56,8 +54,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v2 v2.27.3 h1:/POWahRmdh7uztQ3CYnaDddk0Rm90PyOgIxgW2rr41M=
github.com/urfave/cli/v2 v2.27.3/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8=
github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
Expand All @@ -70,15 +66,11 @@ go.uber.org/ratelimit v0.3.1/go.mod h1:6euWsTB6U/Nb3X++xEUXA8ciPJvr19Q/0h1+oDcJh
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -94,17 +86,11 @@ golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/ekristen/aws-nuke/v3/pkg/common"

_ "github.com/ekristen/aws-nuke/v3/pkg/commands/account"
_ "github.com/ekristen/aws-nuke/v3/pkg/commands/completion"
_ "github.com/ekristen/aws-nuke/v3/pkg/commands/config"
_ "github.com/ekristen/aws-nuke/v3/pkg/commands/list"
_ "github.com/ekristen/aws-nuke/v3/pkg/commands/nuke"
_ "github.com/ekristen/aws-nuke/v3/pkg/commands/version"

_ "github.com/ekristen/aws-nuke/v3/resources"
)
Expand Down Expand Up @@ -43,6 +45,8 @@ func main() {
logrus.Fatalf("Command %s not found.", command)
}

app.EnableBashCompletion = true

if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ nav:
- CLI:
- Usage: cli-usage.md
- Options: cli-options.md
- Shell Completion: cli-completion.md
- Experimental: cli-experimental.md
- Examples: cli-examples.md
- Config:
Expand Down
71 changes: 71 additions & 0 deletions pkg/commands/completion/completion.go
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)
}
32 changes: 32 additions & 0 deletions pkg/commands/completion/files/bash_autocomplete
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
20 changes: 20 additions & 0 deletions pkg/commands/completion/files/zsh_autocomplete
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
29 changes: 29 additions & 0 deletions pkg/commands/version/version.go
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)
}
5 changes: 0 additions & 5 deletions pkg/common/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import (

var commands []*cli.Command

// Commander --
type Commander interface {
Execute(c *cli.Context)
}

// RegisterCommand --
func RegisterCommand(command *cli.Command) {
logrus.Debugln("Registering", command.Name, "command...")
Expand Down

0 comments on commit d76d6ef

Please sign in to comment.