forked from brbsix/debtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debtool-completion
135 lines (100 loc) · 3.66 KB
/
debtool-completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
#
# bash completions for debtool
# ensure bash-completion is installed
[[ $(dpkg-query --show --showformat="\${Status}" 'bash-completion' 2>/dev/null) != 'install ok installed' ]] && return 0
# ensure debtool is on the PATH
_have debtool && {
__debtool(){
local cmd cur lopts options params shopts
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
if [[ $cur = -* || ( -z $cur && $COMP_CWORD -eq 1 ) ]]; then
# add completion for all of debtool's command line options
readarray -t COMPREPLY < \
<( compgen -W '-a --auto -b --build -c --combo -d --download
-f --fast --format -h --help -i --interactive
-m --md5sums -q --quiet -r --reinst --repack
-s --show -u --unpack -z' -- "$cur" )
return 0
fi
# parse options (necessary to break up options like -brq)
shopts='a,b,c,d,f,h,i,m,q,r,s,u,z'
lopts='auto,build,combo,download,fast,format,help,interactive,md5sums,quiet,reinst,repack,show,unpack'
params=$(getopt -o "$shopts" -l "$lopts" -- "${COMP_WORDS[@]::${#COMP_WORDS[@]}-1}" 2>/dev/null)
eval "options=($params)"
cmd=$(__debtool_getcmd)
if [[ -n $cmd ]]; then
case $cmd in
build)
# configure pathname completion
compopt -o filenames &>/dev/null
# include directories (except hidden ones)
readarray -t COMPREPLY < \
<( compgen -d -X '?(*/).*' -- "$cur" )
return 0
;;
download)
# include packages in apt-cache
readarray -t COMPREPLY < \
<( apt-cache --no-generate pkgnames -- "$cur" 2>/dev/null )
return 0
;;
reinst)
# configure pathname completion
compopt -o filenames &>/dev/null
# include directories and debian archives
readarray -t COMPREPLY < <( __debtool_lsdebs "$cur" )
return 0
;;
repack)
# include installed packages
readarray -t COMPREPLY < \
<( _xfunc dpkg _comp_dpkg_installed_packages "$cur" 2>/dev/null )
return 0
;;
unpack)
# configure pathname completion
compopt -o filenames &>/dev/null
# include directories, debian archives, and installed packages
readarray -t COMPREPLY < \
<( __debtool_lsdebs "$cur"
_xfunc dpkg _comp_dpkg_installed_packages "$cur" 2>/dev/null )
return 0
;;
esac
fi
return 0
}
__debtool_contains(){
local arg option
for arg in "$@"; do
for option in "${options[@]}"; do
if [[ $arg = "$option" ]]; then
return 0
fi
done
done
return 1
}
__debtool_getcmd(){
if __debtool_contains '-b' '--build' '--fast' '-z'; then
echo 'build'
elif __debtool_contains '-c' '--combo' '-d' '--download' '-i' '--interactive' '-s' '--show'; then
echo 'download'
elif __debtool_contains '-r' '--reinst'; then
echo 'reinst'
elif __debtool_contains '--repack'; then
echo 'repack'
elif __debtool_contains '-u' '--unpack'; then
echo 'unpack'
fi
}
__debtool_lsdebs(){
# include directories (except hidden ones)
compgen -d -X '?(*/).*' -- "$1"
# include files ending with .deb, .DEB, .udeb, .UDEB
compgen -f -X '!*.@(deb|DEB|udeb|UDEB)' -- "$1"
}
complete -F __debtool debtool
}