extras/cubicweb-ctl.bash_completion
author Jérémy Bobbio <jeremy.bobbio@irq7.fr>
Wed, 05 Jun 2019 16:29:04 +0200
changeset 12642 5722d8c461eb
parent 11832 e8be49ecb522
permissions -rw-r--r--
[pkg] Switch to Debian source format 3.0 (quilt) This forces us to base the Debian package on the source of the Python package (as produced by running `python3 setup.py sdist`). While it might seem cumbersome, this greatly reduces likelyhood of mismatch between an installation via `pip` and one using the Debian package. `dpkg-source` will also document for us what is currently in the source repository but not in the Python package. Currently the missing file are: PKG-INFO cubicweb/sobjects/test/data/cubicweb_comment/schema.py cubicweb/sobjects/test/data/cubicweb_comment/__init__.py cubicweb/sobjects/test/data/cubicweb_comment/__pkginfo__.py cubicweb/sobjects/test/data/cubicweb_card/schema.py cubicweb/sobjects/test/data/cubicweb_card/entities.py cubicweb/sobjects/test/data/cubicweb_card/__init__.py cubicweb/sobjects/test/data/cubicweb_card/__pkginfo__.py cubicweb/server/test/data-migractions/cubicweb_comment/schema.py cubicweb/server/test/data-migractions/cubicweb_comment/__init__.py cubicweb/server/test/data-migractions/cubicweb_comment/__pkginfo__.py cubicweb/server/test/data-migractions/cubicweb_localperms/schema.py cubicweb/server/test/data-migractions/cubicweb_localperms/__init__.py cubicweb/server/test/data-migractions/cubicweb_localperms/__pkginfo__.py cubicweb/server/test/data-migractions/cubicweb_basket/schema.py cubicweb/server/test/data-migractions/cubicweb_basket/__init__.py cubicweb/server/test/data-migractions/cubicweb_basket/__pkginfo__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_comment/schema.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_comment/__init__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_comment/__pkginfo__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_localperms/schema.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_localperms/__init__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_localperms/__pkginfo__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_basket/schema.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_basket/__init__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_basket/__pkginfo__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_tag/schema.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_tag/__init__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_tag/__pkginfo__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_card/schema.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_card/__init__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_card/__pkginfo__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_file/schema.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_file/__init__.py cubicweb/server/test/data-migractions/migratedapp/cubicweb_file/__pkginfo__.py cubicweb/server/test/data-migractions/cubicweb_tag/schema.py cubicweb/server/test/data-migractions/cubicweb_tag/__init__.py cubicweb/server/test/data-migractions/cubicweb_tag/__pkginfo__.py cubicweb/server/test/data-migractions/cubicweb_card/schema.py cubicweb/server/test/data-migractions/cubicweb_card/__init__.py cubicweb/server/test/data-migractions/cubicweb_card/__pkginfo__.py cubicweb/server/test/data-migractions/cubicweb_file/schema.py cubicweb/server/test/data-migractions/cubicweb_file/__init__.py cubicweb/server/test/data-migractions/cubicweb_file/__pkginfo__.py cubicweb/test/data-rewrite/cubicweb_localperms/schema.py cubicweb/test/data-rewrite/cubicweb_localperms/__init__.py cubicweb/test/data-rewrite/cubicweb_localperms/__pkginfo__.py cubicweb/test/data-rewrite/cubicweb_card/schema.py cubicweb/test/data-rewrite/cubicweb_card/entities.py cubicweb/test/data-rewrite/cubicweb_card/__init__.py cubicweb/test/data-rewrite/cubicweb_card/__pkginfo__.py The current manifest will duplicate several files that are stored as symlinks in the Mercurial repository, hence the long list of ignored files in `extend-diff-ignore`.

# -*- shell-script -*-

_ec_commands()
{
    local commands
    commands="$("$ec" listcommands 2>/dev/null)" || commands=""
    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
}

_ec()
{
    local cur prev cmd cmd_index opts i
    local ec="$1"

    COMPREPLY=()
    cur="$2"
    prev="$3"

    # searching for the command
    # (first non-option argument that doesn't follow a global option that
    #  receives an argument)
    for ((i=1; $i<=$COMP_CWORD; i++)); do
	if [[ ${COMP_WORDS[i]} != -* ]]; then
	    cmd="${COMP_WORDS[i]}"
	    cmd_index=$i
	    break
	fi
    done

    if [[ "$cur" == -* ]]; then
        if [ -z "$cmd" ]; then
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '--help' -- "$cur"))
	else
	    options="$("$ec" listcommands "$cmd" 2>/dev/null)" || commands=""
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options' -- "$cur"))
	fi
	return
    fi

    if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
	_ec_commands
	return
    fi

    # try to generate completion candidates for whatever command the user typed
    if _ec_command_specific; then
	return
    fi
}

_ec_command_specific()
{
    if [ "$(type -t "_ec_cmd_$cmd")" = function ]; then
	"_ec_cmd_$cmd"
	return 0
    fi

    case "$cmd" in
	client)
	    if [ "$prev" == "-b" ] || [ "$prev" == "--batch" ]; then
		COMPREPLY=( $( compgen -o filenames -G "$cur*" ) )
		return
	    fi
	    options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options=""
	    instances="$("$ec" listinstances 2>/dev/null)" || instances=""
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur"))
	;;
	db-dump)
	    if [ "$prev" == "-o" ] || [ "$prev" == "--output" ]; then
		COMPREPLY=( $( compgen -o filenames -G "$cur*" ) )
		return
	    fi
	    options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options=""
	    instances="$("$ec" listinstances 2>/dev/null)" || instances=""
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur"))
	;;
	# commands with template as argument
 	i18ncube)
	    cubes="$("$ec" listcubes 2>/dev/null)" || cubes=""
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $cubes' -- "$cur"))
 	;;
	# generic commands with instance as argument
 	start|stop|reload|restart|upgrade|start-repository|db-create|db-init|db-check|db-grant-user)
	    options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options=""
	    instances="$("$ec" listinstances 2>/dev/null)" || instances=""
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur"))
 	;;
	# generic commands without argument
 	list|newtemplate|i18ncubicweb|live-server)
	    options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options=""
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur"))
 	;;
	# generic commands without option
 	shell|i18ninstance|delete|status|schema-sync)
	    instances="$("$ec" listinstances 2>/dev/null)" || instances=""
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur"))
 	;;
	# XXX should do better
 	create)
	    options="$("$ec" listcommands "$cmd" 2>/dev/null)" || options=""
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur"))
 	;;
 	db-copy,db-restore,mboximport)
	    instances="$("$ec" listinstances 2>/dev/null)" || instances=""
	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$options $instances' -- "$cur"))
 	;;
 	*)
 	    return 1
 	;;
    esac

    return 0
}

complete -o bashdefault -o default -F _ec cubicweb-ctl 2>/dev/null \
    || complete -o default -F _ec cubicweb-ctl