forked from appmgr/appmgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_completion
123 lines (108 loc) · 2.39 KB
/
app_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
#!/bin/bash
_appmgr_find() {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && echo $1 && return; done
}
_appmgr_method_groups=(
"instance"
"conf"
"operate"
)
_appmgr_methods=(
"install"
)
_appmgr_resolvers=(
"file"
"maven"
)
_appmgr_parse_opts() {
local prev=""
local has_method
for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
local curr=${COMP_WORDS[$i]}
if [ "$prev" == -n ]
then
echo "local has_n=$curr"
elif [ "$prev" == -i ]
then
echo "local has_i=$curr"
fi
has_method_group="$has_method_group$(_appmgr_find "$curr" ${_appmgr_method_groups[@]})"
has_method="$has_method$(_appmgr_find "$curr" ${_appmgr_methods[@]})"
prev="${COMP_WORDS[$i]}"
done
echo "local has_method_group='$has_method_group'"
echo "local has_method='$has_method'"
case "${COMP_WORDS[COMP_CWORD]}" in
-*)
echo "local in_option=yes"
;;
esac
}
_complete_appmgr() {
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local opts=""
local x=`_appmgr_parse_opts`
eval "$x"
# set | grep ^has_
if [ $COMP_CWORD == 1 ]
then
opts="${_appmgr_method_groups[@]}"
opts="$opts"
if [ "$in_option" = "yes" ]
then
opts="-n $opts"
fi
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
case "$prev" in
-n)
values=$(./app instance list -P name)
COMPREPLY=($(compgen -W "$values" -- ${cur}))
return 0
;;
-i)
if [ -n "$has_n" ]
then
values=$(./app instance list -n $has_n -P instance)
COMPREPLY=($(compgen -W "$values" -- ${cur}))
return 0
fi
;;
-r)
COMPREPLY="${_appmgr_resolvers[@]}"
COMPREPLY=($(compgen -W "$COMPREPLY" -- ${cur}))
return
;;
-u)
COMPREPLY=($(compgen -o filenames -f -- ${cur}))
return
;;
esac
if [ -n "$has_n" -a -z "$has_i" ]
then
opts="$opts -i"
fi
has_method_group=$has_method_group
has_method=$has_method
case "$has_method_group" in
instance)
if [ -z $has_method ]
then
opts="$opts install list list-versions set-current"
else
case "$has_method" in
install)
opts="$opts -r -u"
;;
esac
fi
;;
esac
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
set +x
}
complete -F _complete_appmgr app