lrossett / centos / centpkg

Forked from centos/centpkg 4 years ago
Clone
3bff02
# centpkg bash completion
3bff02
3bff02
_centpkg()
3bff02
{
3bff02
    COMPREPLY=()
3bff02
3bff02
    in_array()
3bff02
    {
3bff02
        local i
3bff02
        for i in $2; do
3bff02
            [[ $i = $1 ]] && return 0
3bff02
        done
3bff02
        return 1
3bff02
    }
3bff02
3bff02
    _filedir_exclude_paths()
3bff02
    {
3bff02
        _filedir "$@"
3bff02
        for ((i=0; i<=${#COMPREPLY[@]}; i++)); do
3bff02
            [[ ${COMPREPLY[$i]} =~ /?\.git/? ]] && unset COMPREPLY[$i]
3bff02
        done
3bff02
    }
3bff02
3bff02
    local cur prev
3bff02
    _get_comp_words_by_ref cur prev
3bff02
3bff02
    # global options
3bff02
3bff02
    local options="--help -v -q"
3bff02
    local options_value="--dist --release --user --path --user-config --name --namespace"
3bff02
    local commands="build chain-build ci clean clog clone co commit compile \
3bff02
    container-build diff gimmespec giturl help gitbuildhash import install lint \
3bff02
    local mockbuild mock-config module-build module-build-cancel \
3bff02
    module-build-local module-build-info module-build-watch module-overview \
3bff02
    module-scratch-build \
3bff02
    new new-sources patch prep pull push retire request-branch request-repo \
3bff02
    request-tests-repo request-side-tag list-side-tags remove-side-tag \
3bff02
    scratch-build sources srpm switch-branch tag unused-patches update upload \
3bff02
    verify-files verrel override fork"
3bff02
3bff02
    # parse main options and get command
3bff02
3bff02
    local command=
3bff02
    local command_first=
3bff02
    local path=
3bff02
3bff02
    local i w
3bff02
    for (( i = 0; i < ${#COMP_WORDS[*]} - 1; i++ )); do
3bff02
        w="${COMP_WORDS[$i]}"
3bff02
        # option
3bff02
        if [[ ${w:0:1} = - ]]; then
3bff02
            if in_array "$w" "$options_value"; then
3bff02
                ((i++))
3bff02
                [[ "$w" = --path ]] && path="${COMP_WORDS[$i]}"
3bff02
            fi
3bff02
        # command
3bff02
        elif in_array "$w" "$commands"; then
3bff02
            command="$w"
3bff02
            command_first=$((i+1))
3bff02
            break
3bff02
        fi
3bff02
    done
3bff02
3bff02
    # complete base options
3bff02
3bff02
    if [[ -z $command ]]; then
3bff02
        if [[ $cur == -* ]]; then
3bff02
            COMPREPLY=( $(compgen -W "$options $options_value" -- "$cur") )
3bff02
            return 0
3bff02
        fi
3bff02
3bff02
        case "$prev" in
3bff02
            --dist | --release | --user | -u | --config)
3bff02
                ;;
3bff02
            --path)
3bff02
                _filedir_exclude_paths
3bff02
                ;;
3bff02
            --namespace)
3bff02
                COMPREPLY=( $(compgen -W "$(_centpkg_namespaces)" -- "$cur") )
3bff02
                ;;
3bff02
            *)
3bff02
                COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
3bff02
                ;;
3bff02
        esac
3bff02
3bff02
        return 0
3bff02
    fi
3bff02
3bff02
    # parse command specific options
3bff02
3bff02
    local options=
3bff02
    local options_target= options_arches= options_branch= options_string= options_file= options_dir= options_srpm= options_mroot= options_builder= options_namespace=
3bff02
    local options_update_type= options_update_request=
3bff02
    local options_yaml=
3bff02
    local after= after_more=
3bff02
3bff02
    case $command in
3bff02
        help|gimmespec|gitbuildhash|giturl|new|push|unused-patches|verrel)
3bff02
            ;;
3bff02
        build)
3bff02
            options="--nowait --background --skip-tag --scratch --skip-remote-rules-validation --fail-fast"
3bff02
            options_arches="--arches"
3bff02
            options_srpm="--srpm"
3bff02
            options_target="--target"
3bff02
            ;;
3bff02
        chain-build)
3bff02
            options="--nowait --background"
3bff02
            options_target="--target"
3bff02
            after="package"
3bff02
            after_more=true
3bff02
            ;;
3bff02
        clean)
3bff02
            options="--dry-run -x"
3bff02
            ;;
3bff02
        clog)
3bff02
            options="--raw"
3bff02
            ;;
3bff02
        clone|co)
3bff02
            options="--branches --anonymous"
3bff02
            options_branch="-b"
3bff02
            after="package"
3bff02
            ;;
3bff02
        commit|ci)
3bff02
            options="--push --clog --raw --tag --with-changelog"
3bff02
            options_string="--message"
3bff02
            options_file="--file"
3bff02
            after="file"
3bff02
            after_more=true
3bff02
            ;;
3bff02
        compile|install)
3bff02
            options="--short-circuit"
3bff02
            options_arches="--arch"
3bff02
            options_dir="--builddir"
3bff02
            ;;
3bff02
        container-build)
3bff02
            options="--scratch --nowait --repo-url --skip-remote-rules-validation"
3bff02
            options_arches="--arches"
3bff02
            options_target="--target"
3bff02
            ;;
3bff02
        diff)
3bff02
            options="--cached"
3bff02
            after="file"
3bff02
            after_more=true
3bff02
            ;;
3bff02
        import)
3bff02
            options="--create"
3bff02
            options_branch="--branch"
3bff02
            after="srpm"
3bff02
            ;;
3bff02
        lint)
3bff02
            options="--info"
3bff02
            options_file="--rpmlintconf"
3bff02
            ;;
3bff02
        list-side-tags)
3bff02
            options="--mine"
3bff02
            options_string="--user --base-tag"
3bff02
            ;;
3bff02
        local)
3bff02
            options="--md5"
3bff02
            options_arches="--arch"
3bff02
            options_string="--with --without"
3bff02
            options_dir="--builddir"
3bff02
            ;;
3bff02
        mock-config)
3bff02
            options="--target"
3bff02
            options_arches="--arch"
3bff02
            ;;
3bff02
        mockbuild)
3bff02
            options="--md5 --no-clean --no-cleanup-after --no-clean-all --shell"
3bff02
            options_string="--with --without"
3bff02
            options_mroot="--root --mock-config"
3bff02
            ;;
3bff02
        module-build)
3bff02
            options="--scratch --watch"
3bff02
            options_string="--optional --requires --buildrequires"
3bff02
            options_yaml="--file"
3bff02
            options_srpm="--srpm"
3bff02
            ;;
3bff02
        module-build-local)
3bff02
            options="--skip-tests"
3bff02
            options_string="--add-local-build --stream --set-default-stream"
3bff02
            options_yaml="--file"
3bff02
            options_srpm="--srpm"
3bff02
            ;;
3bff02
        module-overview)
3bff02
            options="--unfinished"
3bff02
            options_string="--limit"
3bff02
            ;;
3bff02
        module-scratch-build)
3bff02
            options="--watch"
3bff02
            options_string="--optional --requires --buildrequires"
3bff02
            options_yaml="--file"
3bff02
            options_srpm="--srpm"
3bff02
            ;;
3bff02
        patch)
3bff02
            options="--rediff"
3bff02
            options_string="--suffix"
3bff02
            ;;
3bff02
        prep|verify-files)
3bff02
            options_arches="--arch"
3bff02
            options_dir="--builddir"
3bff02
            ;;
3bff02
        pull)
3bff02
            options="--rebase --no-rebase"
3bff02
            ;;
3bff02
        remove-side-tag)
3bff02
            after_more=true
3bff02
            ;;
3bff02
        retire)
3bff02
            after_more=true
3bff02
            ;;
3bff02
        request-branch)
3bff02
            options="--no-git-branch --all-releases --no-auto-module"
3bff02
            options_string="--sl --repo"
3bff02
            ;;
3bff02
        request-repo)
3bff02
            options="--exception --no-initial-commit"
3bff02
            options_string="--description --monitor --upstreamurl --summary"
3bff02
            options_namespace="--namespace"
3bff02
            ;;
3bff02
        request-tests-repo)
3bff02
            options_string="--bug"
3bff02
            ;;
3bff02
        request-side-tag)
3bff02
            options_string="--base-tag"
3bff02
            ;;
3bff02
        scratch-build)
3bff02
            options="--nowait --background"
3bff02
            options_target="--target"
3bff02
            options_arches="--arches"
3bff02
            options_srpm="--srpm"
3bff02
            ;;
3bff02
        sources)
3bff02
            options_dir="--outdir"
3bff02
            ;;
3bff02
        srpm)
3bff02
            options="--md5"
3bff02
            ;;
3bff02
        switch-branch)
3bff02
            options="--list"
3bff02
            after="branch"
3bff02
            ;;
3bff02
        tag)
3bff02
            options="--clog --raw --force --list --delete"
3bff02
            options_string="--message"
3bff02
            options_file="--file"
3bff02
            after_more=true
3bff02
            ;;
3bff02
        upload|new-sources)
3bff02
            after="file"
3bff02
            after_more=true
3bff02
            ;;
3bff02
        update)
3bff02
            options="--not-close-bugs --suggest-reboot --disable-autokarma"
3bff02
            options_string="--notes --bugs --stable-karma --unstable-karma"
3bff02
            options_update_type="--type"
3bff02
            options_update_request="--request"
3bff02
            ;;
3bff02
    esac
3bff02
3bff02
    local all_options="--help $options"
3bff02
    local all_options_value="$options_target $options_arches $options_branch \
3bff02
    $options_string $options_file $options_dir $options_srpm $options_mroot \
3bff02
    $options_builder $options_namespace $options_update_type $options_update_request \
3bff02
    $options_yaml"
3bff02
3bff02
    # count non-option parameters
3bff02
3bff02
    local i w
3bff02
    local last_option=
3bff02
    local after_counter=0
3bff02
    for (( i = $command_first; i < ${#COMP_WORDS[*]} - 1; i++)); do
3bff02
        w="${COMP_WORDS[$i]}"
3bff02
        if [[ ${w:0:1} = - ]]; then
3bff02
            if in_array "$w" "$all_options"; then
3bff02
                last_option="$w"
3bff02
                continue
3bff02
            elif in_array "$w" "$all_options_value"; then
3bff02
                last_option="$w"
3bff02
                ((i++))
3bff02
                continue
3bff02
            fi
3bff02
        fi
3bff02
        in_array "$last_option" "$options_arches" || ((after_counter++))
3bff02
    done
3bff02
3bff02
    # completion
3bff02
3bff02
    if [[ -n $options_target ]] && in_array "$prev" "$options_target"; then
3bff02
        COMPREPLY=( $(compgen -W "$(_centpkg_target)" -- "$cur") )
3bff02
3bff02
    elif [[ -n $options_arches ]] && in_array "$last_option" "$options_arches"; then
3bff02
        COMPREPLY=( $(compgen -W "$(_centpkg_arch) $all_options" -- "$cur") )
3bff02
3bff02
    elif [[ -n $options_srpm ]] && in_array "$prev" "$options_srpm"; then
3bff02
        _filedir_exclude_paths "*.src.rpm"
3bff02
3bff02
    elif [[ -n $options_yaml ]] && in_array "$prev" "$options_yaml"; then
3bff02
        _filedir_exclude_paths "yaml"
3bff02
3bff02
    elif [[ -n $options_branch ]] && in_array "$prev" "$options_branch"; then
3bff02
        COMPREPLY=( $(compgen -W "$(_centpkg_branch "$path")" -- "$cur") )
3bff02
3bff02
    elif [[ -n $options_file ]] && in_array "$prev" "$options_file"; then
3bff02
        _filedir_exclude_paths
3bff02
3bff02
    elif [[ -n $options_dir ]] && in_array "$prev" "$options_dir"; then
3bff02
        _filedir_exclude_paths -d
3bff02
3bff02
    elif [[ -n $options_string ]] && in_array "$prev" "$options_string"; then
3bff02
        COMPREPLY=( )
3bff02
3bff02
    elif [[ -n $options_mroot ]] && in_array "$prev" "$options_mroot"; then
3bff02
        COMPREPLY=( )
3bff02
        if declare -F _mock_root &>/dev/null; then
3bff02
            _mock_root
3bff02
        elif declare -F _xfunc &>/dev/null; then
3bff02
            _xfunc mock _mock_root
3bff02
        fi
3bff02
3bff02
    elif [[ -n $options_namespace ]] && in_array "$prev" "$options_namespace"; then
3bff02
        COMPREPLY=( $(compgen -W "$(_centpkg_namespaces)" -- "$cur") )
3bff02
3bff02
    elif [[ -n $options_update_type ]] && in_array "$prev" "$options_update_type"; then
3bff02
        COMPREPLY=( $(compgen -W "bugfix security enhancement newpackage" -- "$cur") )
3bff02
3bff02
    elif [[ -n $options_update_request ]] && in_array "$prev" "$options_update_request"; then
3bff02
        COMPREPLY=( $(compgen -W "testing stable" -- "$cur") )
3bff02
3bff02
    else
3bff02
        local after_options=
3bff02
3bff02
        if [[ $after_counter -eq 0 ]] || [[ $after_more = true ]]; then
3bff02
            case $after in
3bff02
                file)    _filedir_exclude_paths ;;
3bff02
                srpm)    _filedir_exclude_paths "*.src.rpm" ;;
3bff02
                branch)  after_options="$(_centpkg_branch "$path")" ;;
3bff02
                package) after_options="$(_centpkg_package "$cur")";;
3bff02
            esac
3bff02
        fi
3bff02
3bff02
        if [[ $cur != -* ]]; then
3bff02
            all_options=
3bff02
            all_options_value=
3bff02
        fi
3bff02
3bff02
        COMPREPLY+=( $(compgen -W "$all_options $all_options_value $after_options" -- "$cur" ) )
3bff02
    fi
3bff02
3bff02
    return 0
3bff02
} &&
3bff02
complete -F _centpkg centpkg
3bff02
3bff02
_centpkg_target()
3bff02
{
3bff02
    koji list-targets --quiet 2>/dev/null | cut -d" " -f1
3bff02
}
3bff02
3bff02
_centpkg_arch()
3bff02
{
3bff02
    echo "i386 i686 x86_64 armv5tel armv7hl armv7hnl ppc ppc64 ppc64le ppc64p7 s390 s390x"
3bff02
}
3bff02
3bff02
_centpkg_branch()
3bff02
{
3bff02
    local git_options= format="--format %(refname:short)"
3bff02
    [[ -n $1 ]] && git_options="--git-dir=$1/.git"
3bff02
3bff02
    git $git_options for-each-ref $format 'refs/remotes' | sed 's,.*/,,'
3bff02
    git $git_options for-each-ref $format 'refs/heads'
3bff02
}
3bff02
3bff02
_centpkg_package()
3bff02
{
3bff02
    repoquery -C --qf=%{sourcerpm} "$1*" 2>/dev/null | sort -u | sed -r 's/(-[^-]*){2}\.src\.rpm$//'
3bff02
}
3bff02
3bff02
_centpkg_namespaces()
3bff02
{
3bff02
    grep "^distgit_namespaces =" /etc/rpkg/centpkg.conf | cut -d'=' -f2
3bff02
}
3bff02
3bff02
3bff02
# Local variables:
3bff02
# mode: shell-script
3bff02
# sh-basic-offset: 4
3bff02
# sh-indent-comment: t
3bff02
# indent-tabs-mode: nil
3bff02
# End:
3bff02
# ex: ts=4 sw=4 et filetype=sh