-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_input
executable file
·68 lines (61 loc) · 1.77 KB
/
get_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
#!/usr/bin/env zsh
# More safety, by turning some bugs into errors.
set -o errexit -o pipefail -o noclobber -o nounset
# option --output/-o requires 1 argument
LONGOPTS=year:,day:,token::
OPTIONS=y:,d:,t::
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
# -if getopt fails, it complains itself to stdout
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") || exit 2
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
year=- day=- token=-
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
--year)
year="$2"
shift 2
;;
--day)
day="$2"
shift 2
;;
--token)
token="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# check date is not in the future
if [[ $(date -d $year-12-$day +%s) > $(date +%s) ]]; then
echo "Can not access the future"
exit 4
fi
# set token to env token
if [[ $token == "-" ]]; then
token=$AOC_SESSION_TOKEN
fi
# ensure input directory exists
INPUT_DIRECTORY="$(git rev-parse --show-toplevel)/${year}/input"
if [[ ! -d "$INPUT_DIRECTORY" ]]; then
mkdir -p $INPUT_DIRECTORY
fi
# download input data if not already present
INPUT_FILE_PATH=$(printf "%s/day%02d.txt" $INPUT_DIRECTORY $day)
if [[ ! -f $INPUT_FILE_PATH ]]; then
printf "Downloading input data for AoC-%s day %d into '%s' ...\n" $year $day $INPUT_FILE_PATH
curl "https://adventofcode.com/${year}/day/${day}/input" --cookie "session=$token" -o $INPUT_FILE_PATH
else
printf "Input data for AoC-%s day %d already exist!\n" $year $day $INPUT_FILE_PATH
fi