]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/resolvectl
Merge pull request #14488 from yuwata/networkctl-show-logs
[thirdparty/systemd.git] / shell-completion / bash / resolvectl
CommitLineData
bd188ff0
YW
1# resolvectl(1) completion -*- shell-script -*-
2# SPDX-License-Identifier: LGPL-2.1+
3#
4# This file is part of systemd.
5#
bd188ff0
YW
6# systemd is free software; you can redistribute it and/or modify it
7# under the terms of the GNU Lesser General Public License as published by
8# the Free Software Foundation; either version 2.1 of the License, or
9# (at your option) any later version.
10#
11# systemd is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public License
17# along with systemd; If not, see <http://www.gnu.org/licenses/>.
18
19__contains_word () {
843cfcb1
ZJS
20 local w word=$1; shift
21 for w in "$@"; do
22 [[ $w = "$word" ]] && return
23 done
bd188ff0
YW
24}
25
26__get_interfaces(){
843cfcb1
ZJS
27 local name
28 for name in $(cd /sys/class/net && ls); do
29 [[ "$name" != "lo" ]] && echo "$name"
30 done
bd188ff0
YW
31}
32
33_resolvectl() {
843cfcb1
ZJS
34 local i comps verb name
35 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
36 local -A OPTS=(
37 [STANDALONE]='-h --help --version --no-pager -4 -6
38 --service-address=no --service-txt=no
39 --cname=no --search=no --legend=no'
40 [ARG]='-i --interface -p --protocol -t --type -c --class --raw'
41 )
42 local -A VERBS=(
43 [DOMAIN]='query service openpgp'
44 [FAMILY]='tlsa'
45 [STATUS]='status'
46 [LINK]='revert dns domain nta'
47 [RESOLVE]='llmnr mdns'
48 [DNSSEC]='dnssec'
49 [DNSOVERTLS]='dnsovertls'
50 [STANDALONE]='statistics reset-statistics flush-caches reset-server-features'
51 )
52 local -A ARGS=(
53 [FAMILY]='tcp udp sctp'
54 [RESOLVE]='yes no resolve'
55 [DNSSEC]='yes no allow-downgrade'
4310bfc2 56 [DNSOVERTLS]='yes no opportunistic'
843cfcb1
ZJS
57 )
58 local interfaces=$( __get_interfaces )
59
60 if __contains_word "$prev" ${OPTS[ARG]}; then
61 case $prev in
62 --interface|-i)
63 comps="$interfaces"
64 ;;
65 --protocol|-p|--type|-t|--class|-c)
66 comps=$( resolvectl --legend=no "$prev" help; echo help )
67 ;;
68 --raw)
69 comps="payload packet"
70 ;;
71 esac
72 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
73 return 0
74 fi
bd188ff0 75
843cfcb1
ZJS
76 if [[ "$cur" = -* ]]; then
77 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
78 return 0
79 fi
80
81 for ((i=0; i < COMP_CWORD; i++)); do
82 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
83 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
84 verb=${COMP_WORDS[i]}
85 break
bd188ff0 86 fi
843cfcb1 87 done
bd188ff0 88
843cfcb1
ZJS
89 if [[ -z $verb ]]; then
90 comps="${VERBS[*]}"
91
92 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[DOMAIN]}; then
93 comps=''
94
95 elif __contains_word "$verb" ${VERBS[STATUS]}; then
96 comps="$interfaces"
97
98 elif __contains_word "$verb" ${VERBS[FAMILY]}; then
99 for ((i++; i < COMP_CWORD; i++)); do
100 if __contains_word "${COMP_WORDS[i]}" ${ARGS[FAMILY]} &&
101 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
102 name=${COMP_WORDS[i]}
103 break;
104 fi
bd188ff0 105 done
843cfcb1
ZJS
106 if [[ -z $name ]]; then
107 comps=${ARGS[FAMILY]}
108 else
109 comps=""
110 fi
bd188ff0 111
843cfcb1
ZJS
112 elif __contains_word "$verb" ${VERBS[LINK]} ${VERBS[RESOLVE]} ${VERBS[DNSSEC]} ${VERBS[DNSOVERTLS]}; then
113 for ((i++; i < COMP_CWORD; i++)); do
114 if __contains_word "${COMP_WORDS[i]}" $interfaces &&
115 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
116 name=${COMP_WORDS[i]}
117 break;
118 fi
119 done
bd188ff0 120
843cfcb1
ZJS
121 if [[ -z $name ]]; then
122 comps="$interfaces"
bd188ff0 123
843cfcb1
ZJS
124 elif __contains_word "$verb" ${VERBS[RESOLVE]}; then
125 name=
126 for ((i++; i < COMP_CWORD; i++)); do
127 if __contains_word "${COMP_WORDS[i]}" ${ARGS[RESOLVE]} &&
128 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
129 name=${COMP_WORDS[i]}
130 break;
131 fi
132 done
bd188ff0 133
843cfcb1
ZJS
134 if [[ -z $name ]]; then
135 comps=${ARGS[RESOLVE]}
136 else
137 comps=''
138 fi
139
140 elif __contains_word "$verb" ${VERBS[DNSSEC]}; then
141 name=
142 for ((i++; i < COMP_CWORD; i++)); do
143 if __contains_word "${COMP_WORDS[i]}" ${ARGS[DNSSEC]} &&
144 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
145 name=${COMP_WORDS[i]}
146 break;
bd188ff0 147 fi
843cfcb1 148 done
bd188ff0 149
843cfcb1
ZJS
150 if [[ -z $name ]]; then
151 comps=${ARGS[DNSSEC]}
152 else
153 comps=''
154 fi
155
156 elif __contains_word "$verb" ${VERBS[DNSOVERTLS]}; then
157 name=
158 for ((i++; i < COMP_CWORD; i++)); do
159 if __contains_word "${COMP_WORDS[i]}" ${ARGS[DNSOVERTLS]} &&
160 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
161 name=${COMP_WORDS[i]}
162 break;
bd188ff0 163 fi
843cfcb1
ZJS
164 done
165
166 if [[ -z $name ]]; then
167 comps=${ARGS[DNSOVERTLS]}
168 else
169 comps=''
170 fi
171
172 else
173 comps=''
bd188ff0 174 fi
843cfcb1 175 fi
bd188ff0 176
843cfcb1
ZJS
177 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
178 return 0
bd188ff0
YW
179}
180
181complete -F _resolvectl resolvectl