Skip to content

bash/zsh completion

Pre-release
Pre-release
Compare
Choose a tag to compare
@daidokoro daidokoro released this 29 Dec 18:23
· 62 commits to master since this release

Changes:

It is now possible to let cobra generate autocompletion code for bash and zsh by using a new completion command like this:

# for current bash session
qaz completion bash > /tmp/qaz-completion.sh
source /tmp/qaz-completion.sh

# for future bash sessions if using homebrew
qaz completion bash > $(brew --prefix)/etc/bash_completion.d/qaz

# for current zsh session
source <(qaz completion zsh)

# for future zsh sessions
qaz completion zsh > "${fpath[1]}/_qaz"

The resulting completion script could be improved by implementing autocompletion for config files, stack names and profiles like this (here: zsh, note the :profile:_profiles and :stack:_stacks extension to refer the custom functions _profile and _stacks in the argument description)

function _stacks {
  # warning: this operates on the default config.yml and does not consider a previously given config file using the -c|--config option
  local -a stacks
  stacks=( $(echo "ls" | qaz shell | awk 'NR>5{flag=1}/EOF/{flag=0}flag' | sort) )
  _describe 'stack'  stacks
}

function _profiles {
  local -a profiles
  profiles=( $( cat ~/.aws/credentials 2>/dev/null | sed -n "s/^\[\(.*\)\]$/\1/p" | sort) )
  _describe 'profile' profiles
}

function _qaz {
  local -a commands

  _arguments -C \
    '--debug[Run in debug mode...]' \
    '--no-colors[disable colors in outputs]' \
    '(-p --profile)'{-p,--profile}'[configured aws profile]:profile:_profiles' \
...
function _qaz_change_create {
  _arguments \
    '(-c --config)'{-c,--config}'[path to config file (Required)]:filename:_files' \
    '(-s --stack)'{-s,--stack}'[Qaz local project Stack Name (Required)]:stack:_stacks' \
...

The full extended completion script can be found here: https://gist.githubusercontent.com/thorstenhuhn/7a8356cc57cc07d120eb2e4edb111790/raw/4372f7a280a2116b8dcf3eaa4f0ef2b47318cbe4/_qaz

Author: @thorstenhuhn