chiark / gitweb /
bash-completion: add completion for resolvectl
[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                [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         )
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
75
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
86                 fi
87         done
88
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
105                 done
106                 if [[ -z $name ]]; then
107                         comps=${ARGS[FAMILY]}
108                 else
109                         comps=""
110                 fi
111
112         elif __contains_word "$verb" ${VERBS[LINK]} ${VERBS[RESOLVE]} ${VERBS[DNSSEC]}; 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
120
121                 if [[ -z $name ]]; then
122                         comps="$interfaces"
123
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
133
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;
147                                 fi
148                         done
149
150                         if [[ -z $name ]]; then
151                                 comps=${ARGS[DNSSEC]}
152                         else
153                                 comps=''
154                         fi
155
156                 else
157                         comps=''
158                 fi
159         fi
160
161         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
162         return 0
163 }
164
165 complete -F _resolvectl resolvectl