Skip to content

Instantly share code, notes, and snippets.

@mtwentyman
Created September 13, 2016 15:09
Show Gist options
  • Save mtwentyman/05803aac5d476a5fd5fcd86243f2810f to your computer and use it in GitHub Desktop.
Save mtwentyman/05803aac5d476a5fd5fcd86243f2810f to your computer and use it in GitHub Desktop.
capp, an ohmyzsh plugin for capistrano
#compdef capp
#autoload
# Added `capp` because `cap` is a reserved word. `cap` completion doesn't work.
# https://stackoverflow.com/questions/21353937/is-cap-a-reserved-word-zsh-completion?noredirect=1#comment32196838_21353937
# http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fcap-Module
# https://github.com/capistrano/capistrano/wiki/Capistrano-Tasks
# http://askql.wordpress.com/2011/01/11/zsh-writing-own-completion/
local curcontext="$curcontext" state line ret=1
local -a _configs
_arguments -C \
'1: :->cmds' \
'2:: :->args' && ret=0
_cap_tasks() {
if [[ -f config/deploy.rb || -f Capfile ]]; then
if [[ ! -f .cap_tasks~ ]]; then
stage=`_cap_stage_list`
opt=`$(capp $stage -a > /dev/null 2>&1) && echo 'a' || echo 'v'`
capp $stage -$opt --tasks | sed 's/\(\[\)\(.*\)\(\]\)/\2:/' | awk '{command=$2; $1=$2=$3=""; gsub(/^[ \t\r\n]+/, "", $0); gsub(":", "\\:", command); print command"["$0"]"}' > .cap_tasks~
fi
OLD_IFS=$IFS
IFS=$'\n'
_values 'cap commands' $(< .cap_tasks~)
IFS=$OLD_IFS
# https://stackoverflow.com/questions/21356370/how-to-pass-the-contents-of-a-file-using-cat-to-values-zsh-completion/21357799?noredirect=1#21357799
# zmodload zsh/mapfile
# _values ${(f)mapfile[.cap_tasks~]}
fi
}
_cap_stages() {
compadd $(find config/deploy -name \*.rb | cut -d/ -f3 | sed s:.rb::g)
}
_cap_stage_list() {
if [[ -d config/deploy ]]; then
find config/deploy -name \*.rb | cut -d/ -f3 | sed s:.rb::g | head -n 1
else
echo ''
fi
}
case $state in
cmds)
if [[ -d config/deploy ]]; then
_cap_stages
else
_cap_tasks
fi
ret=0
;;
args)
_cap_tasks
ret=0
;;
esac
return ret
# Added `capp` because `cap` is a reserved word. `cap` completion doesn't work.
# https://stackoverflow.com/questions/21353937/is-cap-a-reserved-word-zsh-completion?noredirect=1#comment32196838_21353937
# http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fcap-Module
func capp() {
if [ -f Gemfile ]
then
bundle exec cap $*
else
cap $*
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment