chiark / gitweb /
shell-completion: systemctl cat
[elogind.git] / shell-completion / zsh / _systemctl
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     "load:Load one or more units"
30     "list-unit-files:List installed unit files"
31     "enable:Enable one or more unit files"
32     "disable:Disable one or more unit files"
33     "reenable:Reenable one or more unit files"
34     "preset:Enable/disable one or more unit files based on preset configuration"
35     "help:Show documentation for specified units"
36     "list-dependencies:Show unit dependency tree"
37     "mask:Mask one or more units"
38     "unmask:Unmask one or more units"
39     "link:Link one or more units files into the search path"
40     "is-enabled:Check whether unit files are enabled"
41     "list-jobs:List jobs"
42     "cancel:Cancel all, one, or more jobs"
43     "snapshot:Create a snapshot"
44     "delete:Remove one or more snapshots"
45     "show-environment:Dump environment"
46     "set-environment:Set one or more environment variables"
47     "unset-environment:Unset one or more environment variables"
48     "daemon-reload:Reload systemd manager configuration"
49     "daemon-reexec:Reexecute systemd manager"
50     "default:Enter system default mode"
51     "rescue:Enter system rescue mode"
52     "emergency:Enter system emergency mode"
53     "halt:Shut down and halt the system"
54     "suspend:Suspend the system"
55     "poweroff:Shut down and power-off the system"
56     "reboot:Shut down and reboot the system"
57     "kexec:Shut down and reboot the system with kexec"
58     "exit:Ask for user instance termination"
59   )
60
61   if (( CURRENT == 1 )); then
62     _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
63   else
64     local curcontext="$curcontext"
65
66     cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
67     # Deal with any aliases
68     case $cmd in
69       condrestart) cmd="try-restart";;
70       force-reload) cmd="reload-or-try-restart";;
71     esac
72
73     if (( $#cmd )); then
74       curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
75
76       local update_policy
77       zstyle -s ":completion:${curcontext}:" cache-policy update_policy
78       if [[ -z "$update_policy" ]]; then
79         zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
80       fi
81
82       _call_function ret _systemctl_$cmd || _message 'no more arguments'
83     else
84       _message "unknown systemctl command: $words[1]"
85     fi
86     return ret
87   fi
88 }
89
90 __systemctl()
91 {
92   local -a _modes
93   _modes=("--user" "--system")
94   systemctl ${words:*_modes} --full --no-legend --no-pager "$@"
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 ) &&
102     ! _retrieve_cache SYS_ALL_UNITS;
103   then
104     _sys_all_units=( $(__systemctl list-units --all | { while read a b; do echo " $a"; done; }) )
105     _store_cache SYS_ALL_UNITS _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 ) &&
115     ! _retrieve_cache SYS_REALLY_ALL_UNITS;
116   then
117     all_unit_files=( $(__systemctl list-unit-files | { while read a b; do echo " $a"; done; }) )
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_really_all_units
122   fi
123 }
124
125 _filter_units_by_property() {
126   local property=$1 value=$2 ; shift ; shift
127   local -a units ; units=($*)
128   local prop unit
129   for ((i=1; $i <= ${#units[*]}; i++)); do
130     # FIXME: "Failed to issue method call: Unknown unit" errors are ignored for
131     # now (related to DBUS_ERROR_UNKNOWN_OBJECT). in the future, we need to
132     # revert to calling 'systemctl show' once for all units, which is way
133     # faster
134     unit=${units[i]}
135     prop=${(f)"$(_call_program units "$service show --no-pager --property="$property" ${unit} 2>/dev/null")"}
136     if [[ "${prop}" = "$property=$value" ]]; then
137       echo " ${unit}"
138     fi
139   done
140 }
141
142 _systemctl_active_units()  {_sys_active_units=(  $(__systemctl list-units          | { while read a b; do echo " $a"; done; }) )}
143 _systemctl_inactive_units(){_sys_inactive_units=($(__systemctl list-units --all    | { while read a b c d; do [[ $c == "inactive" || $c == "failed" ]] && echo " $a"; done; }) )}
144 _systemctl_failed_units()  {_sys_failed_units=(  $(__systemctl list-units --failed | { while read a b; do echo " $a"; done; }) )}
145 _systemctl_enabled_units() {_sys_enabled_units=( $(__systemctl list-unit-files     | { while read a b; do [[ $b == "enabled" ]] && echo " $a"; done; }) )}
146 _systemctl_disabled_units(){_sys_disabled_units=($(__systemctl list-unit-files     | { while read a b; do [[ $b == "disabled" ]] && echo " $a"; done; }) )}
147 _systemctl_masked_units()  {_sys_masked_units=(  $(__systemctl list-unit-files     | { while read a b; do [[ $b == "masked" ]] && echo " $a"; done; }) )}
148
149 # Completion functions for ALL_UNITS
150 for fun in is-active is-failed is-enabled status show cat mask preset help list-dependencies ; do
151   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
152   {
153     _systemctl_really_all_units
154     compadd "$@" -a - _sys_really_all_units
155   }
156 done
157
158 # Completion functions for ENABLED_UNITS
159 for fun in disable reenable ; do
160   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
161   {
162     _systemctl_enabled_units
163     _systemctl_disabled_units
164     compadd "$@" -a - _sys_enabled_units _sys_disabled_units
165   }
166 done
167
168 # Completion functions for DISABLED_UNITS
169 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
170 {
171   _systemctl_disabled_units
172   compadd "$@" -a - _sys_disabled_units
173 }
174
175 # Completion functions for FAILED_UNITS
176 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
177 {
178   _systemctl_failed_units
179   compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
180 }
181
182 # Completion functions for STARTABLE_UNITS
183 (( $+functions[_systemctl_start] )) || _systemctl_start()
184 {
185   _systemctl_inactive_units
186   compadd "$@" -a - _sys_inactive_units
187 }
188
189 # Completion functions for STOPPABLE_UNITS
190 for fun in stop kill try-restart condrestart ; do
191   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
192   {
193     _systemctl_active_units
194     compadd "$@" - $( _filter_units_by_property CanStop yes \
195       ${_sys_active_units[*]} )
196   }
197 done
198
199 # Completion functions for ISOLATABLE_UNITS
200 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
201 {
202   _systemctl_all_units
203   compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
204     ${_sys_all_units[*]} )
205 }
206
207 # Completion functions for RELOADABLE_UNITS
208 for fun in reload reload-or-try-restart force-reload ; do
209   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
210   {
211     _systemctl_active_units
212     compadd "$@" - $( _filter_units_by_property CanReload yes \
213       ${_sys_active_units[*]} )
214   }
215 done
216
217 # Completion functions for RESTARTABLE_UNITS
218 for fun in restart reload-or-restart ; do
219   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
220   {
221     _systemctl_all_units
222     compadd "$@" - $( _filter_units_by_property CanStart yes \
223       ${_sys_all_units[*]} | while read line; do \
224       [[ "$line" =~ \.device$ ]] || echo " $line"; \
225       done )
226   }
227 done
228
229 # Completion functions for MASKED_UNITS
230 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
231 {
232   _systemctl_masked_units
233   compadd "$@" -a - _sys_masked_units || _message "no masked unit found"
234 }
235
236 # Completion functions for JOBS
237 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
238 {
239   compadd "$@" - $(__systemctl list-jobs \
240     | cut -d' ' -f1  2>/dev/null ) || _message "no job found"
241 }
242
243 # Completion functions for SNAPSHOTS
244 (( $+functions[_systemctl_delete] )) || _systemctl_delete()
245 {
246   compadd "$@" - $(__systemctl list-units --type snapshot --all \
247     | cut -d' ' -f1  2>/dev/null ) || _message "no snapshot found"
248 }
249
250 # Completion functions for ENVS
251 for fun in set-environment unset-environment ; do
252   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
253   {
254     local fun=$0 ; fun=${fun##_systemctl_}
255     local suf
256     if [[ "${fun}" = "set-environment" ]]; then
257       suf='-S='
258     fi
259
260     compadd "$@" ${suf} - $(systemctl show-environment \
261       | while read line; do echo " ${line%%\=}";done )
262   }
263 done
264
265 (( $+functions[_systemctl_link] )) || _systemctl_link() { _files }
266
267 # no systemctl completion for:
268 #    [STANDALONE]='daemon-reexec daemon-reload default
269 #                  emergency exit halt kexec list-jobs list-units
270 #                  list-unit-files poweroff reboot rescue show-environment'
271 #         [NAME]='snapshot load'
272
273 _systemctl_caching_policy()
274 {
275   local _sysunits
276   local -a oldcache
277
278   # rebuild if cache is more than a day old
279   oldcache=( "$1"(mh+1) )
280   (( $#oldcache )) && return 0
281
282   _sysunits=($(__systemctl --all | cut -d' ' -f1))
283
284   if (( $#_sysunits )); then
285     for unit in $_sysunits; do
286       [[ "$unit" -nt "$1" ]] && return 0
287     done
288   fi
289
290   return 1
291 }
292
293 _unit_states() {
294     local -a _states
295     _states=(loaded failed active inactive not-found listening running waiting plugged mounted exited dead masked)
296     _values -s , "${_states[@]}"
297 }
298
299 _unit_types() {
300     local -a _types
301     _types=(automount device mount path service snapshot socket swap target timer)
302     _values -s , "${_types[@]}"
303 }
304
305 _arguments -s \
306     {-h,--help}'[Show help]' \
307     '--version[Show package version]' \
308     {-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \
309     '--state=[Display units in the specifyied state]:unit state:_unit_states' \
310     \*{-p+,--property=}'[Show only properties by specific name]:unit property' \
311     {-a,--all}'[Show all units/properties, including dead/empty ones]' \
312     '--reverse[Show reverse dependencies]' \
313     '--after[Show units ordered after]' \
314     '--before[Show units ordered before]' \
315     '--failed[Show only failed units]' \
316     {-l,--full}"[Don't ellipsize unit names on output]" \
317     '--fail[When queueing a new job, fail if conflicting jobs are pending]' \
318     '--show-types[When showing sockets, show socket type]' \
319     '--irreversible[Mark transactions as irreversible]' \
320     '--ignore-dependencies[When queueing a new job, ignore all its dependencies]' \
321     {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \
322     {-q,--quiet}'[Suppress output]' \
323     '--no-block[Do not wait until operation finished]' \
324     '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
325     '--no-pager[Do not pipe output into a pager]' \
326     '--system[Connect to system manager]' \
327     '--user[Connect to user service manager]' \
328     "--no-wall[Don't send wall message before halt/power-off/reboot]" \
329     '--global[Enable/disable unit files globally]' \
330     "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
331     '--no-ask-password[Do not ask for system passwords]' \
332     '--kill-who=[Who to send signal to]:killwho:(main control all)' \
333     {-s+,--signal=}'[Which signal to send]:signal:_signals' \
334     {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
335     '--root=[Enable unit files in the specified root directory]:directory:_directories' \
336     '--runtime[Enable unit files only temporarily until next reboot]' \
337     {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
338     {-P,--privileged}'[Acquire privileges before execution]' \
339     {-n+,--lines=}'[Journal entries to show]:number of entries' \
340     {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
341     '--plain[When used with list-dependencies, print output as a list]' \
342     '*::systemctl command:_systemctl_command'