complete -F _emote emote function _emote() { local cmd; if [ $COMP_CWORD -eq 1 ] then _emote_top else cmd="${COMP_WORDS[1]}" case "$cmd" in add) _emote_add ;; build) _emote_artifact ;; create) _emote_create ;; deploy) _emote_artifact ;; download) _emote_download ;; exec) _emote_default ;; generateFromWsdl) _emote_generateFromWsdl ;; getWsdl) ;; list) _emote_list ;; log) _emote_log ;; status) ;; test) _emote_test ;; undeploy) _emote_undeploy ;; esac fi } function _emote_top() { local cmds cur cur="${COMP_WORDS[1]}" cmds=("add build create deploy download exec generateFromWsdl getWsdl list log status test undeploy") COMPREPLY=($(compgen -W "${cmds}" -- ${cur}) ) } function _emote_add() { local types parent word matches if [ "${COMP_WORDS[${COMP_CWORD}-1]}" = "--template" -o "${COMP_WORDS[${COMP_CWORD}-1]}" = "--t" ] then parent=$EMOTE_HOME/template word="${COMP_WORDS[${COMP_CWORD}]}" matches="$(_emote_subdirs $parent skipHidden)" COMPREPLY=($(compgen -W "${matches}" -- ${word}) ) return; fi if [ $COMP_CWORD -eq 2 ] then types="all app model operation proxy test theme" COMPREPLY=($(compgen -W "${types}" -- ${COMP_WORDS[2]} )) return; fi } function _emote_create() { local word parent if [ $COMP_CWORD -eq 2 ] then _emote_default elif [ "${COMP_WORDS[${COMP_CWORD}-1]}" = "--template" -o "${COMP_WORDS[${COMP_CWORD}-1]}" = "--t" ] then parent=$EMOTE_HOME/template word="${COMP_WORDS[${COMP_CWORD}]}" matches="$(_emote_subdirs $parent)" COMPREPLY=($(compgen -W "${matches}" -- ${word}) ) fi } function _emote_download() { if [ $COMP_CWORD -eq 2 ] then word="${COMP_WORDS[2]}" COMPREPLY=($(compgen -W "app sample" -- ${word}) ) fi } function _emote_generateFromWsdl() { local word matches parent projdir projdir="$(_emote_find_project)" parent="" if [ "$projdir" != "" ] then if [ $COMP_CWORD -eq 2 -a -d $projdir/model ] then parent=$projdir/model word="${COMP_WORDS[2]}" fi fi if [ parent != "" ] then matches="$(_emote_subdirs $parent)" COMPREPLY=($(compgen -W "${matches}" -- ${word}) ) fi } function _emote_list() { local types if [ $COMP_CWORD -eq 2 ] then types="app externalSystem model proxy resource sample template" COMPREPLY=($(compgen -W "${types}" -- ${COMP_WORDS[2]} )) return; fi } function _emote_log() { if [ "${COMP_WORDS[${COMP_CWORD}-1]}" = "--logLevel" ] then parent=$EMOTE_HOME/template word="${COMP_WORDS[${COMP_CWORD}]}" matches="AUDIT DEBUG ERROR INFO WARN" COMPREPLY=($(compgen -W "${matches}" -- ${word}) ) fi } function _emote_test { local word matches parent projdir projdir="$(_emote_find_project)" parent="" if [ "$projdir" != "" ] then if [ $COMP_CWORD -eq 2 -a -d $projdir/test ] then parent=$projdir/test word="${COMP_WORDS[2]}" matches="$(ls $projdir/test)" COMPREPLY=($(compgen -W "${matches}" -- ${word}) ) fi fi } function _emote_undeploy() { local word matches parent projdir projdir="$(_emote_find_project)" matches="" if [ "$projdir" != "" ] then if [ $COMP_CWORD -eq 2 ] then word="${COMP_WORDS[2]}" matches="app model operation theme" elif [ $COMP_CWORD -eq 3 ] then parent="$projdir/${COMP_WORDS[2]}" word="${COMP_WORDS[3]}" matches="$(_emote_subdirs $parent)" fi fi if [ "$matches" != "" ] then COMPREPLY=($(compgen -W "${matches}" -- ${word}) ) fi } # # commands of the form: command artifactType artifact function _emote_artifact() { local word matches parent projdir projdir="$(_emote_find_project)" parent="" if [ "$projdir" != "" ] then if [ $COMP_CWORD -eq 2 ] then parent=$projdir word="${COMP_WORDS[2]}" elif [ $COMP_CWORD -eq 3 ] then parent="$projdir/${COMP_WORDS[2]}" word="${COMP_WORDS[3]}" fi fi if [ parent != "" ] then matches="$(_emote_subdirs $parent)" COMPREPLY=($(compgen -W "${matches}" -- ${word}) ) fi } # # arguments: # 1: the parent directory # 1: skip hidden directories if non-null # # returns the subdirectories that are not hidden function _emote_subdirs { local children child fullpath matches skipHidden skipHidden=$2 matches="" if [ -d $1 ] then children="$(ls $1)" for child in $children do fullpath=$1/$child if [ -d $fullpath ] then if [ -z "$skipHidden" ] then matches="$matches $child" elif [[ $child != _* ]] then matches="$matches $child" fi fi done fi echo $matches } # apply default file completion function _emote_default() { local reply reply=($(compgen -f ${COMP_WORDS[${COMP_CWORD}]} ) ) COMPREPLY=() for word in ${reply[@]} do if [ -d "$word" ] then word=${word}/ fi COMPREPLY=("${COMPREPLY[@]}" $word) done } # return the current emote project directory # (or the empty string if not in a project) # function _emote_find_project() { local projectDir projectDir="$(pwd)" if ( echo $projectDir | egrep -q ' |'$'\t' ) then echo "" return fi while [ ! -r $projectDir/project.json ] do if [ $projectDir = "/" ] then echo "" return fi projectDir="$(dirname $projectDir)" done echo $projectDir }