]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/systemd-bash-completion.sh
virt: detect LXC+libvirt containers
[thirdparty/systemd.git] / src / systemd-bash-completion.sh
CommitLineData
42bb3074
RB
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 General Public License as published by
7# the Free Software Foundation; either version 2 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 General Public License
16# along with systemd; If not, see <http://www.gnu.org/licenses/>.
17
74eeab04 18__systemctl() {
8aea83c7 19 systemctl --full --no-legend "$@"
74eeab04
DR
20}
21
42bb3074
RB
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 ; shift
30 local -a units=( $* )
74eeab04 31 local -a props=( $(__systemctl show --property "$property" -- ${units[*]} | grep -v ^$) )
42bb3074
RB
32 for ((i=0; $i < ${#units[*]}; i++)); do
33 if [[ "${props[i]}" = "$property=$value" ]]; then
34 echo "${units[i]}"
35 fi
36 done
37}
38
8aea83c7
RB
39__get_all_units () { __systemctl list-units --all | awk ' {print $1}' ; }
40__get_active_units () { __systemctl list-units | awk ' {print $1}' ; }
41__get_inactive_units () { __systemctl list-units --all | awk '$3 == "inactive" {print $1}' ; }
42__get_failed_units () { __systemctl list-units | awk '$3 == "failed" {print $1}' ; }
43__get_enabled_units () { __systemctl list-unit-files | awk '$2 == "enabled" {print $1}' ; }
44__get_disabled_units () { __systemctl list-unit-files | awk '$2 == "disabled" {print $1}' ; }
45__get_masked_units () { __systemctl list-unit-files | awk '$2 == "masked" {print $1}' ; }
42bb3074
RB
46
47_systemctl () {
48 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
49 local verb comps
50
51 local -A OPTS=(
e67c3609 52 [STANDALONE]='--all -a --defaults --fail --ignore-dependencies --failed --force -f --full --global
8aea83c7
RB
53 --help -h --no-ask-password --no-block --no-legend --no-pager --no-reload --no-wall
54 --order --require --quiet -q --privileged -P --system --user --version --runtime'
55 [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t --root'
42bb3074
RB
56 )
57
58 if __contains_word "$prev" ${OPTS[ARG]}; then
59 case $prev in
60 --signal|-s)
8aea83c7 61 comps=$(compgen -A signal)
42bb3074
RB
62 ;;
63 --type|-t)
64 comps='automount device mount path service snapshot socket swap target timer'
65 ;;
66 --kill-who)
67 comps='all control main'
68 ;;
69 --kill-mode)
cd25cce9 70 comps='control-group process'
42bb3074 71 ;;
8aea83c7
RB
72 --root)
73 comps=$(compgen -A directory -- "$cur" )
74 compopt -o filenames
75 ;;
76 --host|-H)
77 comps=$(compgen -A hostname)
78 ;;
79 --property|-p)
42bb3074
RB
80 comps=''
81 ;;
82 esac
83 COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
84 return 0
85 fi
86
87
88 if [[ "$cur" = -* ]]; then
89 COMPREPLY=( $(compgen -W "${OPTS[*]}" -- "$cur") )
90 return 0
91 fi
92
93 local -A VERBS=(
8aea83c7
RB
94 [ALL_UNITS]='is-active is-enabled status show mask preset'
95 [ENABLED_UNITS]='disable reenable'
96 [DISABLED_UNITS]='enable'
42bb3074 97 [FAILED_UNITS]='reset-failed'
54a43705 98 [STARTABLE_UNITS]='start'
8aea83c7 99 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
2dcd4d24 100 [ISOLATABLE_UNITS]='isolate'
8aea83c7
RB
101 [RELOADABLE_UNITS]='reload condreload reload-or-try-restart force-reload'
102 [RESTARTABLE_UNITS]='restart reload-or-restart'
103 [MASKED_UNITS]='unmask'
42bb3074
RB
104 [JOBS]='cancel'
105 [SNAPSHOTS]='delete'
106 [ENVS]='set-environment unset-environment'
8aea83c7
RB
107 [STANDALONE]='daemon-reexec daemon-reload default dot dump
108 emergency exit halt kexec list-jobs list-units
109 list-unit-files poweroff reboot rescue show-environment'
42bb3074 110 [NAME]='snapshot load'
8aea83c7 111 [FILE]='link'
42bb3074
RB
112 )
113
42bb3074
RB
114 for ((i=0; $i <= $COMP_CWORD; i++)); do
115 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
116 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG}]}; then
117 verb=${COMP_WORDS[i]}
118 break
119 fi
120 done
121
122 if [[ -z $verb ]]; then
123 comps="${VERBS[*]}"
124
125 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
126 comps=$( __get_all_units )
127
8aea83c7
RB
128 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
129 comps=$( __get_enabled_units )
130
131 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
132 comps=$( __get_disabled_units )
133
42bb3074
RB
134 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
135 comps=$( __filter_units_by_property CanStart yes \
136 $( __get_inactive_units | grep -Ev '\.(device|snapshot)$' ))
137
54a43705
FC
138 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
139 comps=$( __filter_units_by_property CanStart yes \
140 $( __get_all_units | grep -Ev '\.(device|snapshot|socket|timer)$' ))
141
42bb3074
RB
142 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
143 comps=$( __filter_units_by_property CanStop yes \
144 $( __get_active_units ) )
145
146 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
147 comps=$( __filter_units_by_property CanReload yes \
148 $( __get_active_units ) )
149
150 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
151 comps=$( __filter_units_by_property AllowIsolate yes \
152 $( __get_all_units ) )
153
154 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
155 comps=$( __get_failed_units )
156
8aea83c7
RB
157 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
158 comps=$( __get_masked_units )
159
42bb3074
RB
160 elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
161 comps=''
162
163 elif __contains_word "$verb" ${VERBS[JOBS]}; then
74eeab04 164 comps=$( __systemctl list-jobs | awk '{print $1}' )
42bb3074
RB
165
166 elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
74eeab04 167 comps=$( __systemctl list-units --type snapshot --full --all | awk '{print $1}' )
42bb3074
RB
168
169 elif __contains_word "$verb" ${VERBS[ENVS]}; then
74eeab04 170 comps=$( __systemctl show-environment | sed 's_\([^=]\+=\).*_\1_' )
42bb3074 171 compopt -o nospace
8aea83c7
RB
172
173 elif __contains_word "$verb" ${VERBS[FILE]}; then
174 comps=$( compgen -A file -- "$cur" )
175 compopt -o filenames
42bb3074
RB
176 fi
177
178 COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
179 return 0
180}
181complete -F _systemctl systemctl
3cdbf916
RB
182
183__get_all_sessions () { systemd-loginctl list-sessions | awk '{print $1}' ; }
184__get_all_users () { systemd-loginctl list-users | awk '{print $2}' ; }
185__get_all_seats () { systemd-loginctl list-seats | awk '{print $1}' ; }
186
187_systemd_loginctl () {
188 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
189 local verb comps
190
191 local -A OPTS=(
192 [STANDALONE]='--all -a --help -h --no-pager --privileged -P --version'
193 [ARG]='--host -H --kill-who --property -p --signal -s'
194 )
195
196 if __contains_word "$prev" ${OPTS[ARG]}; then
197 case $prev in
198 --signal|-s)
199 comps=$(compgen -A signal)
200 ;;
201 --kill-who)
202 comps='all leader'
203 ;;
204 --host|-H)
205 comps=$(compgen -A hostname)
206 ;;
207 --property|-p)
208 comps=''
209 ;;
210 esac
211 COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
212 return 0
213 fi
214
215
216 if [[ "$cur" = -* ]]; then
217 COMPREPLY=( $(compgen -W "${OPTS[*]}" -- "$cur") )
218 return 0
219 fi
220
221 local -A VERBS=(
222 [SESSIONS]='session-status show-session activate lock-session unlock-session terminate-session kill-session'
223 [USERS]='user-status show-user enable-linger disable-linger terminate-user kill-user'
224 [SEATS]='seat-status show-seat terminate-seat'
225 [STANDALONE]='list-sessions list-users list-seats flush-devices'
226 [ATTACH]='attach'
227 )
228
229 for ((i=0; $i <= $COMP_CWORD; i++)); do
230 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
231 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG}]}; then
232 verb=${COMP_WORDS[i]}
233 break
234 fi
235 done
236
237 if [[ -z $verb ]]; then
238 comps="${VERBS[*]}"
239
240 elif __contains_word "$verb" ${VERBS[SESSIONS]}; then
241 comps=$( __get_all_sessions )
242
243 elif __contains_word "$verb" ${VERBS[USERS]}; then
244 comps=$( __get_all_users )
245
246 elif __contains_word "$verb" ${VERBS[SEATS]}; then
247 comps=$( __get_all_seats )
248
249 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
250 comps=''
251
252 elif __contains_word "$verb" ${VERBS[ATTACH]}; then
253 if [[ $prev = $verb ]]; then
254 comps=$( __get_all_seats )
255 else
256 comps=$(compgen -A file -- "$cur" )
257 compopt -o filenames
258 fi
259 fi
260
261 COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
262 return 0
263}
264complete -F _systemd_loginctl systemd-loginctl