]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/resolvectl
test: applying timezone is asynchronous
[thirdparty/systemd.git] / shell-completion / bash / resolvectl
1 # shellcheck shell=bash
2 # resolvectl(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 name
29 for name in $(cd /sys/class/net && command ls); do
30 [[ "$name" != "lo" ]] && echo "$name"
31 done
32 }
33
34 _resolvectl() {
35 local i comps verb name
36 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
37 local -A OPTS=(
38 [STANDALONE]='-h --help --version -4 -6 --legend=no --cname=no
39 --validate=no --synthesize=no --cache=no --relax-single-label=no --zone=no
40 --trust-anchor=no --network=no --service-address=no
41 --service-txt=no --search=no --stale-data=no --no-pager'
42 [ARG]='-t --type -c --class -i --interface -p --protocol --raw --json'
43 )
44 local -A VERBS=(
45 [DOMAIN]='query service openpgp'
46 [FAMILY]='tlsa'
47 [STATUS]='status'
48 [LINK]='revert dns domain nta'
49 [BOOLEAN]='default-route'
50 [RESOLVE]='llmnr mdns'
51 [DNSSEC]='dnssec'
52 [DNSOVERTLS]='dnsovertls'
53 [STANDALONE]='statistics reset-statistics flush-caches reset-server-features monitor show-cache show-server-state'
54 [LOG_LEVEL]='log-level'
55 )
56 local -A ARGS=(
57 [FAMILY]='tcp udp sctp'
58 [BOOLEAN]='yes no'
59 [RESOLVE]='yes no resolve'
60 [DNSSEC]='yes no allow-downgrade'
61 [DNSOVERTLS]='yes no opportunistic'
62 )
63
64 if __contains_word "$prev" ${OPTS[ARG]}; then
65 case $prev in
66 --interface|-i)
67 comps=$( __get_interfaces )
68 ;;
69 --protocol|-p|--type|-t|--class|-c|--json)
70 comps=$( resolvectl --legend=no "$prev" help; echo help )
71 ;;
72 --raw)
73 comps="payload packet"
74 ;;
75 esac
76 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
77 return 0
78 fi
79
80 if [[ "$cur" = -* ]]; then
81 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
82 return 0
83 fi
84
85 for ((i=0; i < COMP_CWORD; i++)); do
86 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
87 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
88 verb=${COMP_WORDS[i]}
89 break
90 fi
91 done
92
93 if [[ -z ${verb-} ]]; then
94 comps="${VERBS[*]}"
95
96 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[DOMAIN]}; then
97 comps=''
98
99 elif __contains_word "$verb" ${VERBS[STATUS]}; then
100 comps=$( __get_interfaces )
101
102 elif __contains_word "$verb" ${VERBS[LOG_LEVEL]}; then
103 comps='debug info notice warning err crit alert emerg'
104
105 elif __contains_word "$verb" ${VERBS[FAMILY]}; then
106 for ((i++; i < COMP_CWORD; i++)); do
107 if __contains_word "${COMP_WORDS[i]}" ${ARGS[FAMILY]} &&
108 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
109 name=${COMP_WORDS[i]}
110 break;
111 fi
112 done
113 if [[ -z $name ]]; then
114 comps=${ARGS[FAMILY]}
115 else
116 comps=""
117 fi
118
119 elif __contains_word "$verb" ${VERBS[LINK]} ${VERBS[BOOLEAN]} ${VERBS[RESOLVE]} ${VERBS[DNSSEC]} ${VERBS[DNSOVERTLS]}; then
120 local interfaces=$( __get_interfaces )
121 for ((i++; i < COMP_CWORD; i++)); do
122 if __contains_word "${COMP_WORDS[i]}" $interfaces &&
123 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
124 name=${COMP_WORDS[i]}
125 break;
126 fi
127 done
128
129 if [[ -z $name ]]; then
130 comps="$interfaces"
131
132 elif __contains_word "$verb" ${VERBS[RESOLVE]}; then
133 name=
134 for ((i++; i < COMP_CWORD; i++)); do
135 if __contains_word "${COMP_WORDS[i]}" ${ARGS[RESOLVE]} &&
136 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
137 name=${COMP_WORDS[i]}
138 break;
139 fi
140 done
141
142 if [[ -z $name ]]; then
143 comps=${ARGS[RESOLVE]}
144 else
145 comps=''
146 fi
147
148 elif __contains_word "$verb" ${VERBS[BOOLEAN]}; then
149 name=
150 for ((i++; i < COMP_CWORD; i++)); do
151 if __contains_word "${COMP_WORDS[i]}" ${ARGS[BOOLEAN]} &&
152 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
153 name=${COMP_WORDS[i]}
154 break;
155 fi
156 done
157
158 if [[ -z $name ]]; then
159 comps=${ARGS[BOOLEAN]}
160 else
161 comps=''
162 fi
163
164 elif __contains_word "$verb" ${VERBS[DNSSEC]}; then
165 name=
166 for ((i++; i < COMP_CWORD; i++)); do
167 if __contains_word "${COMP_WORDS[i]}" ${ARGS[DNSSEC]} &&
168 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
169 name=${COMP_WORDS[i]}
170 break;
171 fi
172 done
173
174 if [[ -z $name ]]; then
175 comps=${ARGS[DNSSEC]}
176 else
177 comps=''
178 fi
179
180 elif __contains_word "$verb" ${VERBS[DNSOVERTLS]}; then
181 name=
182 for ((i++; i < COMP_CWORD; i++)); do
183 if __contains_word "${COMP_WORDS[i]}" ${ARGS[DNSOVERTLS]} &&
184 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
185 name=${COMP_WORDS[i]}
186 break;
187 fi
188 done
189
190 if [[ -z $name ]]; then
191 comps=${ARGS[DNSOVERTLS]}
192 else
193 comps=''
194 fi
195
196 else
197 comps=''
198 fi
199 fi
200
201 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
202 return 0
203 }
204
205 complete -F _resolvectl resolvectl