-
Notifications
You must be signed in to change notification settings - Fork 2
/
placebo
259 lines (217 loc) · 4.26 KB
/
placebo
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
#!/usr/bin/env bash
_usage() {
case "$1" in
"main")
echo "Usage: . $0 [-h]"
;;
"attach")
echo "Usage: pill_attach [-h] \
command=command1[,command2,...] 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() {
local _command _data_path c
[ "$1" == "-h" ] && \
_usage attach && return
_command="$1"
_data_path="$2"
{ [[ ! "$_command" =~ command= ]] || \
[[ ! "$_data_path" =~ data_path= ]] ; } \
&& _usage attach && return 1
_command="${_command#command=}"
IFS=',' read -r -a COMMANDS <<< "$_command"
DATA_PATH="${_data_path#data_path=}"
if [ ! -d "$DATA_PATH" ] ; then
echo "DATA_PATH should be a directory"
_usage attach && return 1
fi
_mock
true > commands_log
}
pill_playback() {
[ "$1" == "-h" ] && \
_usage playback && return
# shellcheck disable=SC2034
PILL=playback
}
pill_record() {
[ "$1" == "-h" ] && \
_usage record && return
# shellcheck disable=SC2034
PILL=record
}
pill_log() {
[ "$1" == "-h" ] && \
_usage log && return
[ -f commands_log ] && \
cat commands_log
}
pill_detach() {
local f funcs
[ "$1" == "-h" ] && \
_usage detach && return
funcs="_usage
pill_attach
pill_playback
pill_record
pill_log
pill_detach
_mock
_cli_to_comm
_comm_to_file
_create_new
_update_existing
_filter
_record"
for f in $funcs "${COMMANDS[@]}"; do
unset -f "$f"
done
unset PILL DATA_PATH COMMANDS
command rm -f commands_log
}
# private functions.
_comm_to_file() {
echo "$DATA_PATH"/"$1".sh
}
_filter() {
sed -E "
s/ +$//
s/ +'/'/
"
}
_mock() {
local c code
for c in "${COMMANDS[@]}" ; do
# shellcheck disable=SC2016,SC1004
code="$c"'() {
local file comm
comm="${FUNCNAME[0]}"
file=$(_comm_to_file "$comm")
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 \
playback or record. Try pill_playback \
or pill_record"
return 1
fi
if { [ ! -f "$file" ] && \
[ "$PILL" == "playback" ] ; }; then
echo "\
$file not found. \
Make sure you firstly record it"
return 1
fi
echo "$comm $*" | \
_filter >> commands_log
case "$PILL" in
"playback")
source "$file" "$@"
;;
"record")
_record "$file" "$comm $*"
;;
esac
}'
eval "$code"
done
}
_cli_to_comm() {
awk '{print $1}' <<< "$1"
}
_record() {
local file cli comm temp_file rc lines
file="$1" ; cli="$2"
comm=$(_cli_to_comm "$cli")
temp_file=/tmp/"$$"
if [ -s "$file" ] ; then
_update_existing "$comm"
else
_create_new "$comm"
fi
# capture response data and exit status.
# shellcheck disable=SC2086
command $cli | tee -a "$temp_file" ; rc="${PIPESTATUS[0]}"
response=$(<"$temp_file")
lines=$(wc -l <<< "$response")
if [ "$lines" -eq 1 ] ; then
if [ "$rc" -eq 0 ] ; then
if [ -z "$response" ] ; then echo \
"'$cli') true ;;"
else echo \
"'$cli') echo '$response' ;;"
fi
else
if [ -z "$response" ] ; then echo \
"'$cli') (exit $rc) ;;"
else echo \
"'$cli') echo '$response' ; (exit $rc) ;;"
fi
fi
elif [ "$lines" -gt 1 ] ; then echo \
"'$cli')
cat <<'EOF'
$response
EOF
"
[ "$rc" -ne 0 ] && echo \
" (exit $rc)"
echo \
" ;;"
fi \
>> "$file"
cat >> "$file" <<EOD
*)
echo "No responses for: $comm \$*" | \
tee -a unknown_commands
;;
esac
EOD
command rm -f "$temp_file"
(exit "$rc")
}
_create_new() {
local comm f
comm="$1" ; f=$(_comm_to_file "$comm")
echo 'case "'"$comm"' $*" in' > "$f"
}
_update_existing() {
local comm f
comm="$1" ; f=$(_comm_to_file "$comm")
awk '
$0 == "*)" {
exit
}
{
print
}
' "$f" > "$f.bak"
mv "$f.bak" "$f"
}
# vim: ft=sh