-
Notifications
You must be signed in to change notification settings - Fork 4
/
completion.sh
247 lines (200 loc) · 5.55 KB
/
completion.sh
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
__sider_engines="redis mariadb postgres mongodb"
# Returns the .siderrc (or default) basePath
__sider_get_base_path() {
basePath=~/.sider
if [ -e ~/.siderrc ]; then
basePath=$(cat ~/.siderrc | grep basePath | sed -E 's/\s*"basePath":\s+"(.+)"$/\1/')
fi
}
# Returns the array in the variable result
__sider_get_completions_for() {
local type=$1
local basePath
__sider_get_base_path
# TODO If we don't find any files, do exit
local typePath="${basePath/#\~/$HOME}/$type/*/specs.json"
local files=$(find $typePath -type f -prune -printf "%h\n" 2> /dev/null)
if [ -z "$files" ]; then
result=""
return 0
fi
result=$(xargs -I% basename % 2> /dev/null <<<$files)
}
__sider_engine() {
local argv=("$@")
if [ "${#argv[@]}" = 3 ]; then
return 0
fi
if [ "${#argv[@]}" = 1 ]; then
COMPREPLY=( $(compgen -W "setconf getconf remconf help" -- "${argv[0]}") )
return 0
fi
local subcommand="${argv[0]}"
case $subcommand in
help)
COMPREPLY=( $(compgen -W "setconf getconf remconf" -- "${argv[1]}") )
;;
*)
COMPREPLY=( $(compgen -W "-h --help ${__sider_engines}" -- "${argv[1]}") )
;;
esac
}
__sider_snapshot() {
local argv=("$@")
local argvlen="${#argv[@]}"
if [ $argvlen = 1 ]; then
COMPREPLY=( $(compgen -W "add empty list mv remove getconf setconf remconf help" -- "${argv[0]}") )
return 0
fi
local subcommand="${argv[0]}"
case $subcommand in
help)
if [ $argvlen = 3 ]; then
return 0
fi
COMPREPLY=( $(compgen -W "add empty list mv remove getconf setconf remconf" -- "${argv[1]}") )
;;
add)
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help ${__sider_engines}" -- "${argv[1]}") )
fi
if [ $argvlen = 4 ]; then
compopt -o default
COMPREPLY=()
return 0
fi
;;
empty)
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help -p ${__sider_engines}" -- "${argv[1]}") )
fi
if [ $argvlen = 3 ] && [ ${argv[1]} = "-p" ]; then
COMPREPLY=( $(compgen -W "${__sider_engines}" -- "${argv[2]}") )
fi
;;
mv)
local result
__sider_get_completions_for "snapshots"
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help ${result}" -- "${argv[1]}") )
fi
;;
list)
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help" -- "${argv[1]}") )
fi
;;
remove|getconf|setconf|remconf)
local result
__sider_get_completions_for "snapshots"
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help ${result}" -- "${argv[1]}") )
fi
;;
esac
}
__sider_db() {
local argv=("$@")
local argvlen="${#argv[@]}"
if [ $argvlen = 1 ]; then
COMPREPLY=( $(compgen -W "clone eject mv getconf setconf remconf list promote remove reset start help" -- "${argv[0]}") )
return 0
fi
local subcommand="${argv[0]}"
case $subcommand in
help)
if [ $argvlen = 3 ]; then
return 0
fi
COMPREPLY=( $(compgen -W "clone eject mv list promote remove reset start getconf setconf remconf" -- "${argv[1]}") )
;;
clone)
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help" -- "${argv[1]}") )
fi
if [ $argvlen = 3 ]; then
local result
__sider_get_completions_for "snapshots"
if [ "$result" = "" ]; then
return 0
fi
COMPREPLY=( $(compgen -W "$result" -- ${argv[2]}) )
fi
;;
start)
local result
__sider_get_completions_for "dbs"
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help -p ${result}" -- "${argv[1]}") )
fi
# TODO This was broken, test it
if [ $argvlen = 3 ] && [ ${argv[1]} = "-p" ]; then
COMPREPLY=( $(compgen -W "${result}" -- "${argv[2]}") )
fi
;;
eject)
local result
__sider_get_completions_for "dbs"
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help ${result}" -- "${argv[1]}") )
fi
if [ $argvlen = 3 ]; then
compopt -o default
COMPREPLY=()
return 0
fi
;;
getconf|setconf|remconf|remove|reset|promote)
local result
__sider_get_completions_for "dbs"
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help ${result}" -- "${argv[1]}") )
fi
;;
list)
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help -s --settings" -- "${argv[1]}") )
fi
;;
mv)
local result
__sider_get_completions_for "dbs"
if [ $argvlen = 2 ]; then
COMPREPLY=( $(compgen -W "-h --help ${result}" -- "${argv[1]}") )
fi
;;
esac
}
__sider() {
local cur prev words cword
_init_completion || return
if [ $prev = "sider" ]; then
COMPREPLY=( $(compgen -W "engine db snapshot help version -V --version" -- $cur) )
return 0
fi;
# Help guard - if any command has -h or --help after it, terminate completion
if [ $prev = '-h' ] || [ $prev = '--help' ]; then
return 0
fi
local second="${words[1]}"
case $second in
help)
COMPREPLY=( $(compgen -W "engine db snapshot" -- $cur) )
;;
db)
__sider_db "${words[@]:2}"
;;
engine)
__sider_engine "${words[@]:2}"
;;
snapshot)
__sider_snapshot "${words[@]:2}"
;;
esac
}
if [ $BASH_VERSION ]; then
complete -F __sider sider
else
2>&1 echo "Completion for this type of shell is not yet supported."
2>&1 echo "Supported shells are: [bash]"
fi