You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.6 KiB

function _incus_container {
incus list --format csv --columns n
}
function _active_users {
echo root
miaou-exec "$container" -- awk -F: '$3 >= 1000 && $7 !~ /nologin|false/ {print $1}' /etc/passwd
}
function _miaou_login {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD - 1]}"
local container="${COMP_WORDS[1]}"
case $COMP_CWORD in
1)
# first argument — containers
COMPREPLY=($(compgen -W "$(_incus_container)" -- "$cur"))
;;
2)
# second argument - options
COMPREPLY=($(compgen -W "--user -u" -- "$cur"))
;;
3)
if [[ $prev =~ ^(--user|-u) ]]; then
# user expected
COMPREPLY=($(compgen -W "$(_active_users "$container")" -- "$cur"))
fi
;;
esac
}
function _miaou_exec2() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD - 1]}"
local container="${COMP_WORDS[1]}"
# find '--' position
local dashdash_pos=0
local i
for ((i = 0; i < ${#COMP_WORDS[@]}; i++)); do
[[ ${COMP_WORDS[$i]} == "--" ]] && dashdash_pos=$i
done
# complete container names
if ((COMP_CWORD == 1)); then
# first argument — containers
COMPREPLY=($(compgen -W "$(_incus_container)" -- "$cur"))
return
fi
if ((COMP_CWORD == 2)); then
# second argument - options
COMPREPLY=($(compgen -W "--" -- "$cur"))
return
fi
if ((COMP_CWORD == 3)); then
# command or file from inside container
COMPREPLY=($(
incus exec "$container" -- bash << EOF
source /etc/bash_completion
compgen -c -- "$cur"
compgen -f -- "$cur"
EOF
))
fi
if ((COMP_CWORD > 3)); then
command=${COMP_WORDS[dashdash_pos + 1]}
completion_command=$(
incus exec "$container" -- bash << EOF
source /etc/bash_completion
for i in /etc/bash_completion.d/*; do source \$i; done
[[ -f /usr/share/bash-completion/completions/$command ]] && source /usr/share/bash-completion/completions/$command
complete -p "$command" | grep -oP '(?<=-F )\S+'
EOF
)
if [[ -n $completion_command ]]; then
# Remove first $dashdash_pos items
COMP_WORDS=("${COMP_WORDS[@]:dashdash_pos+1}")
COMP_CWORD=$((COMP_CWORD - dashdash_pos - 1))
COMP_LINE="${COMP_WORDS[@]}"
COMP_POINT=${#COMP_LINE}
COMP_POINT=$((COMP_POINT + 1))
# echo -e "\ncompletion_command $completion_command \nprev=$prev \ncur=$cur \nCOMP_WORDS=(${COMP_WORDS[@]}) \nCOMP_CWORD=$COMP_CWORD \nCOMP_LINE=$COMP_LINE \nCOMP_POINT=$COMP_POINT" >&2
COMPREPLY=($(
incus exec "$container" -- bash << EOF
source /etc/bash_completion
for i in /etc/bash_completion.d/*; do source \$i; done
source /usr/share/bash-completion/bash_completion
[[ -f /usr/share/bash-completion/completions/$command ]] && source /usr/share/bash-completion/completions/$command
COMP_WORDS=(${COMP_WORDS[@]})
COMP_CWORD=$COMP_CWORD
COMP_LINE="$COMP_LINE"
COMP_POINT=$COMP_POINT
COMP_TYPE=9
COMP_KEY=9
export COMP_WORDS COMP_CWORD COMP_LINE COMP_POINT COMP_TYPE COMP_KEY
$completion_command 2>/dev/null
echo \${COMPREPLY[@]}
EOF
))
else
COMPREPLY=($(
incus exec "$container" -- bash << EOF
source /etc/bash_completion
compgen -f -- "$cur"
EOF
))
fi
fi
}
function _miaou_exec {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD - 1]}"
local container="${COMP_WORDS[1]}"
case $COMP_CWORD in
1)
# first argument — containers
COMPREPLY=($(compgen -W "$(_incus_container)" -- "$cur"))
;;
2)
# second argument - options
COMPREPLY=($(compgen -W "--" -- "$cur"))
;;
*)
# echo "coucou" >&2
# COMPREPLY=($(compgen -W \
# "$(incus exec "$container" -- bash -c 'compgen -c' 2> /dev/null)" \
# -- "$cur"))
COMPREPLY=($(compgen -W \
"$(incus exec "$container" -- \
bash -c "compgen -c -- $(printf '%q' "$cur")" 2> /dev/null)" \
-- "$cur"))
;;
esac
}
complete -F _miaou_login "miaou-login"
complete -F _miaou_exec2 "miaou-exec"
function _toto {
for i in ${!COMP_WORDS[@]}; do echo "COMP_WORDS[$i]=${COMP_WORDS[$i]}"; done
echo COMP_CWORD=$COMP_CWORD
COMPREPLY=(one two three)
}
complete -F _toto toto