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