-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisco-packages.sh
More file actions
304 lines (253 loc) · 9.61 KB
/
Copy pathdisco-packages.sh
File metadata and controls
304 lines (253 loc) · 9.61 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/sh
#FUNCTION DEFINITIONS
infoFunction() {
echo "################################"
echo "# #"
echo "# foojay jdk discovery api #"
echo "# #"
echo "# disco-packages.sh #"
echo "# #"
echo "################################"
echo
echo
echo "Be aware that you need to have jq and wget installed"
echo
echo "Script parameters:"
echo "--help : Shows this info"
echo "--version : Version (mandatory or version by definition) e.g. 1.8.0_265 or 11 or 13.0.5.1"
echo "--version_by_definition : Version by definition (mandatory if no version) e.g. latest, latest_sts, latest_mts, latest_lts"
echo "--distro : Distribution e.g. aoj, aoj_openj9, dragonwell, corretto, liberica, microsoft, ojdk_build, openlogic, oracle_open_jdk, sap_machine, temurin, trava, zulu"
echo "--architecture : Architecture e.g. aarch64, arm, arm64, mips, ppc, ppc64, ppc64le, riscv64, s390x, sparc, sparcv9, x64, x86, amd64"
echo "--archive_type : File extension e.g. cab, deb, dmg, exe, msi, pkg, rpm, tar, zip"
echo "--package_type : Package type e.g. jre, jdk"
echo "--operating_system : Operating System (mandatory) e.g. windows, macos, linux"
echo "--libc_type : Type of libc e.g. glibc, libc, musl, c_std_lib"
echo "--release_status : Release status e.g. ea, ga"
echo "--term_of_support : Term of support e.g. sts, mts, lts"
echo "--bitness : Bitness e.g. 32, 64"
echo "--javafx_bundled : With JavaFX e.g. true, false"
echo "--directly_downloadable : Directly downloadable e.g. true, false"
echo "--latest : Latest e.g. overall, per_distro, available"
echo "--dest : Destination e.g. /Users/HanSolo"
echo
echo "Usage example:"
echo "disco-packages.sh --dest /Users/Hansolo --operating_system windows --version 1.8.0_265 --distro zulu --package_type jdk --architecture x64 --archive_type zip --release_status ga"
echo
exit 1
}
# FIELDS
destField="dest"
versionField="version"
fromVersionField="from_version"
toVersionField="to_version"
operatingSystemField="operating_system"
libcTypeField="libc_type"
archiveTypeField="archive_type"
architectureField="architecture"
distroField="distro"
bitnessField="bitness"
packageTypeField="package_type"
releaseStatusField="release_status"
supportTermField="support_term"
versionByDefinitionField="version_by_definition"
directlyDownloadableField="directly_downloadable"
javafxBundledField="javafx_bundled"
latestField="latest"
# CHECK FOR GIVEN PARAMETERS
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
param="${1/--/}"
declare $param=$2
if [ "$1" == "--help" ]; then
infoFunction
fi
if [ "$param" = "$destField" ]; then
declare destValue=$2
fi
if [ "$param" = "$versionField" ]; then
declare versionValue=$2
fi
if [ "$param" = "$fromVersionField" ]; then
declare fromVersionValue=$2
fi
if [ "$param" = "$toVersionField" ]; then
declare toVersionValue=$2
fi
if [ "$param" = "$operatingSystemField" ]; then
declare operatingSystemValue=$2
fi
if [ "$param" = "$libcTypeField" ]; then
declare libcTypeValue=$2
fi
if [ "$param" = "$archiveTypeField" ]; then
declare archiveTypeValue=$2
fi
if [ "$param" = "$architectureField" ]; then
declare architectureValue=$2
fi
if [ "$param" = "$distroField" ]; then
declare distroValue=$2
fi
if [ "$param" = "$bitnessField" ]; then
declare -i bitnessValue=$2
fi
if [ "$param" = "$packageTypeField" ]; then
declare packageTypeValue=$2
fi
if [ "$param" = "$releaseStatusField" ]; then
declare releaseStatusValue=$2
fi
if [ "$param" = "$supportTermField" ]; then
declare supportTermValue=$2
fi
if [ "$param" = "$versionByDefinitionField" ]; then
declare versionByDefinitionValue=$2
fi
if [ "$param" = "$directlyDownloadableField" ]; then
declare directlyDownloadableValue=$2
fi
if [ "$param" = "$javafxBundledField" ]; then
declare javafxBundledValue=$2
fi
if [ "$param" = "$latestField" ]; then
declare latestValue=$2
fi
fi
shift
done
# CALL THE DISCOAPI
url="https://api.foojay.io/disco/v1.0/packages"
present="0"
if [[ $versionValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}version=${versionValue}"
let present="1"
elif [[ $versionByDefinitionValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}version_by_definition=${versionByDefinitionValue}"
let present="1"
else
infoFunction
fi
if [[ $operatingSystemValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}operating_system=${operatingSystemValue}"
# SET EXTENSION DEPENDENT ON THE OPERATING SYSTEM
#if [[ $os -eq "windows" ]]; then
# url="${url}&ext=zip"
#elif [[ $os -eq "macos" ]]; then
# url="${url}&ext=zip"
#elif [[ $os -eq "linux" ]]; then
# url="${url}&ext=tar"
#else
# exit 1
#fi
let present="1"
else
infoFunction
fi
if [[ $libcTypeValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}libc_type=${libcTypeValue}"
let present="1"
fi
if [[ $archiveTypeValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}archive_type=${archiveTypeValue}"
let present="1"
fi
if [[ $architectureValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}architecture=${architectureValue}"
let present="1"
fi
if [[ $distroValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}distro=${distroValue}"
let present="1"
fi
if [[ $bitnessValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}bitness=${bitnessValue}"
let present="1"
fi
if [[ $packageTypeValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}package_type=${packageTypeValue}"
let present="1"
fi
if [[ $releaseStatusValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}release_status=${releaseStatusValue}"
let present="1"
fi
if [[ $supportTermValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}support_term=${supportTermValue}"
let present="1"
fi
if [[ $directlyDownloadableValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}directly_downloadable=${directlyDownloadableValue}"
let present="1"
fi
#if [[ $releaseValue ]]; then
# if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
# url="${url}release=${releaseValue}"
# let present="1"
# echo $url
#fi
if [[ $javafxBundledValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}javafx_bundled=${javafxBundledValue}"
let present="1"
fi
if [[ $latestValue ]]; then
if [ $present -eq "1" ]; then url="${url}&"; else url="${url}?"; fi
url="${url}latest=${latestValue}"
let present="1"
fi
#echo $url
# READ REST RESPONSE INTO VARIABLE
json="$(curl ${url} 2>/dev/null)"
#echo $json
# PRINT OUT
#echo "$json" | jq -c '.[]|"\(.id) , \(.filename) , \(.ephemeral_id)"'
#echo "$json" | jq -c '.[]|"\(.id)"'
noOfEntries=$(echo ${json} | jq length)
if [[ "$noOfEntries" > 1 ]]; then
echo
read -r -p "Found ${noOfEntries} packages, would you like to select the first one only (Y/n)? " response
response=${response:l} # bash version: response=${response,,}
echo
elif [[ "$noOfEntries" = 1 ]]; then
echo
echo "Exactly one package found that matches the given parameters"
echo
else
echo
echo "Sorry no packages found that matches the given parameters"
exit 1
fi
ephemeralIdsUrl="https://api.foojay.io/disco/v1.0/ephemeral_ids/"
#for k in $(jq '.[] | keys | .[]' <<< "$json"); do # iterate through keys of array
for k in $(jq '.[] | .ephemeral_id' <<< "$json"); do
dlUrl="${ephemeralIdsUrl}${k//\"}"
packageInfoJson="$(curl "$(echo "$dlUrl" | tr -d \")" 2>/dev/null)"
filename="$(echo "$packageInfoJson" | jq '.filename' | tr -d \")"
downloadLink="$(echo "$packageInfoJson" | jq '.direct_download_uri' | tr -d \")"
if [[ $destValue ]]; then
filename="${destValue}/${filename}"
fi
read -p "Download ${filename} (y/N) ?" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo Download package to $filename
#echo $filename $downloadLink
wget -O $filename $downloadLink
fi
# EXIT AFTER FIRST FILE HAS BEEN DOWNLOADED IF USER DECIDED NO
if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
exit 1
fi
done