Skip to content

Instantly share code, notes, and snippets.

@hirose31
Created June 3, 2011 09:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirose31/1006095 to your computer and use it in GitHub Desktop.
Save hirose31/1006095 to your computer and use it in GitHub Desktop.
bash completion for dotcloud
### dotcloud 0.3.1
# must be load .bash_completion (http://bash-completion.alioth.debian.org/)
# before load this file.
# expire_in filename expire_seconds
# expire_in foo.cache 300 # return true if go by 5min from last modification.
expire_in() {
local file=$1
local expire=$2
local mtime=$(stat --format='%Y' $file)
local now=$(date '+%s')
(( $now - $mtime > $expire ))
}
# to clear cache of namescape.deployment: rm $cache
_dotcloud_namespace_deployment() {
cache=‾/.dotcloud/.bash_namespace_deployment
if [ ! -e $cache ] || $(expire_in $cache 86400) || (( $(stat --format='%s' $cache) <= 0 )); then
dotcloud list | awk '/[[:space:]]+-/{print $2}' > $cache
fi
cat $cache
}
_compgen_dc_ns() {
local cur=$1; shift
compgen -W "$@ $(_dotcloud_namespace_deployment)" -- "$cur"
}
_dotcloud() {
local cur prev arg1
COMPREPLY=()
cur=$(_get_cword)
prev=${COMP_WORDS[COMP_CWORD-1]}
arg1=${COMP_WORDS[1]}
local commands="status info run logs deploy list alias ssh destroy push rollback create restart" # ignore setup
local services="java mongodb mysql nodejs perl php php-worker postgresql python python-worker rabbitmq redis ruby ruby-worker smtp static"
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=($( compgen -W "$commands" -- "$cur" ))
else
case $arg1 in
status|info|ssh|destroy|rollback|restart)
# take only one namespace.deployment
if [ $COMP_CWORD -eq 2 ]; then
COMPREPLY=($(_compgen_dc_ns "$cur"))
fi
;;
run)
COMPREPLY=('-- COMMAND');;
logs)
if [ $COMP_CWORD -eq 2 ]; then
COMPREPLY=($(_compgen_dc_ns "$cur" "-b"))
elif [ $COMP_CWORD -eq 3 ]; then
COMPREPLY=($(_compgen_dc_ns "$cur"))
fi
;;
deploy)
case $prev in
deploy)
COMPREPLY=($( compgen -W '-c -t' -- "$cur" ));;
-c)
COMPREPLY=("'"'{"arg1":"value"}'"'");;
-t)
COMPREPLY=($( compgen -W "$services" -- "$cur" ));;
*)
case "${COMP_WORDS[@]}" in
*¥ -t¥ *)
COMPREPLY=($(_compgen_dc_ns "$cur"));;
*)
COMPREPLY=($( compgen -W '-t' -- "$cur" ));;
esac
;;
esac
;;
'alias')
case $prev in
add|remove)
COMPREPLY=();;
*)
COMPREPLY=($( compgen -W "add remove" -- "$cur" ));;
esac
;;
list)
COMPREPLY=();;
push)
case $prev in
-b)
COMPREPLY=("BRANCH");;
-r)
COMPREPLY=("REVISION");;
*)
candidate=
[[ "${COMP_WORDS[@]}" == *¥ -b¥ * ]] || candidate="$candidate -b"
[[ "${COMP_WORDS[@]}" == *¥ -r¥ * ]] || candidate="$candidate -r"
COMPREPLY=($(_compgen_dc_ns "$cur" $candidate));;
esac
;;
create)
[ $COMP_CWORD -eq 2 ] && COMPREPLY=("NAMESPACE");;
*)
;;
esac
fi
}
complete -F _dotcloud dotcloud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment