forked from alex-harvey-z3q/bash_placebo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplacebo
More file actions
264 lines (220 loc) · 4.41 KB
/
placebo
File metadata and controls
264 lines (220 loc) · 4.41 KB
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
#!/usr/bin/env bash
_usage() {
case $1 in
"main")
echo "Usage: . $0 [-h]"
;;
"attach")
echo "Usage: pill_attach [-h] \
data_path=path/to/responses/"
;;
"playback")
echo "Usage: pill_playback [-h]"
echo "Sets Placebo to playback mode"
;;
"record")
echo "Usage: pill_record [-h]"
echo "Sets Placebo to record mode"
;;
"detach")
echo "Usage: pill_detach [-h]"
echo "Detaches Placebo and cleans \
up functions and variables"
;;
esac
return 0
}
if [[ "$0" == "${BASH_SOURCE[0]}" ]] ; then
_usage main
false
fi
if [[ "$1" == "-h" ]] ; then
_usage main
true
fi
pill_attach() {
[[ "$1" == "-h" ]] && \
_usage attach && return
DATA_PATH=$(echo "$1" | cut -f2 -d=)
if [[ ! -d "$DATA_PATH" ]] ; then
echo "DATA_PATH should be a path to \
a directory but you specified a file"
_usage attach && return 1
fi
true > commands_log
}
pill_playback() {
[[ "$1" == "-h" ]] && \
_usage playback && return
if [[ -z "$DATA_PATH" ]] ; then
echo "DATA_PATH must be set. \
Try pill_attach"
return 1
fi
PILL=playback
local COMMAND_PATH
COMMAND_PATH="$(_join_path "$DATA_PATH" function.sh)"
source "$COMMAND_PATH"
}
pill_record() {
[[ "$1" == "-h" ]] && \
_usage record && return
PILL=record
}
pill_log() {
[[ "$1" == "-h" ]] && \
_usage log && return
[[ -f commands_log ]] && \
cat commands_log
}
pill_detach() {
[[ "$1" == "-h" ]] && \
_usage detach && return
local f funcs
funcs="_usage
pill_attach
pill_playback
pill_record
pill_log
pill_detach
_create_new
_update_existing
_filter
_record
_contains
_save_func
mock"
funcs=($(echo "$funcs" | tr "\n\r" "\n"))
for f in "${funcs[@]}" ; do
unset -f "$f"
done
if [[ "$PILL" == "playback" ]] ; then
local COMMAND_PATH="$(_join_path "$DATA_PATH" function.sh)"
local mock_funcs="$(bash "$COMMAND_PATH" -l)"
mock_funcs=($(echo "$mock_funcs" | tr "\n\r" "\n"))
for f in "${mock_funcs[@]}" ; do
unset -f "$f"
done
fi
unset PILL
unset DATA_PATH
rm -f commands_log
}
mock() {
if [[ -z "$DATA_PATH" ]] ; then
echo "DATA_PATH must be set. \
Try pill_attach"
return 1
fi
if [[ -z "$PILL" ]] ; then
echo "PILL must be set to \
or record. Try pill_record"
return 1
fi
if [ -z "$DATA_PATH" ] && \
[ "$PILL" == "playback" ] ; then
echo "DATA_PATH not found. Try \
pill_record to record one"
return 1
fi
local RES_PATH="$(_join_path ${DATA_PATH} ${1}.sh)"
local c=$(echo $* | tr "\n\r" " ")
echo "$c" | \
_filter >> commands_log
case "$PILL" in
"playback")
echo "PILL must be set to record. \
Try pill_record"
;;
"record")
_record "$RES_PATH" "$@"
_save_func "$DATA_PATH" "$1"
;;
esac
}
# private functions.
_save_func() {
local f="$1" ; shift
local a="$1"
local COMMAND_PATH="$(_join_path $f function.sh)"
[[ -s "$COMMAND_PATH" ]] && _update_existing "$COMMAND_PATH" \
|| _create_new "$COMMAND_PATH" "-c"
local list=$(bash $COMMAND_PATH -l)
local contains=$(_contains $list $a)
if [[ ! $contains ]] ; then
local s="$a() {
_check_response \$(source "$RES_PATH" "\$@") \$?
}
"
echo $s | cat - $COMMAND_PATH > temp && mv temp $COMMAND_PATH
fi
}
_record() {
local f="$1" ; shift
local a="$1"
[[ -s "$f" ]] && _update_existing "$f" \
|| _create_new "$f" "$a"
local c=$(echo $@ | tr "\n\r" " ")
r=$(command "$@")
local e=$?
# open another case.
cat <<EOD | _filter >> "$f"
'$c')
EOD
if [[ $e -ne 0 ]]; then
echo " exit \"$e\"" >> "$f"
else
echo " cat <<'EOF'" >> "$f"
echo "$r" >> "$f"
echo "EOF" >> "$f"
fi
# close the case and the block.
cat >> "$f" <<EOD
;;
*)
echo "No responses for: $a \$*"
;;
esac
EOD
}
_create_new() {
local f=$1
local a=$2
mkdir -p "$(dirname "$f")"
if [[ "$a" == "-c" ]] ; then
cat >> "$f" <<EOD
_check_response() {
if [[ $2 -ne 0 ]]; then
exit $2
else
echo $1
fi
}
list() {
declare -f | awk '/ \(\) $/ && !/^list / {print \$1}'
}
if [[ "\$1" == "-l" ]] ; then
list
true
fi
EOD
else
echo 'case "'"$a"' $*" in' > "$f"
fi
}
_update_existing() {
local f=$1
awk '/^\*\)/{exit}{print}' "$f" > "$f.bak"
mv "$f.bak" "$f"
}
_filter() {
sed -e 's/ *$//' \
-e "s/ *'/'/"
}
_join_path() {
echo "${1:+$1/}$2" | sed 's#//#/#g'
}
_contains() {
[[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]] && echo true || false
}
# vim: ft=sh