From 92827dc219e336d25c411ea002cfc76fc08c6d52 Mon Sep 17 00:00:00 2001 From: Dax McDonald Date: Wed, 5 Feb 2020 15:26:43 -0700 Subject: [PATCH] Add bash/zsh completion This PR enables shell completion via urfav In order to use it, you must source the correct shell completion script in cli/autocomplete/ with the env variable PROG=rio. For example, PROG=rio source $RIO_REPO_ROOT/cli/autocomplete/bash_autocomplete There is still more to be done in order to make this consumable by end users. --- cli/autocomplete/bash_autocomplete | 21 +++++++++++++++++++++ cli/autocomplete/zsh_autocomplete | 13 +++++++++++++ cli/main.go | 1 + 3 files changed, 35 insertions(+) create mode 100755 cli/autocomplete/bash_autocomplete create mode 100644 cli/autocomplete/zsh_autocomplete diff --git a/cli/autocomplete/bash_autocomplete b/cli/autocomplete/bash_autocomplete new file mode 100755 index 000000000..f0f624183 --- /dev/null +++ b/cli/autocomplete/bash_autocomplete @@ -0,0 +1,21 @@ +#! /bin/bash + +: ${PROG:=$(basename ${BASH_SOURCE})} + +_cli_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 _cli_bash_autocomplete $PROG +unset PROG diff --git a/cli/autocomplete/zsh_autocomplete b/cli/autocomplete/zsh_autocomplete new file mode 100644 index 000000000..e4fddbe8e --- /dev/null +++ b/cli/autocomplete/zsh_autocomplete @@ -0,0 +1,13 @@ +#compdef $PROG + +_cli_zsh_autocomplete() { + + local -a opts + opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}") + + _describe 'values' opts + + return +} + +compdef _cli_zsh_autocomplete $PROG diff --git a/cli/main.go b/cli/main.go index 4a239f24e..64795174e 100644 --- a/cli/main.go +++ b/cli/main.go @@ -71,6 +71,7 @@ func main() { app.Name = appName app.Usage = "Containers made simple, as they should be" app.Version = fmt.Sprintf("%s (%s)", version.Version, version.GitCommit) + app.EnableBashCompletion = true cli.VersionPrinter = func(c *cli.Context) { fmt.Printf("%s version %s\n", app.Name, app.Version) }