]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - tools/download-rust-crate
Merge branch 'next'
[people/pmueller/ipfire-2.x.git] / tools / download-rust-crate
CommitLineData
5352e923
MT
1#!/bin/bash
2
3set -o pipefail
4
5RUST_TEMPLATE="lfs/rust-rand"
6
7fetch_latest_version() {
8 local name="${1}"
9
10 if ! curl --silent "https://crates.io/api/v1/crates/${name}" | \
11 jq --raw-output .crate.max_stable_version; then
12 echo "${0}: Could not find the latest stable version of ${name}" >&2
13 return 1
14 fi
15}
16
17main() {
18 local name="${1}"
19 local version="${2}"
20
21 if [ -z "${name}" ]; then
22 echo "${0}: You need to pass a name of a crate" >&2
23 return 2
24 fi
25
26 if [ -z "${version}" ]; then
27 version="$(fetch_latest_version "${name}")"
28 if [ -z "${version}" ]; then
29 # error message has already been printed
30 return 1
31 fi
32 fi
33
34 # Compose download URL
35 local url="https://crates.io/api/v1/crates/${name}/${version}/download"
36 local download="$(mktemp)"
37
38 # Perform download
39 if ! curl -L "${url}" -o "${download}"; then
40 echo "${0}: Could not download ${name}-${version}" >&2
41 unlink "${download}"
42 return 1
43 fi
44
45 # Check if download is an orderly tar file
46 if ! tar tvf "${download}" &>/dev/null; then
47 echo "${0}: Download is not a tar file" >&2
48 unlink "${download}"
49 return 1
50 fi
51
52 # Hash the downloaded file
98b761a5
PM
53 local b2sum="$(b2sum "${download}" | awk '{ print $1 }')"
54 if [ -z "${b2sum}" ]; then
5352e923
MT
55 echo "${0}: Could not hash download" >&2
56 unlink "${download}"
57 return 1
58 fi
59
60 local filename="cache/${name}-${version}.tar.gz"
61
62 # Move to final destination
63 if ! install -m 644 "${download}" "${filename}"; then
64 echo "${0}: Could not move downloaded file to ${filename}" >&2
65 unlink "${download}"
66 return 1
67 fi
68
69 # Remove download
70 unlink "${download}"
71
72 # Create a new LFS file
73 sed < "${RUST_TEMPLATE}" > "lfs/rust-${name}" \
74 -e "s/^VER.*/VER = ${version}/" \
75 -e "s/^THISAPP.*/THISAPP = ${name}-\$(VER)/" \
98b761a5 76 -e "s/^\$(DL_FILE)_BLAKE2.*/\$(DL_FILE)_BLAKE2 = ${b2sum}/"
5352e923
MT
77
78 echo "Done"
79 return 0
80}
81
82main "$@" || exit $?