__lium_arg_used() { local i=0
while [ $i -lt $COMP_CWORD ]; do
test x"${COMP_WORDS[i]}" = x"$1" && return 0
i=$((i+1))
done
return 1
}
_lium_arg_used() { while [ "$#" -ne 0 ]; do
__lium_arg_used $1 && return 0
shift 1
done
return 1
}
_lium_arg_included() { local cur=$1
shift 1
while [ "$#" -ne 0 ]; do
test x"${cur}" = x"$1" && return 0
shift 1
done
return 1
}
_lium_current_repo() {
local i=0
while [ $i -lt $COMP_CWORD ]; do
if [ "${COMP_WORDS[i]}" = "--repo" ]; then
i=$((i+1))
echo "${COMP_WORDS[i]}"
return
fi
i=$((i+1))
done
if [ -d $PWD/.repo -a -d $PWD/chroot ]; then
echo $PWD
fi
}
_lium_get_duts() {
${COMP_WORDS[0]} dut list --ids 2>/dev/null
}
_lium_get_tests() {
${COMP_WORDS[0]} tast list --cached 2>/dev/null | cut -f 1 -d,
}
_lium_get_servos() {
${COMP_WORDS[0]} servo list --serials 2>/dev/null | cut -f 1
}
_lium_get_packages() {
${COMP_WORDS[0]} packages list --cached 2>/dev/null | cut -f 1
}
_lium_get_dut_actions() {
lium dut do --list-actions 2>/dev/null
}
_lium_comp_fs() { local DIR=`compgen ${1} ${2}`
if [ `echo ${DIR} | wc -w` = 1 -a -d "${DIR}" ]; then
compgen ${1} "${DIR}/"
else
compgen ${1} ${2}
fi
}
_lium_current_command() {
local i=0
while [ $i -lt $COMP_CWORD ]; do
case "${COMP_WORDS[i]}" in
-*) i=$COMP_CWORD;;
*)
echo "${COMP_WORDS[i]} ";;
esac
i=$((i+1))
done
}
_lium_get_options() { local cmd=`_lium_current_command`
local otype=0
local a b
${cmd} --help 2>/dev/null | while read a b ;do
case ${a} in
Positional) otype=1;;
Options:) otype=2;;
Commands:) otype=3;;
-*)
if [ ${otype} -eq 2 ]; then
_lium_arg_used ${a} || echo ${a}
fi;;
*)
if [ ${otype} -eq 1 ]; then
case "${a}" in
dut|duts)
_lium_get_duts;;
actions)
_lium_get_dut_actions;;
tests)
_lium_get_tests;;
packages)
_lium_get_packages;;
files)
if [ "${1#-}" == "${1}" ]; then
_lium_comp_fs -f ${1}
fi;;
esac
elif [ ${otype} = 3 ]; then
echo ${a}
fi
;;
esac
done
}
_lium() { local cur=$2
local prev=$3
local dir_opts="--repo --dir --dest"
local file_opts="--image"
local todo_opts="--version --board --workon"
local servo_serial_opts="--serial --servo"
COMPREPLY=
if _lium_arg_used "--help"; then
return 0
fi
if _lium_arg_included ${prev} ${todo_opts}; then
return 0
elif [ x"$prev" = x"--dut" ]; then
local DUTS=`_lium_get_duts`
COMPREPLY=($(compgen -W "${DUTS}" -- $cur))
elif _lium_arg_included ${prev} ${servo_serial_opts}; then
local DUTS=`_lium_get_servos`
COMPREPLY=($(compgen -W "${DUTS}" -- $cur))
elif [ x"$prev" = x"--remove" -a "${COMP_WORDS[1]}" = "dut" -a "${COMP_WORDS[2]}" = "list" ]; then
local DUTS=`_lium_get_duts`
COMPREPLY=($(compgen -W "${DUTS}" -- $cur))
elif _lium_arg_included ${prev} ${dir_opts}; then
COMPREPLY=($(_lium_comp_fs -d ${cur}))
elif _lium_arg_included ${prev} ${file_opts}; then
COMPREPLY=($(_lium_comp_fs -f ${cur}))
else
local OPTS=`_lium_get_options ${cur}`
COMPREPLY=($(compgen -W "${OPTS}" -- ${cur}))
fi
}
complete -F _lium lium