chiark / gitweb /
Fix fdroid import 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_rewritemeta() {
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_lint() {
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_scanner() {
204         opts="-h -v"
205         lopts="--help --verbose --nosvn"
206         case "${cur}" in
207                 -*)
208                         __complete_options
209                         return 0;;
210                 *:)
211                         __vercode
212                         return 0;;
213                 *)
214                         __package
215                         return 0;;
216         esac
217 }
218
219 __complete_verify() {
220         opts="-h -v -p"
221         lopts="--help --verbose"
222         case "${cur}" in
223                 -*)
224                         __complete_options
225                         return 0;;
226                 *:)
227                         __vercode
228                         return 0;;
229                 *)
230                         __package
231                         return 0;;
232         esac
233 }
234
235 __complete_stats() {
236         opts="-h -v -d"
237         lopts="--help --verbose --download"
238         __complete_options
239 }
240
241 __complete_server() {
242         opts="-h -v"
243         lopts="--help --verbose update"
244         __complete_options
245 }
246
247 __complete_init() {
248         opts="-h -v -d"
249         lopts="--help --verbose --keystore --distinguished-name --repo-keyalias"
250         __complete_options
251 }
252
253 _fdroid() {
254         local cmd cmds
255         cmd=${COMP_WORDS[1]}
256         cmds=" build init install update publish checkupdates import \
257 rewritemeta lint scanner verify stats server "
258
259         for c in $cmds; do eval "_fdroid_${c} () {
260                 local cur prev opts lopts
261                 __fdroid_init ${c};
262         }"; done
263
264         [[ $cmds == *\ $cmd\ * ]] && _fdroid_${cmd} || {
265                 (($COMP_CWORD == 1)) && COMPREPLY=( $( compgen -W "${cmds}" -- $cmd ) )
266         }
267 }
268
269 _fd-commit() {
270         __package
271 }
272
273 complete -F _fdroid fdroid
274 complete -F _fd-commit fd-commit
275
276 return 0