chiark / gitweb /
Separate lint messages into warnings and pedantic warnings
[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=${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  --skip-scan --no-tarball --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         __complete_options
181 }
182
183 __complete_rewritemeta() {
184         opts="-h -v"
185         lopts="--help --verbose"
186         case "${cur}" in
187                 -*)
188                         __complete_options
189                         return 0;;
190                 *)
191                         __package
192                         return 0;;
193         esac
194 }
195
196 __complete_lint() {
197         opts="-h -v -p"
198         lopts="--help --verbose --pedantic"
199         case "${cur}" in
200                 -*)
201                         __complete_options
202                         return 0;;
203                 *)
204                         __package
205                         return 0;;
206         esac
207 }
208
209 __complete_scanner() {
210         opts="-h -v"
211         lopts="--help --verbose --nosvn"
212         case "${cur}" in
213                 -*)
214                         __complete_options
215                         return 0;;
216                 *:)
217                         __vercode
218                         return 0;;
219                 *)
220                         __package
221                         return 0;;
222         esac
223 }
224
225 __complete_verify() {
226         opts="-h -v -p"
227         lopts="--help --verbose"
228         case "${cur}" in
229                 -*)
230                         __complete_options
231                         return 0;;
232                 *:)
233                         __vercode
234                         return 0;;
235                 *)
236                         __package
237                         return 0;;
238         esac
239 }
240
241 __complete_stats() {
242         opts="-h -v -d"
243         lopts="--help --verbose --download"
244         __complete_options
245 }
246
247 __complete_server() {
248         opts="-h -v"
249         lopts="--help --verbose update"
250         __complete_options
251 }
252
253 __complete_init() {
254         opts="-h -v -d"
255         lopts="--help --verbose --keystore --distinguished-name --repo-keyalias"
256         __complete_options
257 }
258
259 _fdroid() {
260         local cmd cmds
261         cmd=${COMP_WORDS[1]}
262         cmds=" build init install update publish checkupdates import \
263 readmeta rewritemeta lint scanner verify stats server "
264
265         for c in $cmds; do eval "_fdroid_${c} () {
266                 local cur prev opts lopts
267                 __fdroid_init ${c};
268         }"; done
269
270         [[ $cmds == *\ $cmd\ * ]] && _fdroid_${cmd} || {
271                 (($COMP_CWORD == 1)) && COMPREPLY=( $( compgen -W "${cmds}" -- $cmd ) )
272         }
273 }
274
275 _fd-commit() {
276         __package
277 }
278
279 complete -F _fdroid fdroid
280 complete -F _fd-commit fd-commit
281
282 return 0