48 |
- |
1 |
# main function bound to scl command
|
|
|
2 |
_scl()
|
|
|
3 |
{
|
|
|
4 |
local cur prev opts
|
|
|
5 |
COMPREPLY=()
|
|
|
6 |
|
|
|
7 |
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
8 |
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
9 |
opts="-l --list"
|
|
|
10 |
|
|
|
11 |
# handle options
|
|
|
12 |
if [[ ${cur} == -* ]] ; then
|
|
|
13 |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
14 |
return 0
|
|
|
15 |
fi
|
|
|
16 |
|
|
|
17 |
local collections=($(find /etc/scl/prefixes -maxdepth 1 -mindepth 1 -type f -exec basename {} \; | sort -u))
|
|
|
18 |
|
|
|
19 |
# handle scriptlets; the first parameter must be a scriptlet if it is not an option
|
|
|
20 |
if ((COMP_CWORD == 1)); then
|
|
|
21 |
# get array of scriptlets found throughout collections
|
|
|
22 |
local scriptlets=()
|
|
|
23 |
for col in ${collections[@]}; do
|
|
|
24 |
local prefix=`cat /etc/scl/prefixes/$col`
|
|
|
25 |
scriptlets+=($(find $prefix/$col/* -maxdepth 1 -type f -exec basename {} \; | sort -u))
|
|
|
26 |
done
|
|
|
27 |
scriptlets_str=`echo ${scriptlets[@]} | sed 's/ /\n/g'| sort -u`
|
|
|
28 |
COMPREPLY=( $(compgen -W "$scriptlets_str register deregister" -- ${cur}) )
|
|
|
29 |
return 0
|
|
|
30 |
fi
|
|
|
31 |
|
|
|
32 |
# handle commands; does not handle commands without single or double quotes
|
|
|
33 |
if [[ ${cur} == \'* || ${cur} == \"* ]] ; then
|
|
|
34 |
# it is a command do not reply with anything
|
|
|
35 |
return 0
|
|
|
36 |
fi
|
|
|
37 |
|
|
|
38 |
# handle collections; if it is not an option or a command, it must be a collection
|
|
|
39 |
if [ $prev == "register" ]; then
|
|
|
40 |
compopt -o nospace
|
|
|
41 |
COMPREPLY=( $(compgen -A directory ${cur}) )
|
|
|
42 |
return 0
|
|
|
43 |
fi
|
|
|
44 |
COMPREPLY=( $(compgen -W "${collections[*]}" -- ${cur}) )
|
|
|
45 |
return 0
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
# bind the scl command to the _scl function for completion
|
|
|
49 |
complete -F _scl scl
|