]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/bash/systemctl
systemctl: show reverse dependencies or before/after ordering
[thirdparty/systemd.git] / shell-completion / bash / systemctl
CommitLineData
d611dadc
MB
1# systemctl(1) completion -*- shell-script -*-
2#
3# This file is part of systemd.
4#
5# Copyright 2010 Ran Benita
6#
7# systemd 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# systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
19
20__systemctl() {
9e542e0b
ZJS
21 local mode=$1; shift 1
22 systemctl $mode --full --no-legend "$@"
d611dadc
MB
23}
24
caffaf58
ZJS
25__systemd_properties() {
26 local mode=$1
90cf049b 27 { __systemctl -a $mode show;
caffaf58
ZJS
28 systemd --dump-configuration-items; } |
29 while IFS='=' read -r key value; do
30 [[ $value ]] && echo "$key"
31 done
32}
33
d611dadc
MB
34__contains_word () {
35 local word=$1; shift
36 for w in $*; do [[ $w = $word ]] && return 0; done
37 return 1
38}
39
40__filter_units_by_property () {
9e542e0b 41 local mode=$1 property=$2 value=$3 ; shift 3
d611dadc
MB
42 local units=("$@")
43 local props
44 IFS=$'\n' read -rd '' -a props < \
9e542e0b 45 <(__systemctl $mode show --property "$property" -- "${units[@]}")
d611dadc
MB
46 for ((i=0; $i < ${#units[*]}; i++)); do
47 if [[ "${props[i]}" = "$property=$value" ]]; then
79c16383 48 echo " ${units[i]}"
d611dadc
MB
49 fi
50 done
51}
52
9e542e0b 53__get_all_units () { __systemctl $1 list-units --all \
79c16383 54 | { while read -r a b; do echo " $a"; done; }; }
9e542e0b 55__get_active_units () { __systemctl $1 list-units \
79c16383 56 | { while read -r a b; do echo " $a"; done; }; }
20b3f379 57__get_startable_units () { __systemctl $1 list-units --all -t service,timer,socket,mount,automount,path,snapshot,swap \
c2e09812 58 | { while read -r a b c d; do [[ $c == "inactive" || $c == "failed " ]] && echo " $a"; done; }; }
9e542e0b 59__get_failed_units () { __systemctl $1 list-units \
79c16383 60 | { while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }; }
9e542e0b 61__get_enabled_units () { __systemctl $1 list-unit-files \
79c16383 62 | { while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }; }
9e542e0b 63__get_disabled_units () { __systemctl $1 list-unit-files \
79c16383 64 | { while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
9e542e0b 65__get_masked_units () { __systemctl $1 list-unit-files \
79c16383 66 | { while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }; }
d611dadc
MB
67
68_systemctl () {
69 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
9e542e0b 70 local i verb comps mode
d611dadc
MB
71
72 local -A OPTS=(
afba4199 73 [STANDALONE]='--all -a --reverse --after --before --defaults --fail --ignore-dependencies --failed --force -f --full --global
d611dadc
MB
74 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
75 --quiet -q --privileged -P --system --user --version --runtime'
76 [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
77 )
78
caffaf58
ZJS
79 if __contains_word "--user" ${COMP_WORDS[*]}; then
80 mode=--user
81 else
82 mode=--system
83 fi
84
d611dadc
MB
85 if __contains_word "$prev" ${OPTS[ARG]}; then
86 case $prev in
87 --signal|-s)
88 comps=$(compgen -A signal)
89 ;;
90 --type|-t)
91 comps='automount device mount path service snapshot socket swap target timer'
92 ;;
93 --kill-who)
94 comps='all control main'
95 ;;
96 --kill-mode)
97 comps='control-group process'
98 ;;
99 --root)
100 comps=$(compgen -A directory -- "$cur" )
101 compopt -o filenames
102 ;;
103 --host|-H)
104 comps=$(compgen -A hostname)
105 ;;
106 --property|-p)
caffaf58 107 comps=$(__systemd_properties $mode)
d611dadc
MB
108 ;;
109 esac
110 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
111 return 0
112 fi
113
d611dadc
MB
114 if [[ "$cur" = -* ]]; then
115 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
116 return 0
117 fi
118
119 local -A VERBS=(
afba4199 120 [ALL_UNITS]='is-active is-failed is-enabled status show mask preset help list-dependencies'
c2e09812 121 [ENABLED_UNITS]='disable'
d611dadc 122 [DISABLED_UNITS]='enable'
c2e09812 123 [REENABLABLE_UNITS]='reenable'
d611dadc
MB
124 [FAILED_UNITS]='reset-failed'
125 [STARTABLE_UNITS]='start'
126 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
127 [ISOLATABLE_UNITS]='isolate'
128 [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
129 [RESTARTABLE_UNITS]='restart reload-or-restart'
130 [MASKED_UNITS]='unmask'
131 [JOBS]='cancel'
132 [SNAPSHOTS]='delete'
133 [ENVS]='set-environment unset-environment'
134 [STANDALONE]='daemon-reexec daemon-reload default dump
135 emergency exit halt hibernate hybrid-sleep kexec list-jobs
136 list-units list-unit-files poweroff reboot rescue
137 show-environment suspend'
138 [NAME]='snapshot load'
139 [FILE]='link'
140 )
141
142 for ((i=0; $i <= $COMP_CWORD; i++)); do
143 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
144 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
145 verb=${COMP_WORDS[i]}
146 break
147 fi
148 done
149
150 if [[ -z $verb ]]; then
151 comps="${VERBS[*]}"
152
153 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
9e542e0b 154 comps=$( __get_all_units $mode )
d611dadc
MB
155
156 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
9e542e0b 157 comps=$( __get_enabled_units $mode )
d611dadc
MB
158
159 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
9e542e0b 160 comps=$( __get_disabled_units $mode )
d611dadc 161
c2e09812
ZJS
162 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
163 comps=$( __get_disabled_units $mode;
164 __get_enabled_units $mode )
165
d611dadc 166 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
9e542e0b 167 comps=$( __filter_units_by_property $mode CanStart yes \
20b3f379 168 $( __get_startable_units $mode))
d611dadc
MB
169
170 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
9e542e0b
ZJS
171 comps=$( __filter_units_by_property $mode CanStart yes \
172 $( __get_all_units $mode \
d611dadc 173 | while read -r line; do \
79c16383 174 [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo " $line"; \
d611dadc
MB
175 done ))
176
177 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
9e542e0b
ZJS
178 comps=$( __filter_units_by_property $mode CanStop yes \
179 $( __get_active_units $mode ) )
d611dadc
MB
180
181 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
9e542e0b
ZJS
182 comps=$( __filter_units_by_property $mode CanReload yes \
183 $( __get_active_units $mode ) )
d611dadc
MB
184
185 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
9e542e0b
ZJS
186 comps=$( __filter_units_by_property $mode AllowIsolate yes \
187 $( __get_all_units $mode ) )
d611dadc
MB
188
189 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
9e542e0b 190 comps=$( __get_failed_units $mode )
d611dadc
MB
191
192 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
9e542e0b 193 comps=$( __get_masked_units $mode )
d611dadc
MB
194
195 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
196 comps=''
197
198 elif __contains_word "$verb" ${VERBS[JOBS]}; then
79c16383 199 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
d611dadc
MB
200
201 elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
9e542e0b 202 comps=$( __systemctl $mode list-units --type snapshot --full --all \
79c16383 203 | { while read -r a b; do echo " $a"; done; } )
d611dadc
MB
204
205 elif __contains_word "$verb" ${VERBS[ENVS]}; then
9e542e0b 206 comps=$( __systemctl $mode show-environment \
79c16383 207 | while read -r line; do echo " ${line%%=*}=";done )
d611dadc
MB
208 compopt -o nospace
209
210 elif __contains_word "$verb" ${VERBS[FILE]}; then
211 comps=$( compgen -A file -- "$cur" )
212 compopt -o filenames
213 fi
214
215 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
216 return 0
217}
218
219complete -F _systemctl systemctl