]> git.ipfire.org Git - thirdparty/systemd.git/blob - shell-completion/zsh/_systemctl.in
17736de01c66998e08e27aa40dce632d82eb55f2
[thirdparty/systemd.git] / shell-completion / zsh / _systemctl.in
1 #compdef systemctl
2
3 (( $+functions[_systemctl_command] )) || _systemctl_command()
4 {
5 local -a _systemctl_cmds
6 _systemctl_cmds=(
7 "list-sockets:List sockets"
8 "list-timers:List timers"
9 "list-units:List units"
10 "start:Start (activate) one or more units"
11 "stop:Stop (deactivate) one or more units"
12 "reload:Reload one or more units"
13 "restart:Start or restart one or more units"
14 "condrestart:Restart one or more units if active"
15 "try-restart:Restart one or more units if active"
16 "reload-or-restart:Reload one or more units if possible, otherwise start or restart"
17 "force-reload:Reload one or more units if possible, otherwise restart if active"
18 "hibernate:Hibernate the system"
19 "hybrid-sleep:Hibernate and suspend the system"
20 "reload-or-try-restart:Reload one or more units if possible, otherwise restart if active"
21 "isolate:Start one unit and stop all others"
22 "kill:Send signal to processes of a unit"
23 "is-active:Check whether units are active"
24 "is-failed:Check whether units are failed"
25 "status:Show runtime status of one or more units"
26 "show:Show properties of one or more units/jobs or the manager"
27 "cat:Show the source unit files and drop-ins"
28 "reset-failed:Reset failed state for all, one, or more units"
29 "list-unit-files:List installed unit files"
30 "enable:Enable one or more unit files"
31 "disable:Disable one or more unit files"
32 "reenable:Reenable one or more unit files"
33 "preset:Enable/disable one or more unit files based on preset configuration"
34 "set-default:Set the default target"
35 "get-default:Query the default target"
36 "edit:Edit one or more unit files"
37 "is-system-running:Query overall status of the system"
38 "help:Show documentation for specified units"
39 "list-dependencies:Show unit dependency tree"
40 "mask:Mask one or more units"
41 "unmask:Unmask one or more units"
42 "link:Link one or more units files into the search path"
43 "is-enabled:Check whether unit files are enabled"
44 "list-jobs:List jobs"
45 "cancel:Cancel all, one, or more jobs"
46 "snapshot:Create a snapshot"
47 "delete:Remove one or more snapshots"
48 "show-environment:Dump environment"
49 "set-environment:Set one or more environment variables"
50 "unset-environment:Unset one or more environment variables"
51 "daemon-reload:Reload systemd manager configuration"
52 "daemon-reexec:Reexecute systemd manager"
53 "default:Enter system default mode"
54 "rescue:Enter system rescue mode"
55 "emergency:Enter system emergency mode"
56 "halt:Shut down and halt the system"
57 "suspend:Suspend the system"
58 "poweroff:Shut down and power-off the system"
59 "reboot:Shut down and reboot the system"
60 "kexec:Shut down and reboot the system with kexec"
61 "exit:Ask for user instance termination"
62 "switch-root:Change root directory"
63 )
64
65 if (( CURRENT == 1 )); then
66 _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
67 else
68 local curcontext="$curcontext" expl
69
70 cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
71 # Deal with any aliases
72 case $cmd in
73 condrestart) cmd="try-restart";;
74 force-reload) cmd="reload-or-try-restart";;
75 esac
76
77 if (( $#cmd )); then
78 curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
79
80 local update_policy
81 zstyle -s ":completion:${curcontext}:" cache-policy update_policy
82 if [[ -z "$update_policy" ]]; then
83 zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
84 fi
85
86 _call_function ret _systemctl_$cmd || _message 'no more arguments'
87 else
88 _message "unknown systemctl command: $words[1]"
89 fi
90 return ret
91 fi
92 }
93
94 __systemctl()
95 {
96 systemctl $_sys_service_mgr --full --no-legend --no-pager "$@"
97 }
98
99
100 # Fills the unit list
101 _systemctl_all_units()
102 {
103 if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) &&
104 ! _retrieve_cache SYS_ALL_UNITS;
105 then
106 _sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} )
107 _store_cache SYS_ALL_UNITS _sys_all_units
108 fi
109 }
110
111 # Fills the unit list including all file units
112 _systemctl_really_all_units()
113 {
114 local -a all_unit_files;
115 local -a really_all_units;
116 if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS ) &&
117 ! _retrieve_cache SYS_REALLY_ALL_UNITS;
118 then
119 all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} )
120 _systemctl_all_units
121 really_all_units=($_sys_all_units $all_unit_files)
122 _sys_really_all_units=(${(u)really_all_units})
123 _store_cache SYS_REALLY_ALL_UNITS _sys_really_all_units
124 fi
125 }
126
127 _filter_units_by_property() {
128 local property=$1 value=$2 ; shift ; shift
129 local -a units ; units=($*)
130 local prop unit
131 for ((i=1; $i <= ${#units[*]}; i++)); do
132 # FIXME: "Failed to issue method call: Unknown unit" errors are ignored for
133 # now (related to DBUS_ERROR_UNKNOWN_OBJECT). in the future, we need to
134 # revert to calling 'systemctl show' once for all units, which is way
135 # faster
136 unit=${units[i]}
137 prop=${(f)"$(_call_program units "$service show --no-pager --property="$property" ${unit} 2>/dev/null")"}
138 if [[ "${prop}" = "$property=$value" ]]; then
139 echo -E - " ${unit}"
140 fi
141 done
142 }
143
144 _systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files)"}##*@.[^[:space:]]##}%%@.*}\@ }
145
146
147 _systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units)"}%% *} )}
148
149 _systemctl_startable_units(){
150 _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
151 _filter_units_by_property CanStart yes $(
152 __systemctl $mode list-unit-files --state enabled,disabled,static | \
153 { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; }
154 __systemctl $mode list-units --state inactive,failed | \
155 { while read -r a b; do echo -E - " $a"; done; } )) ) )
156 }
157
158 _systemctl_restartable_units(){
159 _sys_restartable_units=( $(_filter_units_by_property CanStart yes $(
160 __systemctl $mode list-unit-files --state enabled,disabled,static | \
161 { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; }
162 __systemctl $mode list-units | \
163 { while read -r a b; do echo -E - " $a"; done; } )) )
164 }
165
166 _systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --failed)"}%% *} ) }
167 _systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files) ) }
168
169 local fun
170 # Completion functions for ALL_UNITS
171 for fun in is-active is-failed is-enabled status show cat mask preset help list-dependencies edit ; do
172 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
173 {
174 _systemctl_really_all_units
175 _wanted systemd-units expl unit \
176 compadd "$@" -a - _sys_really_all_units
177 }
178 done
179
180 # Completion functions for ENABLED_UNITS
181 (( $+functions[_systemctl_disable] )) || _systemctl_disable()
182 {
183 local _sys_unit_state; _systemctl_unit_state
184 _wanted systemd-units expl 'enabled unit' \
185 compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
186 }
187
188 (( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
189 {
190 local _sys_unit_state; _systemctl_unit_state
191 _wanted systemd-units expl 'enabled/disabled unit' \
192 compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
193 }
194
195 # Completion functions for DISABLED_UNITS
196 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
197 {
198 local _sys_unit_state; _systemctl_unit_state
199 _wanted systemd-units expl 'disabled unit' \
200 compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
201 }
202
203 # Completion functions for FAILED_UNITS
204 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
205 {
206 local _sys_failed_units; _systemctl_failed_units
207 _wanted systemd-units expl 'failed unit' \
208 compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
209 }
210
211 # Completion functions for STARTABLE_UNITS
212 (( $+functions[_systemctl_start] )) || _systemctl_start()
213 {
214 local _sys_startable_units; _systemctl_startable_units
215 _wanted systemd-units expl 'startable unit' \
216 compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names)
217 }
218
219 # Completion functions for STOPPABLE_UNITS
220 for fun in stop kill try-restart condrestart ; do
221 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
222 {
223 local _sys_active_units; _systemctl_active_units
224 _wanted systemd-units expl 'stoppable unit' \
225 compadd "$@" - $( _filter_units_by_property CanStop yes \
226 ${_sys_active_units[*]} )
227 }
228 done
229
230 # Completion functions for ISOLATABLE_UNITS
231 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
232 {
233 _systemctl_all_units
234 _wanted systemd-units expl 'isolatable unit' \
235 compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
236 ${_sys_all_units[*]} )
237 }
238
239 # Completion functions for RELOADABLE_UNITS
240 for fun in reload reload-or-try-restart force-reload ; do
241 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
242 {
243 local _sys_active_units; _systemctl_active_units
244 _wanted systemd-units expl 'reloadable unit' \
245 compadd "$@" - $( _filter_units_by_property CanReload yes \
246 ${_sys_active_units[*]} )
247 }
248 done
249
250 # Completion functions for RESTARTABLE_UNITS
251 for fun in restart reload-or-restart ; do
252 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
253 {
254 local _sys_restartable_units; _systemctl_restartable_units
255 _wanted systemd-units expl 'restartable unit' \
256 compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names)
257 }
258 done
259
260 # Completion functions for MASKED_UNITS
261 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
262 {
263 local _sys_unit_state; _systemctl_unit_state
264 _wanted systemd-units expl 'masked unit' \
265 compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
266 }
267
268 # Completion functions for JOBS
269 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
270 {
271 _wanted systemd-jobs expl job \
272 compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
273 _message "no jobs found"
274 }
275
276 # Completion functions for SNAPSHOTS
277 (( $+functions[_systemctl_delete] )) || _systemctl_delete()
278 {
279 _wanted systemd-snapshots expl snapshot \
280 compadd "$@" - ${${(f)"$(__systemctl list-units --type snapshot --all)"}%% *} ||
281 _message "no snapshots found"
282 }
283
284 # Completion functions for TARGETS
285 (( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
286 {
287 _wanted systemd-targets expl target \
288 compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
289 _message "no targets found"
290 }
291
292 # Completion functions for ENVS
293 for fun in set-environment unset-environment ; do
294 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
295 {
296 local fun=$0 ; fun=${fun##_systemctl_}
297 local suf
298 if [[ "${fun}" = "set-environment" ]]; then
299 suf='-S='
300 fi
301 _wanted systemd-environment expl 'environment variable' \
302 compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*}
303 }
304 done
305
306 (( $+functions[_systemctl_link] )) || _systemctl_link() {
307 _sd_unit_files
308 }
309
310 (( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() {
311 _files
312 }
313
314 # no systemctl completion for:
315 # [STANDALONE]='daemon-reexec daemon-reload default
316 # emergency exit halt kexec list-jobs list-units
317 # list-unit-files poweroff reboot rescue show-environment'
318 # [NAME]='snapshot'
319
320 _systemctl_caching_policy()
321 {
322 local _sysunits
323 local -a oldcache
324
325 # rebuild if cache is more than a day old
326 oldcache=( "$1"(mh+1) )
327 (( $#oldcache )) && return 0
328
329 _sysunits=(${${(f)"$(__systemctl --all)"}%% *})
330
331 if (( $#_sysunits )); then
332 for unit in $_sysunits; do
333 [[ "$unit" -nt "$1" ]] && return 0
334 done
335 fi
336
337 return 1
338 }
339
340 _unit_states() {
341 local -a _states
342 _states=(loaded failed active inactive not-found listening running waiting plugged mounted exited dead masked)
343 _values -s , "${_states[@]}"
344 }
345
346 _unit_types() {
347 local -a _types
348 _types=(automount busname device mount path service snapshot socket swap target timer)
349 _values -s , "${_types[@]}"
350 }
351
352 _unit_properties() {
353 if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES ) &&
354 ! _retrieve_cache SYS_ALL_PROPERTIES;
355 then
356 _sys_all_properties=( ${${(M)${(f)"$(__systemctl show --all;
357 @rootlibexecdir@/systemd --dump-configuration-items)"}##[[:alnum:]]##=*}%%=*}
358 )
359 _store_cache SYS_ALL_PROPRTIES _sys_all_properties
360 fi
361 _values -s , "${_sys_all_properties[@]}"
362 }
363
364 _job_modes() {
365 local -a _modes
366 _modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
367 _values -s , "${_modes[@]}"
368 }
369
370 local -a _modes; _modes=("--user" "--system")
371 local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]:---system}
372 _arguments -s \
373 {-h,--help}'[Show help]' \
374 '--version[Show package version]' \
375 {-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \
376 '--state=[Display units in the specified state]:unit state:_unit_states' \
377 '--job-mode=[Specify how to deal with other jobs]:mode:_job_modes' \
378 {-p+,--property=}'[Show only properties by specific name]:unit property:_unit_properties' \
379 {-a,--all}'[Show all units/properties, including dead/empty ones]' \
380 '--reverse[Show reverse dependencies]' \
381 '--after[Show units ordered after]' \
382 '--before[Show units ordered before]' \
383 '--failed[Show only failed units]' \
384 {-l,--full}"[Don't ellipsize unit names on output]" \
385 '--show-types[When showing sockets, show socket type]' \
386 {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \
387 {-q,--quiet}'[Suppress output]' \
388 '--no-block[Do not wait until operation finished]' \
389 '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
390 '--no-pager[Do not pipe output into a pager]' \
391 '--system[Connect to system manager]' \
392 '--user[Connect to user service manager]' \
393 "--no-wall[Don't send wall message before halt/power-off/reboot]" \
394 '--global[Enable/disable unit files globally]' \
395 "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
396 '--no-ask-password[Do not ask for system passwords]' \
397 '--kill-who=[Who to send signal to]:killwho:(main control all)' \
398 {-s+,--signal=}'[Which signal to send]:signal:_signals' \
399 {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
400 '--root=[Enable unit files in the specified root directory]:directory:_directories' \
401 '--runtime[Enable unit files only temporarily until next reboot]' \
402 {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
403 {-P,--privileged}'[Acquire privileges before execution]' \
404 {-n+,--lines=}'[Journal entries to show]:number of entries' \
405 {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
406 '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \
407 '--plain[When used with list-dependencies, print output as a list]' \
408 '*::systemctl command:_systemctl_command'