chiark / gitweb /
shell-completion: systemctl set-default,get-default,is-system-running
[elogind.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     "is-system-running:Query overall status of the system"
37     "help:Show documentation for specified units"
38     "list-dependencies:Show unit dependency tree"
39     "mask:Mask one or more units"
40     "unmask:Unmask one or more units"
41     "link:Link one or more units files into the search path"
42     "is-enabled:Check whether unit files are enabled"
43     "list-jobs:List jobs"
44     "cancel:Cancel all, one, or more jobs"
45     "snapshot:Create a snapshot"
46     "delete:Remove one or more snapshots"
47     "show-environment:Dump environment"
48     "set-environment:Set one or more environment variables"
49     "unset-environment:Unset one or more environment variables"
50     "daemon-reload:Reload systemd manager configuration"
51     "daemon-reexec:Reexecute systemd manager"
52     "default:Enter system default mode"
53     "rescue:Enter system rescue mode"
54     "emergency:Enter system emergency mode"
55     "halt:Shut down and halt the system"
56     "suspend:Suspend the system"
57     "poweroff:Shut down and power-off the system"
58     "reboot:Shut down and reboot the system"
59     "kexec:Shut down and reboot the system with kexec"
60     "exit:Ask for user instance termination"
61   )
62
63   if (( CURRENT == 1 )); then
64     _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
65   else
66     local curcontext="$curcontext"
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="reload-or-try-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   local -a _modes
95   _modes=("--user" "--system")
96   systemctl ${words:*_modes} --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=( $(__systemctl list-units --all | { while read -r a b; do echo -E - " $a"; done; }) )
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=( $(__systemctl list-unit-files | { while read -r a b; do echo -E - " $a"; done; }) )
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 " ${unit}"
140     fi
141   done
142 }
143
144 _systemctl_all_units() { { __systemctl list-unit-files; __systemctl list-units --all; } | { while read -r a b; do echo -E - " $a"; done; } }
145 _systemctl_get_template_names() { __systemctl list-unit-files | { while read -r a b; do  [[ $a =~ @\. ]] && echo -E - " ${a%%@.*}@"; done; } }
146
147
148 _systemctl_active_units()  {_sys_active_units=(  $(__systemctl list-units          | { while read -r a b; do echo -E - " $a"; done; }) )}
149 _systemctl_startable_units(){_sys_startable_units=($(__systemctl list-units --state inactive,failed -- $(_systemctl_all_units) | { while read -r a b c d; do [[ $b == "loaded" ]] && echo -E - " $a"; done; }) )}
150 _systemctl_restartable_units(){_sys_restartable_units=($(__systemctl list-units --state inactive,failed,active -- $(_systemctl_all_units) | { while read -r a b c d; do [[ $b == "loaded" ]] && echo -E - " $a"; done; }) )}
151 _systemctl_failed_units()  {_sys_failed_units=(  $(__systemctl list-units --failed | { while read -r a b; do echo -E - " $a"; done; }) )}
152 _systemctl_enabled_units() {_sys_enabled_units=( $(__systemctl list-unit-files     | { while read -r a b; do [[ $b == "enabled" ]] && echo -E - " $a"; done; }) )}
153 _systemctl_disabled_units(){_sys_disabled_units=($(__systemctl list-unit-files     | { while read -r a b; do [[ $b == "disabled" ]] && echo -E - " $a"; done; }) )}
154 _systemctl_masked_units()  {_sys_masked_units=(  $(__systemctl list-unit-files     | { while read -r a b; do [[ $b == "masked" ]] && echo -E - " $a"; done; }) )}
155
156 # Completion functions for ALL_UNITS
157 for fun in is-active is-failed is-enabled status show cat mask preset help list-dependencies ; do
158   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
159   {
160     _systemctl_really_all_units
161     compadd "$@" -a - _sys_really_all_units
162   }
163 done
164
165 # Completion functions for ENABLED_UNITS
166 (( $+functions[_systemctl_disable] )) || _systemctl_disable()
167 {
168     _systemctl_enabled_units
169     compadd "$@" -a - _sys_enabled_units
170 }
171
172 (( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
173 {
174     _systemctl_enabled_units
175     _systemctl_disabled_units
176     compadd "$@" -a - _sys_enabled_units _sys_disabled_units $(_systemctl_get_template_names)
177 }
178
179 # Completion functions for DISABLED_UNITS
180 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
181 {
182   _systemctl_disabled_units
183   compadd "$@" -a - _sys_disabled_units $(_systemctl_get_template_names)
184 }
185
186 # Completion functions for FAILED_UNITS
187 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
188 {
189   _systemctl_failed_units
190   compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
191 }
192
193 # Completion functions for STARTABLE_UNITS
194 (( $+functions[_systemctl_start] )) || _systemctl_start()
195 {
196    _systemctl_startable_units
197    compadd "$@" - $( _filter_units_by_property CanStart yes \
198       ${_sys_startable_units[*]} )
199 }
200
201 # Completion functions for STOPPABLE_UNITS
202 for fun in stop kill try-restart condrestart ; do
203   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
204   {
205     _systemctl_active_units
206     compadd "$@" - $( _filter_units_by_property CanStop yes \
207       ${_sys_active_units[*]} )
208   }
209 done
210
211 # Completion functions for ISOLATABLE_UNITS
212 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
213 {
214   _systemctl_all_units
215   compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
216     ${_sys_all_units[*]} )
217 }
218
219 # Completion functions for RELOADABLE_UNITS
220 for fun in reload reload-or-try-restart force-reload ; do
221   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
222   {
223     _systemctl_active_units
224     compadd "$@" - $( _filter_units_by_property CanReload yes \
225       ${_sys_active_units[*]} )
226   }
227 done
228
229 # Completion functions for RESTARTABLE_UNITS
230 for fun in restart reload-or-restart ; do
231   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
232   {
233     _systemctl_restartable_units
234     compadd "$@" - $( _filter_units_by_property CanStart yes \
235       ${_sys_restartable_units[*]} )
236   }
237 done
238
239 # Completion functions for MASKED_UNITS
240 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
241 {
242   _systemctl_masked_units
243   compadd "$@" -a - _sys_masked_units || _message "no masked units found"
244 }
245
246 # Completion functions for JOBS
247 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
248 {
249   compadd "$@" - $(__systemctl list-jobs \
250     | cut -d' ' -f1  2>/dev/null ) || _message "no jobs found"
251 }
252
253 # Completion functions for SNAPSHOTS
254 (( $+functions[_systemctl_delete] )) || _systemctl_delete()
255 {
256   compadd "$@" - $(__systemctl list-units --type snapshot --all \
257     | cut -d' ' -f1  2>/dev/null ) || _message "no snapshots found"
258 }
259
260 # Completion functions for TARGETS
261 (( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
262 {
263   compadd "$@" - $(__systemctl list-unit-files --type target --all \
264     | cut -d' ' -f1  2>/dev/null ) || _message "no targets found"
265 }
266
267 # Completion functions for ENVS
268 for fun in set-environment unset-environment ; do
269   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
270   {
271     local fun=$0 ; fun=${fun##_systemctl_}
272     local suf
273     if [[ "${fun}" = "set-environment" ]]; then
274       suf='-S='
275     fi
276
277     compadd "$@" ${suf} - $(systemctl show-environment \
278       | while read line; do echo " ${line%%\=}";done )
279   }
280 done
281
282 (( $+functions[_systemctl_link] )) || _systemctl_link() {
283    _sd_unit_files
284 }
285
286 # no systemctl completion for:
287 #    [STANDALONE]='daemon-reexec daemon-reload default
288 #                  emergency exit halt kexec list-jobs list-units
289 #                  list-unit-files poweroff reboot rescue show-environment'
290 #         [NAME]='snapshot'
291
292 _systemctl_caching_policy()
293 {
294   local _sysunits
295   local -a oldcache
296
297   # rebuild if cache is more than a day old
298   oldcache=( "$1"(mh+1) )
299   (( $#oldcache )) && return 0
300
301   _sysunits=($(__systemctl --all | cut -d' ' -f1))
302
303   if (( $#_sysunits )); then
304     for unit in $_sysunits; do
305       [[ "$unit" -nt "$1" ]] && return 0
306     done
307   fi
308
309   return 1
310 }
311
312 _unit_states() {
313     local -a _states
314     _states=(loaded failed active inactive not-found listening running waiting plugged mounted exited dead masked)
315     _values -s , "${_states[@]}"
316 }
317
318 _unit_types() {
319     local -a _types
320     _types=(automount busname device mount path service snapshot socket swap target timer)
321     _values -s , "${_types[@]}"
322 }
323
324 _unit_properties() {
325   if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES ) &&
326     ! _retrieve_cache SYS_ALL_PROPERTIES;
327   then
328     _sys_all_properties=( $( {__systemctl show --all;
329        @rootlibexecdir@/systemd --dump-configuration-items; } | {
330        while IFS='=' read -r a b; do [ -n "$b" ] && echo "$a"; done
331     }) )
332     _store_cache SYS_ALL_PROPRTIES _sys_all_properties
333   fi
334   _values -s , "${_sys_all_properties[@]}"
335 }
336
337 _arguments -s \
338     {-h,--help}'[Show help]' \
339     '--version[Show package version]' \
340     {-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \
341     '--state=[Display units in the specifyied state]:unit state:_unit_states' \
342     {-p+,--property=}'[Show only properties by specific name]:unit property:_unit_properties' \
343     {-a,--all}'[Show all units/properties, including dead/empty ones]' \
344     '--reverse[Show reverse dependencies]' \
345     '--after[Show units ordered after]' \
346     '--before[Show units ordered before]' \
347     '--failed[Show only failed units]' \
348     {-l,--full}"[Don't ellipsize unit names on output]" \
349     '--fail[When queueing a new job, fail if conflicting jobs are pending]' \
350     '--show-types[When showing sockets, show socket type]' \
351     '--irreversible[Mark transactions as irreversible]' \
352     '--ignore-dependencies[When queueing a new job, ignore all its dependencies]' \
353     {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \
354     {-q,--quiet}'[Suppress output]' \
355     '--no-block[Do not wait until operation finished]' \
356     '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
357     '--no-pager[Do not pipe output into a pager]' \
358     '--system[Connect to system manager]' \
359     '--user[Connect to user service manager]' \
360     "--no-wall[Don't send wall message before halt/power-off/reboot]" \
361     '--global[Enable/disable unit files globally]' \
362     "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
363     '--no-ask-password[Do not ask for system passwords]' \
364     '--kill-who=[Who to send signal to]:killwho:(main control all)' \
365     {-s+,--signal=}'[Which signal to send]:signal:_signals' \
366     {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
367     '--root=[Enable unit files in the specified root directory]:directory:_directories' \
368     '--runtime[Enable unit files only temporarily until next reboot]' \
369     {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
370     {-P,--privileged}'[Acquire privileges before execution]' \
371     {-n+,--lines=}'[Journal entries to show]:number of entries' \
372     {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
373     '--plain[When used with list-dependencies, print output as a list]' \
374     '*::systemctl command:_systemctl_command'