diff --git a/README.md b/README.md index e2a87a38..70cfc2f9 100644 --- a/README.md +++ b/README.md @@ -1192,6 +1192,10 @@ Those features are set using the environment variable `HELMFILE_EXPERIMENTAL`. H If you want to enable all experimental features set the env var to `HELMFILE_EXPERIMENTAL=true` +## `bash` and `zsh` completion + +Copy `autocomplete/helmfile_bash_autocomplete` or `autocomplete/helmfile_zsh_autocomplete` (depending on your shell of choice) to directory where you keep other shell completion scripts to make sure it is sourced. + ## Examples For more examples, see the [examples/README.md](https://github.com/roboll/helmfile/blob/master/examples/README.md) or the [`helmfile`](https://github.com/cloudposse/helmfiles/tree/master/releases) distribution by [Cloud Posse](https://github.com/cloudposse/). diff --git a/autocomplete/helmfile_bash_autocomplete b/autocomplete/helmfile_bash_autocomplete new file mode 100644 index 00000000..297439f4 --- /dev/null +++ b/autocomplete/helmfile_bash_autocomplete @@ -0,0 +1,18 @@ +#! /bin/bash + +_helmfile_bash_autocomplete() { + if [[ "${COMP_WORDS[0]}" != "source" ]]; then + local cur opts base + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + if [[ "$cur" == "-"* ]]; then + opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion ) + else + opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion ) + fi + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + fi +} + +complete -o bashdefault -o default -o nospace -F _helmfile_bash_autocomplete helmfile diff --git a/autocomplete/helmfile_zsh_autocomplete b/autocomplete/helmfile_zsh_autocomplete new file mode 100644 index 00000000..6ebdbd58 --- /dev/null +++ b/autocomplete/helmfile_zsh_autocomplete @@ -0,0 +1,23 @@ +#compdef helmfile + +_helmfile_zsh_autocomplete() { + + local -a opts + local cur + cur=${words[-1]} + if [[ "$cur" == "-"* ]]; then + opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}") + else + opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}") + fi + + if [[ "${opts[1]}" != "" ]]; then + _describe 'values' opts + else + _files + fi + + return +} + +compdef _helmfile_zsh_autocomplete helmfile diff --git a/main.go b/main.go index 111f543d..ebb8c741 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,7 @@ func main() { cliApp.Name = "helmfile" cliApp.Usage = "" cliApp.Version = version.Version + cliApp.EnableBashCompletion = true cliApp.Flags = []cli.Flag{ cli.StringFlag{ Name: "helm-binary, b",