chiark / gitweb /
367f9c7c84b259ac0e6e07bb5e9f74dca5f7ae15
[elogind.git] / shell-completion / bash / resolvectl
1 # resolvectl(1) completion                                  -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of elogind.
5 #
6 #
7 # elogind 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 # elogind 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 elogind; If not, see <http://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         { cd /sys/class/net && echo *; } | \
29         while read -d' ' -r name; 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 --no-pager -4 -6
39                              --service-address=no --service-txt=no
40                              --cname=no --search=no --legend=no'
41                       [ARG]='-i --interface -p --protocol -t --type -c --class --raw'
42         )
43         local -A VERBS=(
44                    [DOMAIN]='query service openpgp'
45                    [FAMILY]='tlsa'
46                    [STATUS]='status'
47                      [LINK]='revert dns domain nta'
48                   [RESOLVE]='llmnr mdns'
49                    [DNSSEC]='dnssec'
50                [DNSOVERTLS]='dnsovertls'
51                [STANDALONE]='statistics reset-statistics flush-caches reset-server-features'
52         )
53         local -A ARGS=(
54                    [FAMILY]='tcp udp sctp'
55                   [RESOLVE]='yes no resolve'
56                    [DNSSEC]='yes no allow-downgrade'
57                [DNSOVERTLS]='no opportunistic'
58         )
59         local interfaces=$( __get_interfaces )
60
61         if __contains_word "$prev" ${OPTS[ARG]}; then
62                 case $prev in
63                         --interface|-i)
64                                 comps="$interfaces"
65                                 ;;
66                         --protocol|-p|--type|-t|--class|-c)
67                                 comps=$( resolvectl --legend=no "$prev" help; echo help )
68                                 ;;
69                         --raw)
70                                 comps="payload packet"
71                                 ;;
72                 esac
73                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
74                 return 0
75         fi
76
77         if [[ "$cur" = -* ]]; then
78                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
79                 return 0
80         fi
81
82         for ((i=0; i < COMP_CWORD; i++)); do
83                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
84                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
85                         verb=${COMP_WORDS[i]}
86                         break
87                 fi
88         done
89
90         if [[ -z $verb ]]; then
91                 comps="${VERBS[*]}"
92
93         elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[DOMAIN]}; then
94                 comps=''
95
96         elif __contains_word "$verb" ${VERBS[STATUS]}; then
97                 comps="$interfaces"
98
99         elif __contains_word "$verb" ${VERBS[FAMILY]}; then
100                 for ((i++; i < COMP_CWORD; i++)); do
101                         if __contains_word "${COMP_WORDS[i]}" ${ARGS[FAMILY]} &&
102                          ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
103                                 name=${COMP_WORDS[i]}
104                                 break;
105                         fi
106                 done
107                 if [[ -z $name ]]; then
108                         comps=${ARGS[FAMILY]}
109                 else
110                         comps=""
111                 fi
112
113         elif __contains_word "$verb" ${VERBS[LINK]} ${VERBS[RESOLVE]} ${VERBS[DNSSEC]} ${VERBS[DNSOVERTLS]}; then
114                 for ((i++; i < COMP_CWORD; i++)); do
115                         if __contains_word "${COMP_WORDS[i]}" $interfaces &&
116                          ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
117                                 name=${COMP_WORDS[i]}
118                                 break;
119                         fi
120                 done
121
122                 if [[ -z $name ]]; then
123                         comps="$interfaces"
124
125                 elif __contains_word "$verb" ${VERBS[RESOLVE]}; then
126                         name=
127                         for ((i++; i < COMP_CWORD; i++)); do
128                                 if __contains_word "${COMP_WORDS[i]}" ${ARGS[RESOLVE]} &&
129                                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
130                                         name=${COMP_WORDS[i]}
131                                         break;
132                                 fi
133                         done
134
135                         if [[ -z $name ]]; then
136                                 comps=${ARGS[RESOLVE]}
137                         else
138                                 comps=''
139                         fi
140
141                 elif __contains_word "$verb" ${VERBS[DNSSEC]}; then
142                         name=
143                         for ((i++; i < COMP_CWORD; i++)); do
144                                 if __contains_word "${COMP_WORDS[i]}" ${ARGS[DNSSEC]} &&
145                                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
146                                         name=${COMP_WORDS[i]}
147                                         break;
148                                 fi
149                         done
150
151                         if [[ -z $name ]]; then
152                                 comps=${ARGS[DNSSEC]}
153                         else
154                                 comps=''
155                         fi
156
157                 elif __contains_word "$verb" ${VERBS[DNSOVERTLS]}; then
158                         name=
159                         for ((i++; i < COMP_CWORD; i++)); do
160                                 if __contains_word "${COMP_WORDS[i]}" ${ARGS[DNSOVERTLS]} &&
161                                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
162                                         name=${COMP_WORDS[i]}
163                                         break;
164                                 fi
165                         done
166
167                         if [[ -z $name ]]; then
168                                 comps=${ARGS[DNSOVERTLS]}
169                         else
170                                 comps=''
171                         fi
172
173                 else
174                         comps=''
175                 fi
176         fi
177
178         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
179         return 0
180 }
181
182 complete -F _resolvectl resolvectl