-
Notifications
You must be signed in to change notification settings - Fork 9
/
post-input
executable file
·79 lines (71 loc) · 1.77 KB
/
post-input
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
#!/bin/sh
set -e -u
usage() {
echo "usage:\n ./post-input <service> <year> <day> <part> # reads input from stdin"
echo "where <service> is one of:"
echo "- fly"
echo "- fly-grpc"
echo "- cloudflare"
echo "- netlify"
echo "- local"
echo "example:\n ./post-input fly 2019 23 1 < crates/core/src/year2019/day23_input.txt"
exit 1
}
if [ "$#" != 4 ]; then
usage
fi
WHERE=$1
YEAR=$2
DAY=$3
PART=$4
case $WHERE in
fly)
HOST=https://advent.fly.dev;;
fly-grpc)
HOST=fly-grpc;;
cloudflare)
HOST=https://aoc.fornwall.workers.dev;;
netlify)
HOST=https://mystifying-blackwell-9e705f.netlify.app;;
local)
HOST=http://localhost:8080;;
*)
echo "Unsupported server: $1 - use one of cloudflare/fly/local/netlify"; exit 1;;
esac
if [ -z "$YEAR" ]; then
usage
elif [ "$DAY" -ge 26 ] && [ "$PART" -le 0 ]; then
echo "Invalid day: $DAY - only 1-25 is allowed"
exit 1
elif [ "$PART" -ge 2 ] && [ "$PART" -le 0 ]; then
echo "Invalid part: $PART - only 1 or 2 is allowed"
exit 1
fi
if [ "$HOST" = "fly-grpc" ]; then
set +e
echo | base64 --wrap 0 > /dev/null 2>&1
if [ $? -eq 0 ]; then
# GNU coreutils base64, '-w' supported
BASE64_TEXT=$(base64 --wrap 0)
else
# Openssl base64, no wrapping by default
BASE64_TEXT=$(base64)
fi
set -e
JSON=$(jq --null-input \
--arg year $YEAR \
--arg day $DAY \
--arg part $PART \
--arg input "$BASE64_TEXT" \
'{"year":$year, "day":$day, "part":$part, "input":$input | @base64d }' \
)
grpcurl -d "$JSON" \
advent-grpc.fly.dev:443 \
advent.Solver/Solve \
| jq --raw-output .answer
else
curl --fail \
-w '\n'\
--data-binary @- \
"$HOST/solve/$YEAR/$DAY/$PART"
fi