| 1 |
# shellcheck shell=bash |
| 2 |
# varlinkctl(1) completion -*- shell-script -*- |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
# |
| 5 |
# This file is part of systemd. |
| 6 |
# |
| 7 |
# systemd is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation; either version 2.1 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# systemd is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
# General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with systemd; If not, see <https://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
__contains_word () { |
| 21 |
local w word=$1; shift |
| 22 |
for w in "$@"; do |
| 23 |
[[ $w = "$word" ]] && return |
| 24 |
done |
| 25 |
} |
| 26 |
|
| 27 |
__get_interfaces() { |
| 28 |
local address=$1 |
| 29 |
local a |
| 30 |
varlinkctl list-interfaces --no-pager "$address" 2>/dev/null | |
| 31 |
{ while read -r a; do echo " $a"; done; } |
| 32 |
} |
| 33 |
|
| 34 |
__get_methods() { |
| 35 |
local address=$1 |
| 36 |
local a |
| 37 |
varlinkctl list-methods --no-pager "$address" 2>/dev/null | |
| 38 |
{ while read -r a; do echo " $a"; done; } |
| 39 |
} |
| 40 |
|
| 41 |
_varlinkctl() { |
| 42 |
local i n verb comps |
| 43 |
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} |
| 44 |
local -A OPTS=( |
| 45 |
[STANDALONE]='-h --help --version --no-pager -q --quiet --no-ask-password |
| 46 |
--oneway --collect --more --exec --upgrade -j -E' |
| 47 |
[ARG]='--graceful --timeout --push-fd --json' |
| 48 |
) |
| 49 |
|
| 50 |
if __contains_word "$prev" ${OPTS[ARG]}; then |
| 51 |
case $prev in |
| 52 |
--json) |
| 53 |
comps=$( varlinkctl --json=help 2>/dev/null ) |
| 54 |
;; |
| 55 |
esac |
| 56 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 57 |
return 0 |
| 58 |
fi |
| 59 |
|
| 60 |
if [[ "$cur" = -* ]]; then |
| 61 |
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") ) |
| 62 |
return 0 |
| 63 |
fi |
| 64 |
|
| 65 |
local -A VERBS=( |
| 66 |
[STANDALONE]='help list-registry' |
| 67 |
[CALL]='call' |
| 68 |
[FILE]='info list-interfaces validate-idl' |
| 69 |
[ADDRESS_INTERFACES]='list-methods introspect' |
| 70 |
) |
| 71 |
|
| 72 |
for ((i=0; i < COMP_CWORD; i++)); do |
| 73 |
if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} && |
| 74 |
! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then |
| 75 |
verb=${COMP_WORDS[i]} |
| 76 |
break |
| 77 |
fi |
| 78 |
done |
| 79 |
|
| 80 |
n=$((COMP_CWORD - i)) |
| 81 |
|
| 82 |
if [[ -z ${verb-} ]]; then |
| 83 |
comps=${VERBS[*]} |
| 84 |
elif __contains_word "$verb" ${VERBS[STANDALONE]}; then |
| 85 |
comps='' |
| 86 |
elif __contains_word "$verb" ${VERBS[FILE]}; then |
| 87 |
comps=$(compgen -f -- "$cur") |
| 88 |
compopt -o filenames |
| 89 |
elif __contains_word "$verb" ${VERBS[ADDRESS_INTERFACES]}; then |
| 90 |
if [[ $n -eq 1 ]] ; then |
| 91 |
comps=$(compgen -f -- "$cur") |
| 92 |
compopt -o filenames |
| 93 |
elif [[ $n -eq 2 ]] ; then |
| 94 |
comps=$( __get_interfaces ${COMP_WORDS[COMP_CWORD-1]}) |
| 95 |
else |
| 96 |
comps='' |
| 97 |
fi |
| 98 |
elif __contains_word "$verb" ${VERBS[CALL]}; then |
| 99 |
if [[ $n -eq 1 ]] ; then |
| 100 |
comps=$(compgen -f -- "$cur") |
| 101 |
compopt -o filenames |
| 102 |
elif [[ $n -eq 2 ]] ; then |
| 103 |
comps=$( __get_methods ${COMP_WORDS[COMP_CWORD-1]}) |
| 104 |
elif [[ $n -eq 3 ]] ; then |
| 105 |
comps="'{}'" |
| 106 |
elif [[ ${COMP_WORDS[COMP_CWORD-1]} == "--" ]] && __contains_word "--exec" ${COMP_WORDS[*]} ; then |
| 107 |
comps=$(compgen -c -- "$cur") |
| 108 |
else |
| 109 |
comps='' |
| 110 |
fi |
| 111 |
fi |
| 112 |
|
| 113 |
COMPREPLY=( $(compgen -W '$comps' -- "$cur") ) |
| 114 |
return 0 |
| 115 |
} |
| 116 |
|
| 117 |
complete -F _varlinkctl varlinkctl |