Add mdk tracker bash completion
[mdk.git] / extra / bash_completion
1 #
2 # Moodle Development Kit
3 #
4 # Copyright (c) 2012 Frédéric Massart - FMCorz.net
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 # http://github.com/FMCorz/mdk
20 #
21
22 # This file defines the functions for Bash completion.
23 #
24 # To install on Ubuntu, link this file into /etc/bash_completion.d
25 # ln -s bash_completion /etc/bash_completion.d/mdk
26 # source /etc/bash_completion.d/mdk
27
28 function _mdk() {
29 local BIN CMD CUR PREV OPTS
30 COMPREPLY=()
31 BIN="${COMP_WORDS[0]}"
32 CUR="${COMP_WORDS[COMP_CWORD]}"
33 OPTS=""
34
35 # Helper to list the scripts available to `mdk run`.
36 function _list_scripts() {
37 local SCRIPTS
38 SCRIPTS=$($BIN run --list 2> /dev/null | cut -d ' ' -f 1)
39 echo $SCRIPTS
40 }
41
42 # Helper to list the instances.
43 function _list_instances() {
44 local INSTANCES
45 INSTANCES=$($BIN info -ln 2> /dev/null)
46 echo $INSTANCES
47 }
48
49 if [[ "${COMP_CWORD}" == 1 ]]; then
50 # List the commands and aliases.
51 # Ignoring these commands on purpose: init
52 OPTS="alias backport backup behat config create css doctor fix info install js phpunit plugin purge pull push rebase remove run tracker uninstall update upgrade"
53 OPTS="$OPTS $($BIN alias list 2> /dev/null | cut -d ':' -f 1)"
54 else
55 # List of options according to the command.
56 CMD="${COMP_WORDS[1]}"
57 PREV="${COMP_WORDS[COMP_CWORD-1]}"
58 case "${CMD}" in
59 alias)
60 if [[ "${COMP_CWORD}" == 2 ]]; then
61 OPTS="list show add remove set"
62 elif [[ "${COMP_CWORD}" == 3 && "${PREV}" != "add" && "${PREV}" != "list" ]]; then
63 OPTS="$($BIN alias list 2> /dev/null | cut -d ':' -f 1)"
64 fi
65 ;;
66 backport)
67 if [[ "$CUR" == -* ]]; then
68 OPTS="--branch --remote --integration --dont-push --dont-fetch --patch --push --push-to --force-push --versions --update-tracker"
69 else
70 OPTS="-- $(_list_instances)"
71 fi
72 ;;
73 backup)
74 OPTS="--list --info --restore"
75 if [[ "${COMP_CWORD}" == 3 && ("${PREV}" == "--restore" || "${PREV}" == "--info") ]]; then
76 OPTS="$($BIN backup --list 2> /dev/null | cut -d ' ' -f 1)"
77 elif [[ "${COMP_CWORD}" == 2 && "$CUR" != -* ]]; then
78 OPTS="$OPTS $(_list_instances)"
79 fi
80 ;;
81 behat)
82 if [[ "${PREV}" == "--feature" ]] || [[ "${PREV}" == "-f" ]]; then
83 # Basic autocomplete for --feature, should append a / at the end of directory names.
84 compopt +o nospace
85 OPTS="$(compgen -A file $CUR)"
86 compopt -o nospace
87 else
88 OPTS="--run --disable --force --feature --switch-completely --no-javascript --selenium --selenium-download --selenium-verbose --tags"
89 OPTS="$OPTS $(_list_instances)"
90 fi
91 ;;
92 config)
93 if [[ "${COMP_CWORD}" == 2 ]]; then
94 OPTS="flatlist list show set"
95 elif [[ "${COMP_CWORD}" == 3 && ("${PREV}" == "set" || "${PREV}" == "show") ]]; then
96 OPTS="$($BIN config flatlist 2> /dev/null | cut -d ':' -f 1)"
97 fi
98 ;;
99 create)
100 OPTS="--identifier --integration --install --run --version --suffix --engine"
101 if [[ "$PREV" == "--engine" ]]; then
102 OPTS='mariadb mysqli pgsql'
103 elif [[ "$PREV" == "--run" ]]; then
104 OPTS="$(_list_scripts)"
105 fi
106 ;;
107 css)
108 OPTS="--compile --debug --sheets --theme --watch"
109 ;;
110 doctor)
111 OPTS="--all --branch --cached --dependencies --directories --masterbranch --remotes --symlink --wwwroot"
112 ;;
113 fix)
114 if [[ "$PREV" == "-n" || "$PREV" == "--name" ]]; then
115 OPTS=`$BIN info -ln`
116 else
117 OPTS="--autofix --name"
118 fi
119 ;;
120 info)
121 OPTS="--list --var --edit"
122 if [[ "${COMP_CWORD}" == 2 && "$CUR" != -* ]]; then
123 OPTS="$OPTS $(_list_instances)"
124 elif [[ "${COMP_CWORD}" == 3 && ("$PREV" == "--list" || "$PREV" == "-l") ]]; then
125 OPTS="--integration --stable --names-only"
126 fi
127 ;;
128 install)
129 OPTS="--engine --fullname --run"
130 case "$PREV" in
131 -e|--engine)
132 OPTS="mariadb mysqli pgsql"
133 ;;
134 -r|--run)
135 OPTS="$(_list_scripts)"
136 ;;
137 *)
138 if [[ "$CUR" != -* ]]; then
139 OPTS="$OPTS $(_list_instances)"
140 fi
141 ;;
142 esac
143 ;;
144 js)
145 OPTS="--plugin --module"
146 case "$PREV" in
147 js)
148 OPTS="doc shift"
149 ;;
150 doc)
151 OPTS="$(_list_instances)"
152 ;;
153 *)
154 if [[ "$CUR" != -* ]]; then
155 OPTS="$OPTS $(_list_instances)"
156 fi
157 ;;
158 esac
159 ;;
160 plugin)
161 if [[ "${COMP_CWORD}" == 2 ]]; then
162 OPTS="download install"
163 else
164 OPTS="--force --strict --no-cache"
165 OPTS="$OPTS $(_list_instances)"
166 fi
167 ;;
168 phpunit)
169 if [[ "${PREV}" == "--unittest" ]] || [[ "${PREV}" == "-u" ]]; then
170 # Basic autocomplete for --unittest, should append a / at the end of directory names.
171 compopt +o nospace
172 OPTS="$(compgen -A file $CUR)"
173 compopt -o nospace
174 else
175 OPTS="--force --run --testcase --unittest --filter --coverage --testsuite"
176 OPTS="$OPTS $(_list_instances)"
177 fi
178 ;;
179 purge)
180 OPTS="--all --integration --stable --manual"
181 if [[ "$CUR" != -* ]]; then
182 OPTS="$OPTS $(_list_instances)"
183 fi
184 ;;
185 pull)
186 if [[ "${PREV}" == "-m" ]] || "${PREV}" == "--mode"; then
187 OPTS="checkout fetch integration pull testing"
188 else
189 OPTS="--fetch-only --integration --no-merge --testing --mode --prompt"
190 fi
191 ;;
192 push)
193 if [[ "$PREV" != "--branch" && "$PREV" != "-b" && "$PREV" != "--remote" && "$PREV" != "-r" ]]; then
194 OPTS="--branch --remote --force --include-stable --force-stable --patch --update-tracker"
195 if [[ "$CUR" != -* ]]; then
196 OPTS="$OPTS $(_list_instances)"
197 fi
198 fi
199 ;;
200 rebase)
201 if [[ "$PREV" == "rebase" || "$PREV" == "-p" || "$PREV" == "--push" || "$PREV" == "-f" || "$PREV" == "--force-push" ]]; then
202 OPTS="--issues --suffix --versions --push --remote --force-push --update-tracker"
203 if [[ "$CUR" != -* ]]; then
204 OPTS="$OPTS $(_list_instances)"
205 fi
206 fi
207 ;;
208 remove)
209 OPTS="$OPTS $(_list_instances)"
210 ;;
211 run)
212 if [[ "${COMP_CWORD}" == 2 ]]; then
213 OPTS="$(_list_scripts)"
214 elif [[ "${COMP_CWORD}" == 3 ]]; then
215 OPTS="--integration --stable --all --list $(_list_instances)"
216 fi
217 ;;
218 tracker)
219 OPTS="--testing"
220 ;;
221 update)
222 OPTS="--integration --stable --all --upgrade --update-cache"
223 if [[ "$CUR" != -* ]]; then
224 OPTS="$OPTS $(_list_instances)"
225 fi
226 ;;
227 upgrade)
228 OPTS="--integration --stable --all --updade --no-checkout"
229 if [[ "$CUR" != -* ]]; then
230 OPTS="$OPTS $(_list_instances)"
231 fi
232 ;;
233 *)
234 ;;
235 esac
236 fi
237
238 COMPREPLY=( $(compgen -W "${OPTS}" -- ${CUR}) )
239 return 0
240 }
241
242 # MDK autocomplete
243 if [[ -n "$(which mdk)" ]]; then
244 complete -F _mdk mdk
245 fi