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_exec() { 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 compopt -o filenames compopt -o nospace 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 [[ -f /usr/share/bash-completion/completions/$command ]] && source /usr/share/bash-completion/completions/$command complete -p "$command" 2>/dev/null | 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 compopt -o filenames compopt -o nospace COMPREPLY=($( incus exec "$container" -- bash << EOF source /etc/bash_completion compgen -f "$cur" 2>/dev/null EOF )) fi fi } complete -F _miaou_login "miaou-login" complete -F _miaou_exec "miaou-exec"