chiark / gitweb /
server: specify an identity file for SSH when rsyncing
[fdroidserver.git] / completion / bash-completion
1 #!/bin/bash
2 #
3 # bash-completion - part of the FDroid server tools
4 # Bash completion for the fdroid main tools
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 v
69         echo $cur | IFS=':' read p v
70
71         COMPREPLY=( $( compgen -P "${p}:" -W "$( while read line; do
72                 if [[ "$line" == "Build Version:"* ]]
73                 then
74                         line="${line#*,}"
75                         printf "${line%%,*} "
76                 elif [[ "$line" == "Build:"* ]]
77                 then
78                         line="${line#*,}"
79                         printf "${line%%,*} "
80                 fi
81                 done < "metadata/${p}.txt" )" -- $cur ) )
82 }
83
84 __complete_options() {
85         case "${cur}" in
86                 --*)
87                         COMPREPLY=( $( compgen -W "${lopts}" -- $cur ) )
88                         return 0;;
89                 *)
90                         COMPREPLY=( $( compgen -W "${opts} ${lopts}" -- $cur ) )
91                         return 0;;
92         esac
93 }
94
95 __complete_build() {
96         opts="-h -v -q -l -s -t -f -a -w"
97
98         lopts="--help --verbose --quiet --latest --stop --test --server --resetserver
99  --on-server --skip-scan --no-tarball --force --all --wiki"
100         case "${cur}" in
101                 -*)
102                         __complete_options
103                         return 0;;
104                 *:*)
105                         __vercode
106                         return 0;;
107                 *)
108                         __package
109                         return 0;;
110         esac
111 }
112
113 __complete_install() {
114         opts="-h -v -q"
115         lopts="--help --verbose --quiet --all"
116         case "${cur}" in
117                 -*)
118                         __complete_options
119                         return 0;;
120                 *:)
121                         __apk_vercode repo
122                         return 0;;
123                 *)
124                         __apk_package repo
125                         return 0;;
126         esac
127 }
128
129 __complete_update() {
130         opts="-h -c -v -q -b -i -I -e -w"
131         lopts="--help --create-metadata --verbose --quiet --buildreport
132  --interactive --icons --editor --wiki --pretty --clean --delete-unknown"
133         case "${prev}" in
134                 -e|--editor)
135                         _filedir
136                         return 0;;
137         esac
138         __complete_options
139 }
140
141 __complete_publish() {
142         opts="-h -v -q"
143         lopts="--help --verbose --quiet"
144         case "${cur}" in
145                 -*)
146                         __complete_options
147                         return 0;;
148                 *:)
149                         __apk_vercode unsigned
150                         return 0;;
151                 *)
152                         __apk_package unsigned
153                         return 0;;
154         esac
155 }
156
157 __complete_checkupdates() {
158         opts="-h -v -q"
159         lopts="--help --verbose --quiet --auto --autoonly --commit --gplay"
160         case "${cur}" in
161                 -*)
162                         __complete_options
163                         return 0;;
164                 *)
165                         __package
166                         return 0;;
167         esac
168 }
169
170 __complete_import() {
171         opts="-h -u -s -r -q"
172         lopts="--help --url --subdir --repo --rev --quiet"
173         case "${prev}" in
174                 -u|--url|-r|--repo|-s|--subdir|--rev) return 0;;
175         esac
176         __complete_options
177 }
178
179 __complete_readmeta() {
180         opts="-h -v -q"
181         lopts="--help --verbose --quiet"
182         __complete_options
183 }
184
185 __complete_rewritemeta() {
186         opts="-h -v -q"
187         lopts="--help --verbose --quiet"
188         case "${cur}" in
189                 -*)
190                         __complete_options
191                         return 0;;
192                 *)
193                         __package
194                         return 0;;
195         esac
196 }
197
198 __complete_lint() {
199         opts="-h -v -q -p"
200         lopts="--help --verbose --quiet --pedantic"
201         case "${cur}" in
202                 -*)
203                         __complete_options
204                         return 0;;
205                 *)
206                         __package
207                         return 0;;
208         esac
209 }
210
211 __complete_scanner() {
212         opts="-h -v -q"
213         lopts="--help --verbose --quiet --nosvn"
214         case "${cur}" in
215                 -*)
216                         __complete_options
217                         return 0;;
218                 *:)
219                         __vercode
220                         return 0;;
221                 *)
222                         __package
223                         return 0;;
224         esac
225 }
226
227 __complete_verify() {
228         opts="-h -v -q -p"
229         lopts="--help --verbose --quiet"
230         case "${cur}" in
231                 -*)
232                         __complete_options
233                         return 0;;
234                 *:)
235                         __vercode
236                         return 0;;
237                 *)
238                         __package
239                         return 0;;
240         esac
241 }
242
243 __complete_stats() {
244         opts="-h -v -q -d"
245         lopts="--help --verbose --quiet --download"
246         __complete_options
247 }
248
249 __complete_server() {
250         opts="-h -i -v -q"
251         lopts="--help --identity-file --verbose --quiet update"
252         __complete_options
253 }
254
255 __complete_init() {
256         opts="-h -v -q -d"
257         lopts="--help --verbose --quiet --keystore --distinguished-name --repo-keyalias"
258         __complete_options
259 }
260
261 _fdroid() {
262         local cmd cmds
263         cmd=${COMP_WORDS[1]}
264         cmds=" build init install update publish checkupdates import \
265 readmeta rewritemeta lint scanner verify stats server "
266
267         for c in $cmds; do eval "_fdroid_${c} () {
268                 local cur prev opts lopts
269                 __fdroid_init ${c};
270         }"; done
271
272         [[ $cmds == *\ $cmd\ * ]] && _fdroid_${cmd} || {
273                 (($COMP_CWORD == 1)) && COMPREPLY=( $( compgen -W "${cmds}" -- $cmd ) )
274         }
275 }
276
277 _fd-commit() {
278         __package
279 }
280
281 complete -F _fdroid fdroid
282 complete -F _fd-commit fd-commit
283
284 return 0