-
Notifications
You must be signed in to change notification settings - Fork 0
/
rerun.sh
executable file
·338 lines (283 loc) · 7.86 KB
/
rerun.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/bin/bash
#
# NAME: rerun -- BASH history macro generator
#
# USAGE: rerun <action> [args]
# rerun create <macro name> [history ids] [...]
# rerun list
# rerun list <macro name>
# rerun append <macro name> <history ids> [...]
# rerun insert <macro name> <position> <history ids> [...]
# rerun remove <macro name> <position>
# rerun edit <macro name>
# rerun delete <macro name>
#
# DESCRIPTION:
# This program is used to create macros from commands from
# the BASH history.
#
# If not interactive, assume we were executed and not sourced, therefore
# show instructions on how to get rerun into a bash session.
if [[ -z "$PS1" ]]; then
cmd="source "
if [[ $(which rerun) == "$0" ]]; then #
cmd+="rerun"
else
cmd+="\"$0\""
fi
echo "To install rerun for this bash session, run:"
echo
echo " $cmd"
echo
echo "To install rerun for all your future bash sessions,"
echo "put the above in your ~/.bashrc file."
exit
fi
######################
# Internal functions
#
# Print contents of a file with line numbers
__rerun_ncat() {
local index=0
while read; do
printf "%3d %s\n" $((index++)) "$REPLY"
done <$1
}
# Print contents of a file with line numbers, and show
# differences with given input using unified format
__rerun_diff_ncat() {
local index=0
echo "$1" | diff -U 99 - "$2" | tail -n+4 | while read; do
echo -n "${REPLY:0:1}"
if [[ ${REPLY:0:1} == '-' ]]; then
echo -n " "
else
printf '%2d ' $index
((++index))
fi
echo "${REPLY:1}"
done
}
# Echo absolute canonical path of given path ($1)
# Some systems can just use 'readlink -f'
__rerun_get_canonical_path() (
local path=$1
local dir=$(dirname "$path")
# Follow symlinks
while [[ -L "$path" ]]; do
cd "$dir"
path=$(readlink "$path")
dir=$(dirname "$path")
done
# Bail with error on non-existent path
[[ -e "$path" ]] || return 1;
# Get full physical path of file or directory
local base=""
if [[ -d "$path" ]]; then
dir=$path
else
base=/$(basename "$path")
fi
cd "$dir"
echo "$(pwd -P)$base"
)
__rerun_parse_hist_ids() {
local -a hist_ids=()
local arg
for arg in $*
do
if [[ "$arg" =~ ^[0-9]+-[0-9]+$ ]]; then
eval hist_ids+=({${arg/-/..}})
elif [[ "$arg" =~ ^(-?[0-9]+):([0-9]+)$ ]]; then
local start=${BASH_REMATCH[1]}
local count=${BASH_REMATCH[2]}
eval hist_ids+=({$start..$((start+$count-1))})
elif [[ "$arg" =~ ^(-?[0-9]+)([.-]+)$ ]]; then
local id=${BASH_REMATCH[1]}
local spec=${BASH_REMATCH[2]}
hist_ids+=($id)
local c
for c in $(fold -w1 <<<$spec); do
((++id))
[[ $c == '.' ]] && hist_ids+=($id)
done
else
hist_ids+=($arg)
fi
done
echo ${hist_ids[*]}
}
__rerun_get_macro_path() {
echo "$RERUN_DIR/$1"
if [[ ! -x "$RERUN_DIR/$1" ]]; then
echo "ERROR: macro $1 does not exist." >&2
return 1
fi
}
__rerun_exec() {
if (($#!=0)); then
local action=$1; shift
rerun "$action" "${FUNCNAME[1]}" "$@"
else
"$RERUN_DIR"/"${FUNCNAME[1]}"
fi
}
__rerun_do_create() {
local macro_name=$1; shift
# Bailout if the macro is already defined
local macro; macro=$(__rerun_get_macro_path "$macro_name" 2>/dev/null)
if (($?==0)); then
echo "ERROR: macro file already exists."
return 1
fi
touch "$macro"
echo " -- Creating macro [$macro_name]"
# Get commands from history and add them to the macro file
local hist_id
for hist_id in $(__rerun_parse_hist_ids $*)
do
echo $(history -p '!'$hist_id) >> "$macro"
done
# Print the macro file
__rerun_ncat "$macro"
# Make the macro file executable
chmod +x "$macro"
# Create a function with the macro name
eval "$macro_name"'()' '{' '__rerun_exec' '"$@";' '}'
}
__rerun_do_list() {
# If no macro name is specified print out the names of all
# existing macros.
if [[ $# -eq 0 ]]; then
echo " -- Macros:"
local file
for file in $RERUN_DIR/*
do
[[ -x $file ]] && echo " `basename $file`"
done
# Otherwise, print out the contents of the macro file
else
local macro_name=$1
local macro; macro=$(__rerun_get_macro_path "$macro_name") || return $?
echo " -- Macro [$macro_name]"
__rerun_ncat "$macro"
fi
}
__rerun_do_append() {
# Add commands to the end of an existing macro
local macro_name=$1; shift
local macro; macro=$(__rerun_get_macro_path "$macro_name") || return $?
echo " -- Appending macro [$macro_name]"
local orig=$(<"$macro")
local hist_id
for hist_id in $(__rerun_parse_hist_ids $*)
do
echo $(history -p '!'$hist_id) >> "$macro"
done
__rerun_diff_ncat "$orig" "$macro"
}
__rerun_do_insert() {
# Insert commands into an existing macro
local macro_name=$1; shift
local macro; macro=$(__rerun_get_macro_path "$macro_name") || return $?
echo " -- Inserting into macro [$macro_name]"
# Get the index of the insertion point
local cmd_pos=$(($1+1)); shift
local orig=$(<"$macro")
local idx=0
local hist_id
for hist_id in $(__rerun_parse_hist_ids $*)
do
local cmd=$(history -p '!'$hist_id)
local pos=$(($cmd_pos+$idx))
sed -i "$pos"i"$cmd" "$macro"
((++idx))
done
__rerun_diff_ncat "$orig" "$macro"
}
__rerun_do_remove() {
# Remove a command from an existing macro
local macro_name=$1
local macro; macro=$(__rerun_get_macro_path "$macro_name") || return $?
echo " -- Removing from macro [$macro_name]"
# Get the index of item to be removed
local cmd_pos=$2
local orig=$(<"$macro")
sed -i $((cmd_pos+1))d "$macro"
__rerun_diff_ncat "$orig" "$macro"
}
__rerun_do_edit() {
local macro_name=$1
local macro; macro=$(__rerun_get_macro_path "$macro_name") || return $?
local orig=$(<"$macro")
${VISUAL:-${EDITOR:-vi}} "$macro"
__rerun_diff_ncat "$orig" "$macro"
}
__rerun_do_delete() {
# Delete an existing macro
local macro_name=$1
local macro; macro=$(__rerun_get_macro_path "$macro_name") || return $?
# Remove the macro file and unset the function
echo " -- Deleting macro [$macro_name]"
rm -f "$macro"
unset "$macro_name"
}
__rerun_return_help() {
local src=$(sed '1,4d;/^[^#]/q;s/^# \{0,1\}\(.*\)$/echo "\1";/' "$__rerun_source_path")
eval "$src"
return 2
}
######################
# External functions
#
rerun() {
# Setup directories and global variables
RERUN_DIR=${RERUN_DIR:=$(mktemp -d -t rerun.XXXXX)}
mkdir -p "$RERUN_DIR"
# Cleanup macros which have been removed via 'unset'
for file in $RERUN_DIR/*
do
declare -f `basename $file` | grep "__rerun_exec" >& /dev/null
if [[ $? -eq 1 ]]; then
rm -f $file
fi
done
local action=$1; shift
case "$action" in
c|cr|cre|crea|creat|create)
__rerun_do_create "$@" || return $?
;;
l|li|lis|list)
__rerun_do_list "$@" || return $?
;;
a|ap|app|appe|appen|append)
__rerun_do_append "$@" || return $?
;;
i|in|ins|inse|inser|insert)
__rerun_do_insert "$@" || return $?
;;
r|re|rem|remo|remov|remove)
__rerun_do_remove "$@" || return $?
;;
e|ed|edi|edit)
__rerun_do_edit "$@" || return $?
;;
d|de|del|dele|delet|delete)
__rerun_do_delete "$@" || return $?
;;
'')
__rerun_return_help
return $?
;;
*)
echo "ERROR: unrecognized action \"$action\"."
__rerun_return_help
return $?
;;
esac
}
######################
# Internal variables
#
# Path to this source file
__rerun_source_path=$(f(){ __rerun_get_canonical_path "$BASH_SOURCE";};f)