-
Notifications
You must be signed in to change notification settings - Fork 0
/
log2sh
executable file
·56 lines (47 loc) · 1.77 KB
/
log2sh
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
#!/usr/bin/env bash
[[ "${1,,}" != +(\-)@(h)?(elp) ]] || { echo "For each type of request log in .logs, this will do like 90% of the work in generating a curl script to replicate/test the corresponding endpoint."; exit 0; }
baseUrl='https://nightdistrict.co'
[[ "${1,,}" != +(\-)@(l)?(ocal) ]] || baseUrl='http://localhost:3000'
method() { tail -n1 "$1" | jq -Mr '.method' -- 2>/dev/null ; }
_method() { cat <<EOL
curl -s -X $(method $1) \\
EOL
}
url() {
local e="$(tail -n1 "$2" | jq -Mr '.url' -- 2>/dev/null)"
[[ "${e::1}" == \/ ]] && echo "${1}${e}" || echo "${1}/${e}"
}
_url() { cat <<EOL
"$(url $1 $2)" \\
EOL
}
_body() { cat <<EOL
-d $(tail -n1 "$1" | jq -Mc '.body' -- 2>/dev/null)
EOL
}
_headers_body() {
tail -n1 "$1" | jq -Mr '.headers' -- 2>/dev/null | sed 's/": "/: /g;s/^\s*/\t-H /g;/[{}]/d;s/,$/ \\/g'
_body "$1"
}
_headers() { tail -n1 "$1" | jq -Mr '.headers' -- 2>/dev/null | sed 's/": "/: /g;s/^\s*/\t-H /g;/[{}]/d;$!s/,$/ \\/g'; }
_payload() {
local -i len=$(tail -n1 "$1" | jq -Mrjc '.body' -- 2>/dev/null | wc -m)
if ((len > 2)); then _headers_body "$1"; else _headers "$1"; fi
}
#_() { curl -s -X POST "https://nightdistrict.co/api/review" \
# -H "authorization: Bearer $(ndLogin)" \
# -H "connection: upgrade" \
# -H "host: nightdistrict.co" \
# -H "user-agent: Dart/3.3 (dart:io)" \
# -H "accept: application/json" \
# -H "accept-encoding: gzip" \
# -H "content-type: application/json" \
# -d $(parseDump ${@})
#}
#for f in $(find {.,}logs/ -type f -name "*_requests.log"); do
for f in $(find .logs/ -type f -name "*_requests.log"); do
d="${f%\/*}" && mkdir -p .tests/${d#*logs/}
of=".tests/${d#*logs/}/${f##*\/}" && of="${of%\.*}.sh"
{ _method "$f" ; _url "$baseUrl" "$f" ; _payload "$f" ; } > "$of"
sed -i 's|earer .*\"|earer $(./nd_login.sh)\"|' $of
done