chiark / gitweb /
mount: order remote mounts after both network.target and remote-fs-pre.target
[elogind.git] / src / systemctl-bash-completion.sh
1 # This file is part of systemd.
2 #
3 # Copyright 2010 Ran Benita
4 #
5 # systemd is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # systemd is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
17
18 __systemctl() {
19         systemctl --no-legend "$@"
20 }
21
22 __contains_word () {
23         local word=$1; shift
24         for w in $*; do [[ $w = $word ]] && return 0; done
25         return 1
26 }
27
28 __filter_units_by_property () {
29         local property=$1 value=$2 ; shift ; shift
30         local -a units=( $* )
31         local -a props=( $(__systemctl show --property "$property" -- ${units[*]} | grep -v ^$) )
32         for ((i=0; $i < ${#units[*]}; i++)); do
33                 if [[ "${props[i]}" = "$property=$value" ]]; then
34                         echo "${units[i]}"
35                 fi
36         done
37 }
38
39 __get_all_units      () { __systemctl list-units --full --all | awk '                 {print $1}' ; }
40 __get_active_units   () { __systemctl list-units --full       | awk '                 {print $1}' ; }
41 __get_inactive_units () { __systemctl list-units --full --all | awk '$3 == "inactive" {print $1}' ; }
42 __get_failed_units   () { __systemctl list-units --full       | awk '$3 == "failed"   {print $1}' ; }
43
44 _systemctl () {
45         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
46         local verb comps
47
48         local -A OPTS=(
49                [STANDALONE]='--all -a --defaults --fail --ignore-dependencies --failed --force -f --full --global
50                              --help -h --no-ask-password --no-block --no-pager --no-reload --no-wall
51                              --order --require --quiet -q --privileged -P --system --user --version'
52                       [ARG]='--host -H --kill-mode --kill-who --property -p --signal -s --type -t'
53         )
54
55         if __contains_word "$prev" ${OPTS[ARG]}; then
56                 case $prev in
57                         --signal|-s)
58                                 comps=$(compgen -A signal | grep '^SIG' | grep -Ev 'RTMIN|RTMAX|JUNK')
59                         ;;
60                         --type|-t)
61                                 comps='automount device mount path service snapshot socket swap target timer'
62                         ;;
63                         --kill-who)
64                                 comps='all control main'
65                         ;;
66                         --kill-mode)
67                                 comps='control-group process'
68                         ;;
69                         --property|-p|--host|-H)
70                                 comps=''
71                         ;;
72                 esac
73                 COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
74                 return 0
75         fi
76
77
78         if [[ "$cur" = -* ]]; then
79                 COMPREPLY=( $(compgen -W "${OPTS[*]}" -- "$cur") )
80                 return 0
81         fi
82
83         local -A VERBS=(
84                 [ALL_UNITS]='enable disable is-active is-enabled status show'
85              [FAILED_UNITS]='reset-failed'
86           [STARTABLE_UNITS]='start'
87           [STOPPABLE_UNITS]='stop kill try-restart condrestart'
88          [ISOLATABLE_UNITS]='isolate'
89          [RELOADABLE_UNITS]='reload reload-or-try-restart force-reload'
90           [RESTARTABLE_UNITS]='restart reload-or-restart'
91                      [JOBS]='cancel'
92                 [SNAPSHOTS]='delete'
93                      [ENVS]='set-environment unset-environment'
94                [STANDALONE]='daemon-reexec daemon-reload default dot dump emergency exit halt kexec
95                              list-jobs list-units poweroff reboot rescue show-environment'
96                      [NAME]='snapshot load'
97         )
98
99         local verb
100         for ((i=0; $i <= $COMP_CWORD; i++)); do
101                 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
102                  ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG}]}; then
103                         verb=${COMP_WORDS[i]}
104                         break
105                 fi
106         done
107
108         if   [[ -z $verb ]]; then
109                 comps="${VERBS[*]}"
110
111         elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
112                 comps=$( __get_all_units )
113
114         elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
115                 comps=$( __filter_units_by_property CanStart yes \
116                       $( __get_inactive_units | grep -Ev '\.(device|snapshot)$' ))
117
118         elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
119                 comps=$( __filter_units_by_property CanStart yes \
120                       $( __get_all_units | grep -Ev '\.(device|snapshot|socket|timer)$' ))
121
122         elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
123                 comps=$( __filter_units_by_property CanStop yes \
124                       $( __get_active_units ) )
125
126         elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
127                 comps=$( __filter_units_by_property CanReload yes \
128                       $( __get_active_units ) )
129
130         elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
131                 comps=$( __filter_units_by_property AllowIsolate yes \
132                       $( __get_all_units ) )
133
134         elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
135                 comps=$( __get_failed_units )
136
137         elif __contains_word "$verb" ${VERBS[STANDALONE]} ${VERBS[NAME]}; then
138                 comps=''
139
140         elif __contains_word "$verb" ${VERBS[JOBS]}; then
141                 comps=$( __systemctl list-jobs | awk '{print $1}' )
142
143         elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
144                 comps=$( __systemctl list-units --type snapshot --full --all | awk '{print $1}' )
145
146         elif __contains_word "$verb" ${VERBS[ENVS]}; then
147                 comps=$( __systemctl show-environment | sed 's_\([^=]\+=\).*_\1_' )
148                 compopt -o nospace
149         fi
150
151         COMPREPLY=( $(compgen -W "$comps" -- "$cur") )
152         return 0
153 }
154 complete -F _systemctl systemctl