forked from dtolabs/rerun-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_completion.sh
250 lines (223 loc) · 7.35 KB
/
bash_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
248
249
250
#
# rerun command completion script.
# **rerun** is a simple, small modular automation
# framework based on Bash, the POSIX shell.
#
# Installation
# -------------
#
# #. Download or git clone from [rerun](http://github.com/rerun/rerun).
# #. Source this file from your .bashrc.
#
# Usage
# -----
#
# The contained completion support provides for:
#
# * Module listing:
# `rerun [tab][tab]`
# * Command listing for specified module:
# `rerun module:[tab][tab]`
# * Option listing for specified module:command:
# `rerun module: command[tab][tab]`
# * Arguments for specified option:
# `rerun module: command --file[tab][tab]`
#
# @author: <a href="mailto:alex@dtosolutions.com">alex@dtosolutions.com</a>
[ -n "${RERUN_MODULES}" -a -d "${RERUN_MODULES}" ] || {
export RERUN_MODULES=$(pwd)/modules
}
#
# Shell functions to support the command completion
#
# list:member - check if item is contained in list
list:member()
{
local item="$1" list="$2"
for member in $(eval echo $list)
do
[ "${item}" = "${member}" ] && return 0
done
return 1
}
# list:subtract - subtract list2 members from list1
list:subtract()
{
local list1="$1" list2="$2" retlist=""
for item in $(eval echo $list1)
do
list:member $item "$list2" || retlist="$retlist $item"
done
echo $retlist
}
# rerun:modules - list all rerun modules
rerun:modules()
{
local modules=""
for mod in $RERUN_MODULES/*
do
[ -f "$mod/metadata" ] && modules="$modules $(basename $mod)"
done
echo $modules
}
# rerun:module:list - list all the commands for the module
rerun:module:commands()
{
local module=$1 commands=""
for hdlr in $RERUN_MODULES/$module/commands/*/metadata; do
[ -f $hdlr ] && {
cmd_name=$(basename $(dirname $hdlr))
commands="$commands $cmd_name"
}
done
echo $commands
}
# rerun:command:options - List all the registered options for the command
rerun:command:options()
{
local module=$1 command=$2 prefix=$3 options=""
for opt in $(. $RERUN_MODULES/$module/commands/$command/metadata; echo $OPTIONS)
do
options="$options ${prefix}${opt}"
done
echo $options
}
# rerun:option:default - get the default for the specified option
rerun:option:default()
{
local module=$1 command=$2 opt=$3
local opt_metadata=$RERUN_MODULES/$module/options/${opt##*-}/metadata
[ -f "$opt_metadata" ] && {
awk -F= '/^DEFAULT/ {print $2}' "$opt_metadata"
}
}
# rerun:option:has-argument - check if option takes an argument
rerun:option:has-argument()
{
local module=$1 command=$2 opt=$3
local opt_metadata=$RERUN_MODULES/$module/options/${opt##*-}/metadata
[ -f "$opt_metadata" ] && {
args=$(awk -F= '/^ARGUMENTS/ {print $2}' $opt_metadata )
[ "$args" = "true" ] && return 0
}
return 1
}
# rerun:options:remaining - list remaining options
rerun:options:remaining()
{
local argline=$1 options=$2 used=""
for arg in $argline; do
[[ "$arg" == -* ]] && used="$used ${arg}"
done
list:subtract "$options" "$used"
}
rerun:parse:module()
{
local cmdline=$@
local module
local regex="[ ]+--module[ ]([[:alnum:]]+)[ ]*"
if [[ "$cmdline" =~ $regex ]]
then
module=${BASH_REMATCH[1]}
fi
echo $module
}
#
# _rerun - program completion for the `rerun` command.
#
_rerun() {
[ -z "${RERUN_MODULES}" -o ! \( -d "${RERUN_MODULES}" \) ] && {
return 0 ;
}
local cur prev cntx_module cntx_command cntx_options options
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
eval set $COMP_LINE
shift; # shift once to drop the "rerun" from the argument string
# Define regex pattern to parse command line input
# module:command --optionA arg --optionB arg ...
regex='([^:]+)([:]?[ ]?)([A-Za-z0-9_-]*)([ ]*)(.*)'
if [[ "$@" =~ $regex ]]
then
# module context
[ -d "$RERUN_MODULES/${BASH_REMATCH[1]}" ] && cntx_module=${BASH_REMATCH[1]};
[ "${BASH_REMATCH[2]}" == ': ' ] && shift ;# eat the extra space char
# command context
[ -d "$RERUN_MODULES/$cntx_module/commands/${BASH_REMATCH[3]/ /}" ] && {
cntx_command=${BASH_REMATCH[3]/ /}
}
# BASH_REMATCH[4] contains the whitespace between command and options
# option context
cntx_options=${BASH_REMATCH[5]};
fi
# Shift over to the command options
shift;
# Complete commands given the user shell input.
# Just the rerun command was typed. List modules
[ -z "$cntx_module" ] && {
local modules=$(rerun:modules $RERUN_MODULES)
COMPREPLY=( $(compgen -W "$modules" -S ':' -o nospace -- ${cur}) )
return 0
}
# Module specified: List module's commands
[ -n "$cntx_module" -a -z "$cntx_command" ] && {
local commands=$(rerun:module:commands ${cntx_module})
COMPREPLY=( $(compgen -W "$commands" -- ${cur}) )
return 0
}
# Command specified. List command's options
options=$(rerun:command:options ${cntx_module} ${cntx_command} "--")
if [ -n "$cntx_command" -a -z "$cntx_options" ]; then
COMPREPLY=( $(compgen -W "$options" -- "${cur}") )
return 0
fi
# Option(s) specified. Show possible arguments or remaining option choices.
if [ -n "$cntx_options" ]; then
if [[ $prev == -* ]]; then
# check if current option takes an argument ...
if rerun:option:has-argument ${cntx_module} ${cntx_command} ${prev}; then
# ... and has a default value
local default=$(rerun:option:default ${cntx_module} ${cntx_command} ${prev})
if [ -n "$default" ]; then
# print the default value
COMPREPLY=( $(compgen -W "$default" -- ${cur}) )
return 0
else
# ... or wants option specific completion
case "$prev" in
--file*|--out*|--xml|--template)
# file completion
COMPREPLY=( $(compgen -o filenames -A file -- ${cur}) ) ;;
--*dir*|--logs*)
# directory completion
COMPREPLY=( $(compgen -o dirnames -A directory -- ${cur}) ) ;;
--module)
# module completion
modules=$(rerun:modules)
COMPREPLY=( $(compgen -W "$modules" -- ${cur}) ) ;;
--command)
# command completion
module=$(rerun:parse:module ${COMP_WORDS[*]} )
[ -n "$module" ] && {
commands=$(rerun:module:commands ${module})
COMPREPLY=( $(compgen -W "$commands" -- ${cur}) )
}
;;
esac
return 0
fi
fi
else
# Show the remaining/unused option choices
remaining=$(rerun:options:remaining "$cntx_options" "$options")
COMPREPLY=( $(compgen -W "$remaining" -- ${cur}) )
fi
return 0
fi
}
# register the _rerun completion function
complete -F _rerun rerun
#
# This is Free Software distributed under the Apache 2 license.
: