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