[Fuego] [PATCH] bash-completion: add completion script for ftc

Tim.Bird at sony.com Tim.Bird at sony.com
Thu Feb 28 00:21:33 UTC 2019


This is very great stuff!!  This will be super handy.

One license issue, though.  See below.

> -----Original Message-----
> From: Daniel Sangorrin
> 
> Click [Tab] to have bash show you options or try to complete
> what you are writting
> 
> Signed-off-by: Daniel Sangorrin <daniel.sangorrin at toshiba.co.jp>
> ---
>  scripts/ftc_completion.sh | 176
> ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 176 insertions(+)
>  create mode 100755 scripts/ftc_completion.sh
> 
> diff --git a/scripts/ftc_completion.sh b/scripts/ftc_completion.sh
> new file mode 100755
> index 0000000..2acb04e
> --- /dev/null
> +++ b/scripts/ftc_completion.sh
> @@ -0,0 +1,176 @@
> +# !/bin/bash
> +#
> +# Copyright (C) 2019 Toshiba corp.
> +# Author: Daniel Sangorrin <daniel.sangorrin at toshiba.co.jp>
> +#
> +# This library is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU Lesser General Public License as
> +# published by the Free Software Foundation; either version 2 of the
> +# License, or (at your option) any later version.
I'd rather not accept this contribution under this license.
I'm not sure if any of the material below was derived from somewhere else
or not.  If so, that might pose a problem.  However, the default license
for Fuego is MIT.  I would be willing to allow this particular file, which is 
somewhat separate from the rest of Fuego, to be submitted at LGPL
version 2 only.  However, I really dislike the 'or any later version' clause
in this license header.  I don't want any part of Fuego to be susceptible
to conversion in to GPL or LGPL v3.

Please let me know if this is an issue.

> +#
> +# This library is distributed in the hope that it will be useful, but
> +# WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> GNU
> +# Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public
> +# License along with this library; if not, write to the Free Software
> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
> +# USA.
> +#
> +# Bash completion script for Fuego's command line tool 'ftc'
> +#
> +# To enable the completions copy this file to
> +# /etc/bash_completion.d/ftc
> +# and make sure that your user's bashrc calls
> +# . /etc/bash_completion
> +
> +in_array() {
> +    local i
> +    for i in "${@:2}"; do
> +        [[ $1 = "$i" ]] && return
> +    done
> +}
> +
> +_ftc()
> +{
> +    local cur prev words cword
> +    local global_opts="-h --help -v -q -c -x --debug"
> +    declare -A percommand_opts=(
> +        ["add-jobs"]="-b -p -t -s --timeout --rebuild --reboot --precleanup --
> postcleanup"
> +        ["add-nodes"]="-f -b"
> +        ["add-view"]=""
> +        ["build-jobs"]=""
> +        ["config"]="-l"
> +        ["delete-var"]="-b"
> +        ["gen-report"]="--where --format --header_fields --fields --layout -o"
> +        ["help"]=""
> +        ["install-test"]="-u"
> +        ["list-boards"]="-q"
> +        ["list-jobs"]="-q"
> +        ["list-nodes"]="-q"
> +        ["list-plans"]="-q"
> +        ["list-requests"]="-q"
> +        ["list-runs"]="-q --where"
> +        ["list-specs"]="-q -t"
> +        ["list-tests"]="-q"
> +        ["package-run"]="-o -f"
> +        ["package-test"]="-o -f"
> +        ["put-request"]="-R -s"
> +        ["put-run"]=""
> +        ["put-test"]=""
> +        ["query-board"]="-b -n"
> +        ["release-resource"]="-b"
> +        ["reserve-resource"]="-f -b"
> +        ["rm-jobs"]="--remove-logs"
> +        ["rm-nodes"]=""
> +        ["rm-request"]=""
> +        ["run-request"]="--put-run"
> +        ["run-test"]="-b -t -s -p --timeout --rebuild --reboot --precleanup --
> postcleanup --dynamic-vars"
> +        ["set-var"]="-b"
> +        ["version"]=""
> +        ["wait-for"]="-i -t")
> +
> +    _init_completion || return
> +
> +    command="none"
> +    for word in "${COMP_WORDS[@]}"; do
> +        if in_array "$word" "${!percommand_opts[@]}"; then
> +            command=$word
> +            break
> +        fi
> +    done
> +
> +    # if no command yet, autocomplete with global options or commands
> +    if [ "$command" == "none" ]; then
> +        COMPREPLY=( $( compgen -W "$global_opts ${!percommand_opts[*]}"
> -- "$cur" ) )
> +        return 0
> +    fi
> +
> +    # otherwise, autocomplete with per-command options
> +    if [[ "$cur" == -* ]]; then
> +        COMPREPLY=( $( compgen -W "${percommand_opts[$command]}" --
> "$cur" ) )
> +        return 0
> +    fi
> +
> +    # if prev is an option, make sure it is allowed for the command
> +    if [[ "$prev" == -* ]]; then
> +        if [[ "${percommand_opts[$command]}" != *"$prev"* ]]; then
> +            return 0
> +        fi
> +    fi
> +
> +    # try to autocomplete each option
> +    case $prev in
> +        '-b')
> +            # FIXTHIS: support multiple boards separated by commas
> +            COMPREPLY=( $( compgen -W "$(ftc list-boards -q)" -- "$cur" ) )
> +            return 0
> +            ;;
> +        '-t')
> +            #FIXTHIS: distinguish test from timeout
> +            COMPREPLY=( $( compgen -W "$(ftc list-tests -q)" -- "$cur" ) )
> +            return 0
> +            ;;
> +        '-p')
> +            COMPREPLY=( $( compgen -W "$(ftc list-plans -q)" -- "$cur" ) )
> +            return 0
> +            ;;
> +        '--timeout')
> +            #FIXTHIS: try to complete
> +            return 0
> +            ;;
> +        '--rebuild' | '--reboot' | '--postcleanup' | '--precleanup')
> +            COMPREPLY=( $( compgen -W "true false" -- "$cur" ) )
> +            return 0
> +            ;;
> +        'put-run')
> +            COMPREPLY=( $( compgen -W "$(ftc list-runs -q)" -- "$cur" ) )
> +            return 0
> +            ;;
> +        'package-test')
> +            COMPREPLY=( $( compgen -W "$(ftc list-tests -q)" -- "$cur" ) )
> +            return 0
> +            ;;
> +        '--format')
> +            COMPREPLY=( $( compgen -W "txt html pdf excel csv rst" -- "$cur" ) )
> +            return 0
> +            ;;
> +        '-s')
> +            local i TEST=""
> +            for (( i=0; i < cword; i++ )); do
> +                if [[ "${words[i]}" == -t ]]; then
> +                TEST="${words[i + 1]}"
> +                fi
> +            done
> +            if [ -z "$TEST" ]; then
> +                return 0
> +            else
> +                COMPREPLY=( $( compgen -W "$(ftc list-specs -t $TEST -q)" -- "$cur"
> ) )
> +            fi
> +            return 0
> +            ;;
> +    esac
> +
> +    # deal with commands that use no opts
> +    if [[ "$command" == "build-jobs" || "$command" == "rm-jobs" ]]; then
> +            COMPREPLY=( $( compgen -W "$(ftc list-jobs -q)" -- "$cur" ) )
> +            return 0
> +    fi
> +
> +    if [[ "$command" == "rm-nodes" ]]; then
> +            COMPREPLY=( $( compgen -W "$(ftc list-nodes -q)" -- "$cur" ) )
> +            return 0
> +    fi
> +
> +    if [[ "$command" == "rm-request" ]]; then
> +            COMPREPLY=( $( compgen -W "$(ftc list-requests -q)" -- "$cur" ) )
> +            return 0
> +    fi
> +
> +    # if we have nothing, show available opts
> +    COMPREPLY=( $( compgen -W "${percommand_opts[$command]}" --
> "$cur" ) )
> +} &&
> +complete -F _ftc ftc
> +
> +# ex: ts=4 sw=4 et filetype=sh
> --
> 2.7.4

Other than the license issue, this looks good to me.  I'm not a bash completion
expert, although I did try to make a simple one for one of my other utilities.
I was hoping the '-q' options on the ftc commands would come in handy.  :-)

Let me know about the license issue.

For now, this is NOT applied.

Thanks,
 -- Tim



More information about the Fuego mailing list