chiark / gitweb /
tmpfiles: fix error message
[elogind.git] / bash-completion / systemd-bash-completion.sh
1 # This file is part of systemd.
2 #
3 # Copyright 2010 Ran Benita
4 #
5 # systemd is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; either version 2.1 of the License, or
8 # (at your option) any later version.
9 #
10 # systemd is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
17
18 __systemctl() {
19         systemctl --full --no-legend "$@"
20 }
21
22 __contains_word () {
23         local word=$1; shift
24         for w in $*; do [[ $w = $word ]] && return 0; done
25         return 1
26 }
27
28 __filter_units_by_property () {
29         local property=$1 value=$2 ; shift 2
30         local units=("$@")
31         local props
32         IFS=$'\n' read -rd '' -a props < \
33             <(__systemctl show --property "$property" -- "${units[@]}")
34         for ((i=0; $i < ${#units[*]}; i++)); do
35                 if [[ "${props[i]}" = "$property=$value" ]]; then
36                         printf "%s\n" "${units[i]}"
37                 fi
38         done
39 }
40
41 __get_all_units      () { __systemctl list-units --all \
42         | { while read -r a b; do printf "%s\n" "$a"; done; }; }
43 __get_active_units   () { __systemctl list-units       \
44         | { while read -r a b; do printf "%s\n" "$a"; done; }; }
45 __get_inactive_units () { __systemctl list-units --all \
46         | { while read -r a b c d; do [[ $c == "inactive" ]] && printf "%s\n" "$a"; done; }; }
47 __get_failed_units   () { __systemctl list-units       \
48         | { while read -r a b c d; do [[ $c == "failed"   ]] && printf "%s\n" "$a"; done; }; }
49 __get_enabled_units  () { __systemctl list-unit-files  \
50         | { while read -r a b c  ; do [[ $b == "enabled"  ]] && printf "%s\n" "$a"; done; }; }
51 __get_disabled_units () { __systemctl list-unit-files  \
52         | { while read -r a b c  ; do [[ $b == "disabled" ]] && printf "%s\n" "$a"; done; }; }
53 __get_masked_units   () { __systemctl list-unit-files  \
54         | { while read -r a b c  ; do [[ $b == "masked"   ]] && printf "%s\n" "$a"; done; }; }
55
56 _systemctl () {
57         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
58         local verb comps
59
60         local -A OPTS=(
61                [STANDALONE]='--all -a --defaults --fail --ignore-dependencies --failed --force -f --full --global
62                              --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
63                              --order --require --quiet -q --privileged -P --system --user --version --runtime'
64                       [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
65         )
66
67         if __contains_word "$prev" ${OPTS[ARG]}; then
68                 case $prev in
69                         --signal|-s)
70                                 comps=$(compgen -A signal)
71                         ;;
72                         --type|-t)
73                                 comps='automount device mount path service snapshot socket swap target timer'
74                         ;;
75                         --kill-who)
76                                 comps='all control main'
77                         ;;
78                         --kill-mode)
79                                 comps='control-group process'
80                         ;;
81                         --root)
82                                 comps=$(compgen -A directory -- "$cur" )
83                                 compopt -o filenames
84                         ;;
85                         --host|-H)
86                                 comps=$(compgen -A hostname)
87                         ;;
88                         --property|-p)
89                                 comps=''
90                         ;;
91                 esac
92                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
93                 return 0
94         fi
95
96
97         if [[ "$cur" = -* ]]; then
98                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
99                 return 0
100         fi
101
102         local -A VERBS=(
103                 [ALL_UNITS]='is-active is-enabled status show mask preset'
104             [ENABLED_UNITS]='disable reenable'
105            [DISABLED_UNITS]='enable'
106              [FAILED_UNITS]='reset-failed'
107           [STARTABLE_UNITS]='start'
108           [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
109          [ISOLATABLE_UNITS]='isolate'
110          [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
111         [RESTARTABLE_UNITS]='restart reload-or-restart'
112              [MASKED_UNITS]='unmask'
113                      [JOBS]='cancel'
114                 [SNAPSHOTS]='delete'
115                      [ENVS]='set-environment unset-environment'
116                [STANDALONE]='daemon-reexec daemon-reload default dot dump
117                              emergency exit halt kexec list-jobs list-units
118                              list-unit-files poweroff reboot rescue show-environment'
119                      [NAME]='snapshot load'
120                      [FILE]='link'
121         )
122
123         for ((i=0; $i <= $COMP_CWORD; i++)); do
124                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
125                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG}]}; then
126                         verb=${COMP_WORDS[i]}
127                         break
128                 fi
129         done
130
131         if   [[ -z $verb ]]; then
132                 comps="${VERBS[*]}"
133
134         elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
135                 comps=$( __get_all_units )
136
137         elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
138                 comps=$( __get_enabled_units )
139
140         elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
141                 comps=$( __get_disabled_units )
142
143         elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
144                 comps=$( __filter_units_by_property CanStart yes \
145                       $( __get_inactive_units \
146                         | while read -r line; do \
147                                 [[ "$line" =~ \.(device|snapshot)$ ]] || printf "%s\n" "$line"; \
148                         done ))
149
150         elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
151                 comps=$( __filter_units_by_property CanStart yes \
152                       $( __get_all_units \
153                         | while read -r line; do \
154                                 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || printf "%s\n" "$line"; \
155                         done ))
156
157         elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
158                 comps=$( __filter_units_by_property CanStop yes \
159                       $( __get_active_units ) )
160
161         elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
162                 comps=$( __filter_units_by_property CanReload yes \
163                       $( __get_active_units ) )
164
165         elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
166                 comps=$( __filter_units_by_property AllowIsolate yes \
167                       $( __get_all_units ) )
168
169         elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
170                 comps=$( __get_failed_units )
171
172         elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
173                 comps=$( __get_masked_units )
174
175         elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
176                 comps=''
177
178         elif __contains_word "$verb" ${VERBS[JOBS]}; then
179                 comps=$( __systemctl list-jobs | { while read -r a b; do printf "%s\n" "$a"; done; } )
180
181         elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
182                 comps=$( __systemctl list-units --type snapshot --full --all \
183                         | { while read -r a b; do printf "%s\n" "$a"; done; } )
184
185         elif __contains_word "$verb" ${VERBS[ENVS]}; then
186                 comps=$( __systemctl show-environment \
187                     | while read -r line; do printf "%s\n" "${line%%=*}=";done )
188                 compopt -o nospace
189
190         elif __contains_word "$verb" ${VERBS[FILE]}; then
191                 comps=$( compgen -A file -- "$cur" )
192                 compopt -o filenames
193         fi
194
195         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
196         return 0
197 }
198 complete -F _systemctl systemctl
199
200 __get_all_sessions () { loginctl list-sessions | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
201 __get_all_users    () { loginctl list-users    | { while read -r a b; do printf "%s\n" "$b"; done; } ; }
202 __get_all_seats    () { loginctl list-seats    | { while read -r a b; do printf "%s\n" "$a"; done; } ; }
203
204 _loginctl () {
205         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
206         local verb comps
207
208         local -A OPTS=(
209                [STANDALONE]='--all -a --help -h --no-pager --privileged -P --version'
210                       [ARG]='--host -H --kill-who --property -p --signal -s'
211         )
212
213         if __contains_word "$prev" ${OPTS[ARG]}; then
214                 case $prev in
215                         --signal|-s)
216                                 comps=$(compgen -A signal)
217                         ;;
218                         --kill-who)
219                                 comps='all leader'
220                         ;;
221                         --host|-H)
222                                 comps=$(compgen -A hostname)
223                         ;;
224                         --property|-p)
225                                 comps=''
226                         ;;
227                 esac
228                 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
229                 return 0
230         fi
231
232
233         if [[ "$cur" = -* ]]; then
234                 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
235                 return 0
236         fi
237
238         local -A VERBS=(
239                 [SESSIONS]='session-status show-session activate lock-session unlock-session terminate-session kill-session'
240                 [USERS]='user-status show-user enable-linger disable-linger terminate-user kill-user'
241                 [SEATS]='seat-status show-seat terminate-seat'
242                 [STANDALONE]='list-sessions list-users list-seats flush-devices'
243                 [ATTACH]='attach'
244         )
245
246         for ((i=0; $i <= $COMP_CWORD; i++)); do
247                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
248                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG}]}; then
249                         verb=${COMP_WORDS[i]}
250                         break
251                 fi
252         done
253
254         if   [[ -z $verb ]]; then
255                 comps="${VERBS[*]}"
256
257         elif __contains_word "$verb" ${VERBS[SESSIONS]}; then
258                 comps=$( __get_all_sessions )
259
260         elif __contains_word "$verb" ${VERBS[USERS]}; then
261                 comps=$( __get_all_users )
262
263         elif __contains_word "$verb" ${VERBS[SEATS]}; then
264                 comps=$( __get_all_seats )
265
266         elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
267                 comps=''
268
269         elif __contains_word "$verb" ${VERBS[ATTACH]}; then
270                 if [[ $prev = $verb ]]; then
271                         comps=$( __get_all_seats )
272                 else
273                         comps=$(compgen -A file -- "$cur" )
274                         compopt -o filenames
275                 fi
276         fi
277
278         COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
279         return 0
280 }
281 complete -F _loginctl loginctl