From bbfc2f1e81b699d650b1b401db5785cdadafbb66 Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Tue, 17 Sep 2024 18:51:24 -0600 Subject: [PATCH 1/2] feat: shell auto completion, new command version, completion --- go.mod | 2 - go.sum | 14 ---- main.go | 4 ++ pkg/commands/completion/completion.go | 71 +++++++++++++++++++ .../completion/files/bash_autocomplete | 32 +++++++++ .../completion/files/zsh_autocomplete | 20 ++++++ pkg/commands/version/version.go | 29 ++++++++ pkg/common/commands.go | 5 -- 8 files changed, 156 insertions(+), 21 deletions(-) create mode 100644 pkg/commands/completion/completion.go create mode 100644 pkg/commands/completion/files/bash_autocomplete create mode 100644 pkg/commands/completion/files/zsh_autocomplete create mode 100644 pkg/commands/version/version.go diff --git a/go.mod b/go.mod index 78448ebc..45c4cc7c 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 9904b466..f155c702 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/main.go b/main.go index 91a931d2..34268ee5 100644 --- a/main.go +++ b/main.go @@ -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" ) @@ -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) } diff --git a/pkg/commands/completion/completion.go b/pkg/commands/completion/completion.go new file mode 100644 index 00000000..05241aba --- /dev/null +++ b/pkg/commands/completion/completion.go @@ -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) +} diff --git a/pkg/commands/completion/files/bash_autocomplete b/pkg/commands/completion/files/bash_autocomplete new file mode 100644 index 00000000..5d0c8f8b --- /dev/null +++ b/pkg/commands/completion/files/bash_autocomplete @@ -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 \ No newline at end of file diff --git a/pkg/commands/completion/files/zsh_autocomplete b/pkg/commands/completion/files/zsh_autocomplete new file mode 100644 index 00000000..9e58c574 --- /dev/null +++ b/pkg/commands/completion/files/zsh_autocomplete @@ -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 \ No newline at end of file diff --git a/pkg/commands/version/version.go b/pkg/commands/version/version.go new file mode 100644 index 00000000..b74944c5 --- /dev/null +++ b/pkg/commands/version/version.go @@ -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) +} diff --git a/pkg/common/commands.go b/pkg/common/commands.go index 1f44bfbd..d762ce67 100644 --- a/pkg/common/commands.go +++ b/pkg/common/commands.go @@ -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...") From c762e846d42e89b48be463fee4a7a771ac8305be Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Tue, 17 Sep 2024 19:01:28 -0600 Subject: [PATCH 2/2] docs: cli shell completion --- docs/cli-completion.md | 38 ++++++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + 2 files changed, 39 insertions(+) create mode 100644 docs/cli-completion.md diff --git a/docs/cli-completion.md b/docs/cli-completion.md new file mode 100644 index 00000000..43c06467 --- /dev/null +++ b/docs/cli-completion.md @@ -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 +``` diff --git a/mkdocs.yml b/mkdocs.yml index 4594c274..c40137c1 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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: