chiark / gitweb /
Add readmeta completion
[fdroidserver.git] / completion / bash-completion
1 #!/bin/bash
2 #
3 # bash-completion - part of the FDroid server tools
4 # Commits updates to apps, allowing you to edit the commit messages
5 #
6 # Copyright (C) 2013, 2014 Daniel Martí <mvdan@mvdan.cc>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Affero General Public License for more details.
17 #
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 # 'fdroid' is completed automatically, but aliases to it are not.
22 # For instance, to alias 'fd' to 'fdroid' and have competion available:
23 #
24 #       alias fd='fdroid'
25 #       complete -F _fdroid fd
26 #
27 # One can use completion on aliased subcommands as follows:
28 #
29 #       alias fbuild='fdroid build'
30 #       complete -F _fdroid_build fbuild
31
32 __fdroid_init() {
33         COMPREPLY=()
34         cur="${COMP_WORDS[COMP_CWORD]}"
35         prev="${COMP_WORDS[COMP_CWORD-1]}"
36
37         (( $# >= 1 )) && __complete_${1}
38 }
39
40 __package() {
41         files=( metadata/*.txt )
42         files=( ${files[@]#metadata/} )
43         files=${files[@]%.txt}
44         COMPREPLY=( $( compgen -W "$files" -- $cur ) )
45 }
46
47 __apk_package() {
48         files=( ${1}/*.apk )
49         [ -f "${files[0]}" ] || return
50
51         files=( ${files[@]#*/} )
52         files=${files[@]%_*}
53         COMPREPLY=( $( compgen -W "$files" -- $cur ) )
54 }
55
56 __apk_vercode() {
57         local p=${cur:0:-1}
58
59         files=( ${1}/${p}_*.apk )
60         [ -f "${files[0]}" ] || return
61
62         files=( ${files[@]#*_} )
63         files=${files[@]%.apk}
64         COMPREPLY=( $( compgen -P "${p}:" -W "$files" -- $cur ) )
65 }
66
67 __vercode() {
68         local p=${cur:0:-1}
69
70         COMPREPLY=( $( compgen -P "${p}:" -W "$( while read line; do
71                 if [[ "$line" == "Build Version:"* ]]
72                 then
73                         line="${line#*,}"
74                         printf "${line%%,*} "
75                 elif [[ "$line" == "Build:"* ]]
76                 then
77                         line="${line#*,}"
78                         printf "${line%%,*} "
79                 fi
80                 done < "metadata/${p}.txt" )" -- $cur ) )
81 }
82
83 __complete_options() {
84         case "${cur}" in
85                 --*)
86                         COMPREPLY=( $( compgen -W "${lopts}" -- $cur ) )
87                         return 0;;
88                 *)
89                         COMPREPLY=( $( compgen -W "${opts} ${lopts}" -- $cur ) )
90                         return 0;;
91         esac
92 }
93
94 __complete_build() {
95         opts="-h -v -c -l -s -t -f"
96         lopts="--help --verbose --latest --server --resetserver --on-server
97  --force --all"
98         case "${cur}" in
99                 -*)
100                         __complete_options
101                         return 0;;
102                 *:)
103                         __vercode
104                         return 0;;
105                 *)
106                         __package
107                         return 0;;
108         esac
109 }
110
111 __complete_install() {
112         opts="-h -v"
113         lopts="--help --verbose --all"
114         case "${cur}" in
115                 -*)
116                         __complete_options
117                         return 0;;
118                 *:)
119                         __apk_vercode repo
120                         return 0;;
121                 *)
122                         __apk_package repo
123                         return 0;;
124         esac
125 }
126
127 __complete_update() {
128         opts="-h -c -v -q -b -i -I -e -w"
129         lopts="--help --createmeta --verbose --quiet --buildreport --interactive
130  --icons --editor --wiki --pretty --clean"
131         case "${prev}" in
132                 -e|--editor)
133                         _filedir
134                         return 0;;
135         esac
136         __complete_options
137 }
138
139 __complete_publish() {
140         opts="-h -v"
141         lopts="--help --verbose"
142         case "${cur}" in
143                 -*)
144                         __complete_options
145                         return 0;;
146                 *:)
147                         __apk_vercode unsigned
148                         return 0;;
149                 *)
150                         __apk_package unsigned
151                         return 0;;
152         esac
153 }
154
155 __complete_checkupdates() {
156         opts="-h -v"
157         lopts="--help --verbose --auto --autoonly --commit --gplay"
158         case "${cur}" in
159                 -*)
160                         __complete_options
161                         return 0;;
162                 *)
163                         __package
164                         return 0;;
165         esac
166 }
167
168 __complete_import() {
169         opts="-h -u -s -r"
170         lopts="--help --url --subdir --repo"
171         case "${prev}" in
172                 -u|--url|-r|--repo|-s|--subdir) return 0;;
173         esac
174         __complete_options
175 }
176
177 __complete_readmeta() {
178         opts="-h -v"
179         lopts="--help --verbose"
180         case "${cur}" in
181                 -*)
182                         __complete_options
183                         return 0;;
184                 *)
185                         __package
186                         return 0;;
187         esac
188 }
189
190 __complete_rewritemeta() {
191         opts="-h -v"
192         lopts="--help --verbose"
193         case "${cur}" in
194                 -*)
195                         __complete_options
196                         return 0;;
197                 *)
198                         __package
199                         return 0;;
200         esac
201 }
202
203 __complete_lint() {
204         opts="-h -v"
205         lopts="--help --verbose"
206         case "${cur}" in
207                 -*)
208                         __complete_options
209                         return 0;;
210                 *)
211                         __package
212                         return 0;;
213         esac
214 }
215
216 __complete_scanner() {
217         opts="-h -v"
218         lopts="--help --verbose --nosvn"
219         case "${cur}" in
220                 -*)
221                         __complete_options
222                         return 0;;
223                 *:)
224                         __vercode
225                         return 0;;
226                 *)
227                         __package
228                         return 0;;
229         esac
230 }
231
232 __complete_verify() {
233         opts="-h -v -p"
234         lopts="--help --verbose"
235         case "${cur}" in
236                 -*)
237                         __complete_options
238                         return 0;;
239                 *:)
240                         __vercode
241                         return 0;;
242                 *)
243                         __package
244                         return 0;;
245         esac
246 }
247
248 __complete_stats() {
249         opts="-h -v -d"
250         lopts="--help --verbose --download"
251         __complete_options
252 }
253
254 __complete_server() {
255         opts="-h -v"
256         lopts="--help --verbose update"
257         __complete_options
258 }
259
260 __complete_init() {
261         opts="-h -v -d"
262         lopts="--help --verbose --keystore --distinguished-name --repo-keyalias"
263         __complete_options
264 }
265
266 _fdroid() {
267         local cmd cmds
268         cmd=${COMP_WORDS[1]}
269         cmds=" build init install update publish checkupdates import \
270 readmeta rewritemeta lint scanner verify stats server "
271
272         for c in $cmds; do eval "_fdroid_${c} () {
273                 local cur prev opts lopts
274                 __fdroid_init ${c};
275         }"; done
276
277         [[ $cmds == *\ $cmd\ * ]] && _fdroid_${cmd} || {
278                 (($COMP_CWORD == 1)) && COMPREPLY=( $( compgen -W "${cmds}" -- $cmd ) )
279         }
280 }
281
282 _fd-commit() {
283         __package
284 }
285
286 complete -F _fdroid fdroid
287 complete -F _fd-commit fd-commit
288
289 return 0