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