]> git.ipfire.org Git - thirdparty/systemd.git/blame - shell-completion/zsh/_systemctl.in
Merge pull request #11795 from yuwata/fix-network-routing-policy-11280
[thirdparty/systemd.git] / shell-completion / zsh / _systemctl.in
CommitLineData
ff7a0685 1#compdef systemctl
7059062c 2# SPDX-License-Identifier: LGPL-2.1+
ff7a0685 3
c501ecd7 4(( $+functions[_systemctl_commands] )) || _systemctl_commands()
ff7a0685
WG
5{
6 local -a _systemctl_cmds
7 _systemctl_cmds=(
d808ca64 8 "list-sockets:List sockets"
d1811159 9 "list-timers:List timers"
ff7a0685
WG
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"
e68c79db 21 "suspend-then-hibernate:Suspend the system for a period of time, and then hibernate it"
aabf5d42 22 "try-reload-or-restart:Reload one or more units if possible, otherwise restart if active"
ff7a0685
WG
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"
6da49b8b 29 "cat:Show the source unit files and drop-ins"
ff7a0685 30 "reset-failed:Reset failed state for all, one, or more units"
ff7a0685
WG
31 "list-unit-files:List installed unit files"
32 "enable:Enable one or more unit files"
33 "disable:Disable one or more unit files"
d739ce98
FS
34 "add-wants:Add Wants= dependencies to a unit"
35 "add-requires:Add Requires= dependencies to a unit"
ff7a0685
WG
36 "reenable:Reenable one or more unit files"
37 "preset:Enable/disable one or more unit files based on preset configuration"
1cf3c30c
ZJS
38 "set-default:Set the default target"
39 "get-default:Query the default target"
409886c4 40 "edit:Edit one or more unit files"
1cf3c30c 41 "is-system-running:Query overall status of the system"
ff7a0685
WG
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"
ff7a0685
WG
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"
7b742b31 64 "switch-root:Change root directory"
0f8158bd 65 "revert:Revert unit files to their vendor versions"
c4a2deba 66 "set-property:Sets one or more properties of a unit"
ff7a0685
WG
67 )
68
69 if (( CURRENT == 1 )); then
70 _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
71 else
d34b7c11 72 local curcontext="$curcontext" expl
ff7a0685
WG
73
74 cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
75 # Deal with any aliases
76 case $cmd in
77 condrestart) cmd="try-restart";;
aabf5d42 78 force-reload) cmd="try-reload-or-restart";;
ff7a0685
WG
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
c501ecd7 98# @todo _systemd-run has a helper with the same name, so we must redefine
ff7a0685
WG
99__systemctl()
100{
99171d2f 101 systemctl $_sys_service_mgr --full --no-legend --no-pager "$@" 2>/dev/null
ff7a0685
WG
102}
103
104
105# Fills the unit list
c501ecd7 106(( $+functions[_systemctl_all_units] )) ||
ff7a0685
WG
107_systemctl_all_units()
108{
88e4dbd5
АТ
109 if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS$_sys_service_mgr ) ||
110 ! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr;
ff7a0685 111 then
98476dc8 112 _sys_all_units=( ${${(f)"$(__systemctl list-units --all "$PREFIX*" )"}%% *} )
88e4dbd5 113 _store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units
ff7a0685
WG
114 fi
115}
116
117# Fills the unit list including all file units
c501ecd7 118(( $+functions[_systemctl_really_all_units] )) ||
ff7a0685
WG
119_systemctl_really_all_units()
120{
121 local -a all_unit_files;
122 local -a really_all_units;
88e4dbd5
АТ
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;
ff7a0685 125 then
98476dc8 126 all_unit_files=( ${${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}%% *} )
ff7a0685
WG
127 _systemctl_all_units
128 really_all_units=($_sys_all_units $all_unit_files)
129 _sys_really_all_units=(${(u)really_all_units})
88e4dbd5 130 _store_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr _sys_really_all_units
ff7a0685
WG
131 fi
132}
133
c501ecd7 134(( $+functions[_filter_units_by_property] )) ||
ff7a0685 135_filter_units_by_property() {
6c9414a7
EC
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=}"
ff7a0685
WG
141}
142
c501ecd7 143(( $+functions[_systemctl_get_non_template_names] )) ||
4ed14116
EV
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
c501ecd7 149(( $+functions[_systemctl_get_template_names] )) ||
ff728637 150_systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}##*@.[^[:space:]]##}%%@.*}\@ }
e9a19bd8 151
c501ecd7 152(( $+functions[_systemctl_active_units] )) ||
ff728637 153_systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units "$PREFIX*" )"}%% *} )}
81333ecf 154
c501ecd7 155(( $+functions[_systemctl_startable_units] )) ||
81333ecf 156_systemctl_startable_units(){
992f51ea 157 _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
158 _filter_units_by_property CanStart yes ${${${(f)"$(
8a6236e5 159 __systemctl $mode list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$PREFIX*"
ff728637 160 __systemctl $mode list-units --state inactive,failed "$PREFIX*"
992f51ea 161 )"}:#*@.*}%%[[:space:]]*}
162 )) )
81333ecf
ZJS
163}
164
c501ecd7 165(( $+functions[_systemctl_restartable_units] )) ||
81333ecf 166_systemctl_restartable_units(){
992f51ea 167 _sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$(
ff728637
EV
168 __systemctl $mode list-unit-files --state enabled,disabled,static "$PREFIX*"
169 __systemctl $mode list-units "$PREFIX*"
992f51ea 170 )"}:#*@.*}%%[[:space:]]*} ) )
81333ecf
ZJS
171}
172
c501ecd7 173(( $+functions[_systemctl_failed_units] )) ||
ff728637 174_systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed "$PREFIX*" )"}%% *} ) }
c501ecd7 175
176(( $+functions[_systemctl_unit_state] )) ||
ff728637 177_systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files "$PREFIX*" ) ) }
ff7a0685 178
67afa931 179local fun
ff7a0685 180# Completion functions for ALL_UNITS
4ed14116 181for fun in cat mask ; do
ff7a0685
WG
182 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
183 {
184 _systemctl_really_all_units
d34b7c11
EC
185 _wanted systemd-units expl unit \
186 compadd "$@" -a - _sys_really_all_units
ff7a0685
WG
187 }
188done
189
4ed14116 190# Completion functions for NONTEMPLATE_UNITS
c4a2deba 191for fun in is-active is-failed is-enabled status show preset help list-dependencies edit revert add-wants add-requires set-property; do
4ed14116
EV
192 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
193 {
194 _wanted systemd-units expl unit \
195 compadd "$@" - $(_systemctl_get_non_template_names)
196 }
197done
198
ff7a0685 199# Completion functions for ENABLED_UNITS
e9a19bd8
ZJS
200(( $+functions[_systemctl_disable] )) || _systemctl_disable()
201{
fb869ca1 202 local _sys_unit_state; _systemctl_unit_state
d34b7c11
EC
203 _wanted systemd-units expl 'enabled unit' \
204 compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
e9a19bd8
ZJS
205}
206
207(( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
208{
fb869ca1 209 local _sys_unit_state; _systemctl_unit_state
d34b7c11
EC
210 _wanted systemd-units expl 'enabled/disabled unit' \
211 compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
e9a19bd8 212}
ff7a0685
WG
213
214# Completion functions for DISABLED_UNITS
215(( $+functions[_systemctl_enable] )) || _systemctl_enable()
216{
fb869ca1 217 local _sys_unit_state; _systemctl_unit_state
d34b7c11
EC
218 _wanted systemd-units expl 'disabled unit' \
219 compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
ff7a0685
WG
220}
221
222# Completion functions for FAILED_UNITS
223(( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
224{
463985a9 225 local _sys_failed_units; _systemctl_failed_units
d34b7c11
EC
226 _wanted systemd-units expl 'failed unit' \
227 compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
ff7a0685
WG
228}
229
230# Completion functions for STARTABLE_UNITS
231(( $+functions[_systemctl_start] )) || _systemctl_start()
232{
bf8864c2 233 local _sys_startable_units; _systemctl_startable_units
d34b7c11 234 _wanted systemd-units expl 'startable unit' \
9b536b1a 235 compadd "$@" - ${_sys_startable_units[*]}
ff7a0685
WG
236}
237
238# Completion functions for STOPPABLE_UNITS
239for fun in stop kill try-restart condrestart ; do
240 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
241 {
bf8864c2 242 local _sys_active_units; _systemctl_active_units
d34b7c11
EC
243 _wanted systemd-units expl 'stoppable unit' \
244 compadd "$@" - $( _filter_units_by_property CanStop yes \
245 ${_sys_active_units[*]} )
ff7a0685
WG
246 }
247done
248
249# Completion functions for ISOLATABLE_UNITS
250(( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
251{
252 _systemctl_all_units
d34b7c11
EC
253 _wanted systemd-units expl 'isolatable unit' \
254 compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
255 ${_sys_all_units[*]} )
ff7a0685
WG
256}
257
258# Completion functions for RELOADABLE_UNITS
aabf5d42 259for fun in reload try-reload-or-restart force-reload ; do
ff7a0685
WG
260 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
261 {
bf8864c2 262 local _sys_active_units; _systemctl_active_units
d34b7c11
EC
263 _wanted systemd-units expl 'reloadable unit' \
264 compadd "$@" - $( _filter_units_by_property CanReload yes \
265 ${_sys_active_units[*]} )
ff7a0685
WG
266 }
267done
268
269# Completion functions for RESTARTABLE_UNITS
270for fun in restart reload-or-restart ; do
271 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
272 {
bf8864c2 273 local _sys_restartable_units; _systemctl_restartable_units
d34b7c11 274 _wanted systemd-units expl 'restartable unit' \
9b536b1a 275 compadd "$@" - ${_sys_restartable_units[*]}
ff7a0685
WG
276 }
277done
278
279# Completion functions for MASKED_UNITS
280(( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
281{
fb869ca1 282 local _sys_unit_state; _systemctl_unit_state
d34b7c11
EC
283 _wanted systemd-units expl 'masked unit' \
284 compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
ff7a0685
WG
285}
286
287# Completion functions for JOBS
288(( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
289{
d34b7c11
EC
290 _wanted systemd-jobs expl job \
291 compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
292 _message "no jobs found"
ff7a0685
WG
293}
294
1cf3c30c
ZJS
295# Completion functions for TARGETS
296(( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
297{
d34b7c11 298 _wanted systemd-targets expl target \
98476dc8 299 compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all "$PREFIX*" )"}%% *} ||
d34b7c11 300 _message "no targets found"
ff7a0685
WG
301}
302
303# Completion functions for ENVS
304for 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
d34b7c11
EC
312 _wanted systemd-environment expl 'environment variable' \
313 compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*}
ff7a0685
WG
314 }
315done
316
2c12a402
ZJS
317(( $+functions[_systemctl_link] )) || _systemctl_link() {
318 _sd_unit_files
319}
ff7a0685 320
7b742b31
ZJS
321(( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() {
322 _files
323}
324
ff7a0685 325# no systemctl completion for:
299c397c 326# [STANDALONE]='daemon-reexec daemon-reload default
ff7a0685
WG
327# emergency exit halt kexec list-jobs list-units
328# list-unit-files poweroff reboot rescue show-environment'
ff7a0685 329
c501ecd7 330(( $+functions[_systemctl_caching_policy] )) ||
ff7a0685
WG
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
2103d29d 340 _sysunits=(${${(f)"$(__systemctl --all)"}%% *})
ff7a0685
WG
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
c501ecd7 351(( $+functions[_systemctl_unit_states] )) ||
352_systemctl_unit_states() {
298b9e23 353 local -a _states
840b2c0e 354 _states=("${(fo)$(__systemctl --state=help)}")
298b9e23
WG
355 _values -s , "${_states[@]}"
356}
357
c501ecd7 358(( $+functions[_systemctl_unit_types] )) ||
359_systemctl_unit_types() {
298b9e23 360 local -a _types
840b2c0e 361 _types=("${(fo)$(__systemctl -t help)}")
298b9e23
WG
362 _values -s , "${_types[@]}"
363}
364
c501ecd7 365(( $+functions[_systemctl_unit_properties] )) ||
366_systemctl_unit_properties() {
88e4dbd5
АТ
367 if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES$_sys_service_mgr ) ||
368 ! _retrieve_cache SYS_ALL_PROPERTIES$_sys_service_mgr;
c0a67aef 369 then
dd74faef 370 _sys_all_properties=( ${${(M)${(f)"$(@rootlibexecdir@/systemd --dump-bus-properties)"}}} )
88e4dbd5 371 _store_cache SYS_ALL_PROPERTIES$_sys_service_mgr _sys_all_properties
c0a67aef
ZJS
372 fi
373 _values -s , "${_sys_all_properties[@]}"
374}
375
c501ecd7 376(( $+functions[_systemctl_job_modes] )) ||
377_systemctl_job_modes() {
903e7c37
ZJS
378 local -a _modes
379 _modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
380 _values -s , "${_modes[@]}"
381}
382
e09d0d46 383# Build arguments for "systemctl" to be used in completion.
68c4f6d4 384local -a _modes; _modes=("--user" "--system")
e09d0d46
DH
385# Use the last mode (they are exclusive and the last one is used).
386local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]}
ff7a0685
WG
387_arguments -s \
388 {-h,--help}'[Show help]' \
389 '--version[Show package version]' \
c501ecd7 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' \
ff7a0685
WG
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]' \
ff7a0685 398 {-l,--full}"[Don't ellipsize unit names on output]" \
ff7a0685 399 '--show-types[When showing sockets, show socket type]' \
ff7a0685
WG
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]" \
4e93d369 408 '--global[Enable/disable/mask unit files globally]' \
ff7a0685
WG
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)' \
862f4963 412 {-s+,--signal=}'[Which signal to send]:signal:_signals' \
ff7a0685 413 {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
4e93d369
LR
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]' \
6da49b8b 416 {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
ff7a0685 417 {-P,--privileged}'[Acquire privileges before execution]' \
862f4963 418 {-n+,--lines=}'[Journal entries to show]:number of entries' \
a02c5fe7 419 {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
5bdf2243 420 '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \
ff7a0685 421 '--plain[When used with list-dependencies, print output as a list]' \
bef19548 422 '--failed[Show failed units]' \
c501ecd7 423 '*::systemctl command:_systemctl_commands'