]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/bash/systemctl.in
bash-completion: process only non-template units for systemctl isolate
[thirdparty/systemd.git] / shell-completion / bash / systemctl.in
1 # systemctl(1) completion -*- shell-script -*-
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # This file is part of systemd.
5 #
6 # Copyright © 2010 Ran Benita
7
8 __systemctl() {
9 local mode=$1; shift 1
10 systemctl $mode --full --no-legend --no-pager "$@" 2>/dev/null
11 }
12
13 __systemd_properties() {
14 @rootlibexecdir@/systemd --dump-bus-properties
15 }
16
17 __contains_word () {
18 local w word=$1; shift
19 for w in "$@"; do
20 [[ $w = "$word" ]] && return
21 done
22 }
23
24 __filter_units_by_properties () {
25 local mode=$1 properties=$2; shift 2
26 local units=("$@")
27 local props i p n
28 local names= count=0
29
30 IFS=$',' read -r -a p < <(echo "Names,$properties")
31 n=${#p[*]}
32 readarray -t props < \
33 <(__systemctl $mode show --property "Names,$properties" -- "${units[@]}")
34
35 for ((i=0; i < ${#props[*]}; i++)); do
36 if [[ -z ${props[i]} ]]; then
37 if (( count == n )) && [[ -n $names ]]; then
38 echo $names
39 fi
40 names=
41 count=0
42 else
43 (( count++ ))
44 if [[ ${props[i]%%=*} == 'Names' ]]; then
45 names=${props[i]#*=}
46 fi
47 fi
48 done
49 if (( count == n )) && [[ -n $names ]]; then
50 echo $names
51 fi
52 }
53
54 __get_all_units () { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
55 | { while read -r a b; do echo " $a"; done; }; }
56 __get_non_template_units() { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
57 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; }
58 __get_template_names () { __systemctl $1 list-unit-files "$2*" \
59 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
60 __get_active_units () { __systemctl $1 list-units "$2*" \
61 | { while read -r a b; do echo " $a"; done; }; }
62
63 __get_not_masked_unit_files() {
64 # filter out masked, not-found, or template units.
65 __systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$2*" | \
66 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
67 }
68
69 __get_startable_units () {
70 __filter_units_by_properties $1 ActiveState=inactive,CanStart=yes $(
71 { __get_not_masked_unit_files $1 $2
72 # get inactive template units
73 __systemctl $1 list-units --state inactive,failed "$2*" | \
74 { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; }
75 } | sort -u )
76 }
77 __get_restartable_units () {
78 # filter out masked and not-found
79 __filter_units_by_properties $1 CanStart=yes $(
80 { __get_not_masked_unit_files $1 $2
81 __get_active_units $1 $2
82 } | sort -u )
83 }
84
85 __get_stoppable_units () {
86 # filter out masked and not-found
87 local units=$(
88 { __get_not_masked_unit_files $1 $2
89 __get_active_units $1 $2
90 } | sort -u )
91 __filter_units_by_properties $1 ActiveState=active,CanStop=yes $units
92 __filter_units_by_properties $1 ActiveState=reloading,CanStop=yes $units
93 __filter_units_by_properties $1 ActiveState=activating,CanStop=yes $units
94 }
95
96 __get_reloadable_units () {
97 # filter out masked and not-found
98 __filter_units_by_properties $1 ActiveState=active,CanReload=yes $(
99 { __get_not_masked_unit_files $1 $2
100 __get_active_units $1 $2
101 } | sort -u )
102 }
103
104 __get_failed_units () { __systemctl $1 list-units "$2*" \
105 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
106 __get_enabled_units () { __systemctl $1 list-unit-files "$2*" \
107 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
108 __get_disabled_units () { __systemctl $1 list-unit-files "$2*" \
109 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
110 __get_masked_units () { __systemctl $1 list-unit-files "$2*" \
111 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
112 __get_all_unit_files () { { __systemctl $1 list-unit-files "$2*"; } | { while read -r a b; do echo " $a"; done; }; }
113
114 __get_machines() {
115 local a b
116 { machinectl list-images --no-legend --no-pager; machinectl list --no-legend --no-pager; } | \
117 { while read a b; do echo " $a"; done; }
118 }
119
120 _systemctl () {
121 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
122 local cur_orig=$cur
123 local i verb comps mode
124
125 local -A OPTS=(
126 [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
127 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall --now
128 --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
129 --show-types -i --ignore-inhibitors --plain --failed --value --fail --dry-run --wait'
130 [ARG]='--host -H --kill-who --property -p --signal -s --type -t --state --job-mode --root
131 --preset-mode -n --lines -o --output -M --machine --message'
132 )
133
134 if __contains_word "--user" ${COMP_WORDS[*]}; then
135 mode=--user
136 elif __contains_word "--global" ${COMP_WORDS[*]}; then
137 mode=--user
138 else
139 mode=--system
140 fi
141
142 if __contains_word "$prev" ${OPTS[ARG]}; then
143 case $prev in
144 --signal|-s)
145 _signals
146 return
147 ;;
148 --type|-t)
149 comps=$(__systemctl $mode -t help)
150 ;;
151 --state)
152 comps=$(__systemctl $mode --state=help)
153 ;;
154 --job-mode)
155 comps='fail replace replace-irreversibly isolate
156 ignore-dependencies ignore-requirements flush'
157 ;;
158 --kill-who)
159 comps='all control main'
160 ;;
161 --root)
162 comps=$(compgen -A directory -- "$cur" )
163 compopt -o filenames
164 ;;
165 --host|-H)
166 comps=$(compgen -A hostname)
167 ;;
168 --property|-p)
169 comps=$(__systemd_properties)
170 ;;
171 --preset-mode)
172 comps='full enable-only disable-only'
173 ;;
174 --output|-o)
175 comps=$( systemctl --output=help 2>/dev/null )
176 ;;
177 --machine|-M)
178 comps=$( __get_machines )
179 ;;
180 esac
181 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
182 return 0
183 fi
184
185 if [[ "$cur" = -* ]]; then
186 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
187 return 0
188 fi
189
190 local -A VERBS=(
191 [ALL_UNITS]='cat mask'
192 [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert'
193 [ENABLED_UNITS]='disable'
194 [DISABLED_UNITS]='enable'
195 [REENABLABLE_UNITS]='reenable'
196 [FAILED_UNITS]='reset-failed'
197 [STARTABLE_UNITS]='start'
198 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
199 [ISOLATABLE_UNITS]='isolate'
200 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
201 [RESTARTABLE_UNITS]='restart reload-or-restart'
202 [TARGET_AND_UNITS]='add-wants add-requires'
203 [MASKED_UNITS]='unmask'
204 [JOBS]='cancel'
205 [ENVS]='set-environment unset-environment import-environment'
206 [STANDALONE]='daemon-reexec daemon-reload default
207 emergency exit halt hibernate hybrid-sleep
208 suspend-then-hibernate kexec list-jobs list-sockets
209 list-timers list-units list-unit-files poweroff
210 reboot rescue show-environment suspend get-default
211 is-system-running preset-all'
212 [FILE]='link switch-root'
213 [TARGETS]='set-default'
214 [MACHINES]='list-machines'
215 )
216
217 for ((i=0; i < COMP_CWORD; i++)); do
218 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
219 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
220 verb=${COMP_WORDS[i]}
221 break
222 fi
223 done
224
225 # When trying to match a unit name with certain special characters in its name (i.e
226 # foo\x2dbar:01) they get escaped by bash along the way, thus causing any possible
227 # match to fail. Let's unescape such characters in the verb we're trying to
228 # autocomplete to avoid this, however, use the original verb (cur_orig)
229 # during the final match (COMPREPLY)
230 cur="$(echo $cur | xargs echo)"
231
232 if [[ -z $verb ]]; then
233 comps="${VERBS[*]}"
234
235 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
236 comps=$( __get_all_units $mode "$cur" )
237 compopt -o filenames
238
239 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
240 comps=$( __get_non_template_units $mode "$cur" )
241 compopt -o filenames
242
243 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
244 comps=$( __get_enabled_units $mode "$cur" )
245 compopt -o filenames
246
247 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
248 comps=$( __get_disabled_units $mode "$cur";
249 __get_template_names $mode "$cur")
250 compopt -o filenames
251
252 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
253 comps=$( __get_disabled_units $mode "$cur";
254 __get_enabled_units $mode "$cur";
255 __get_template_names $mode "$cur")
256 compopt -o filenames
257
258 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
259 comps=$( __get_startable_units $mode "$cur" )
260 compopt -o filenames
261
262 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
263 comps=$( __get_restartable_units $mode "$cur" )
264 compopt -o filenames
265
266 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
267 comps=$( __get_stoppable_units $mode "$cur" )
268 compopt -o filenames
269
270 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
271 comps=$( __get_reloadable_units $mode "$cur" )
272 compopt -o filenames
273
274 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
275 comps=$( __filter_units_by_properties $mode AllowIsolate=yes \
276 $( __get_non_template_units $mode "$cur" ) )
277 compopt -o filenames
278
279 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
280 comps=$( __get_failed_units $mode "$cur" )
281 compopt -o filenames
282
283 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
284 comps=$( __get_masked_units $mode "$cur" )
285 compopt -o filenames
286
287 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
288 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
289 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
290 comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" \
291 | { while read -r a b; do echo " $a"; done; } )
292 else
293 comps=$( __get_all_unit_files $mode "$cur" )
294 fi
295 compopt -o filenames
296
297 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
298 comps=''
299
300 elif __contains_word "$verb" ${VERBS[JOBS]}; then
301 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
302
303 elif __contains_word "$verb" ${VERBS[ENVS]}; then
304 comps=$( __systemctl $mode show-environment \
305 | while read -r line; do echo " ${line%%=*}="; done )
306 compopt -o nospace
307
308 elif __contains_word "$verb" ${VERBS[FILE]}; then
309 comps=$( compgen -A file -- "$cur" )
310 compopt -o filenames
311
312 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
313 comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \
314 | { while read -r a b; do echo " $a"; done; } )
315 fi
316
317 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
318 return 0
319 }
320
321 complete -F _systemctl systemctl