-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-archlinux-cloud.sh
executable file
·168 lines (138 loc) · 3.15 KB
/
get-archlinux-cloud.sh
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
#!/usr/bin/env bash
set -euo pipefail
function cmd_iso() {
echo 'Fetching ISO...' >&2
local url="$(cmd_iso_url)"
if [ "${OUTPUT}" == - ]
then
run_or_dryrun curl -L "${url}"
else
local output="${OUTPUT:-$(basename "${url}")}"
local part="${output}.part"
if [ -e "${output}" ]
then
echo "${output}: Already exists" >&2
exit 1
fi
run_or_dryrun curl -L "${url}" -o "${part}"
run_or_dryrun mv "${part}" "${output}"
fi
}
function cmd_iso_url() {
echo "https://github.com/${SOURCE}/releases/download/${TAG}/archlinux-cloud-${ISO_VERSION}-x86_64.iso"
}
function cmd_latest_iso_version() {
if [ -z "${ISO_VERSION}" ]
then
function func() {
curl -LSs "https://api.github.com/repos/${SOURCE}/releases/tags/${TAG}" \
| jq -r '.assets[] | .name' \
| grep -o '^archlinux-cloud-.*-x86_64\.iso$' \
| sed -e 's/^archlinux-cloud-//g' -e 's/-x86_64\.iso$//g' \
| sort \
| tail -n1
}
echo 'Fetching latest ISO version...' >&2
local ret
if ! ret="$(func)" || [ -z "${ret}" ]
then
echo 'Failed to get latest ISO version' >&2
return 1
fi
ISO_VERSION="${ret}"
fi
echo "${ISO_VERSION}"
}
function cmd_latest_tag() {
if [ -z "${TAG}" ]
then
function func() {
curl -LSs "https://api.github.com/repos/${SOURCE}/releases/latest" \
| jq -r '.tag_name'
}
echo 'Fetching latest tag...' >&2
local ret
if ! ret="$(func)" || [ "${ret}" == 'null' ]
then
echo 'Failed to get latest tag' >&2
return 1
fi
TAG="${ret}"
fi
echo "${TAG}"
}
function help() {
cat << EOF
${0} [OPTION]... [TARGET]
Options:
-h show help
-n dry run
-o OUTPUT specify output filename (default: basename of ISO)
-s SOURCE specify source repository (default: '${SOURCE}')
-t TAG specify tag (default: latest)
-v ISO_VERSION specify ISO version (default: latest)
Targets:
iso (default)
iso-url
latest-iso-version
latest-tag
EOF
}
function run_or_dryrun() {
if [ -z "${DRYRUN}" ]
then
"${@}"
else
echo '[DRYRUN]' "${@}" >&2
fi
}
DRYRUN=
ISO_VERSION=
OUTPUT=
SOURCE=t13a/archlinux-cloud
TAG=
TARGET=
while getopts hno:s:t:v: OPT
do
case "${OPT}" in
h)
help
exit 0
;;
n)
DRYRUN=yes
;;
o)
OUTPUT="${OPTARG}"
;;
s)
SOURCE="${OPTARG}"
;;
t)
TAG="${OPTARG}"
;;
v)
ISO_VERSION="${OPTARG}"
;;
*)
exit 1
;;
esac
done
shift $((${OPTIND} - 1))
if [ -z "${TAG}" ]
then
if ! TAG="$(cmd_latest_tag)"
then
exit 1
fi
fi
if [ -z "${ISO_VERSION}" ]
then
if ! ISO_VERSION="$(cmd_latest_iso_version)"
then
exit 1
fi
fi
TARGET="${1:-iso}"
"cmd_${TARGET//-/_}"