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