-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinstall.sh
More file actions
165 lines (151 loc) · 4.13 KB
/
Copy pathinstall.sh
File metadata and controls
165 lines (151 loc) · 4.13 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
#!/bin/sh
set -eu
# Use "latest" if not specified, otherwise accept explicit tag like v0.1.0
VERSION=${VERSION:-latest}
# Binary to download: "xprin" (default) or "xprin-helpers". Used in release artifact name and output file.
PACKAGE=${PACKAGE:-xprin}
# Set to "true" (case-insensitive) to download the .tar.gz bundle instead of the raw binary
COMPRESSED=${COMPRESSED:-"False"}
# Set to "true" (case-insensitive) to download .sha256 and verify checksums. Recommended when COMPRESSED=true.
VERIFY_SHA=${VERIFY_SHA:-"False"}
os=$(uname -s)
arch=$(uname -m)
OS=${OS:-"${os}"}
ARCH=${ARCH:-"${arch}"}
OS_ARCH=""
BIN="${PACKAGE}"
EXT=""
unsupported_arch() {
os="$1"
arch="$2"
echo "${PACKAGE} does not support $os / $arch at this time."
exit 1
}
# verify_sha256 verifies that the given file matches the checksum in the .sha256 file.
# The .sha256 file contains only the 64-char checksum (no filename).
verify_sha256() {
downloaded_file="$1"
checksum_file="$2"
if [ ! -f "$downloaded_file" ] || [ ! -f "$checksum_file" ]; then
echo "SHA256 verification failed: missing downloaded file or checksum file."
exit 1
fi
expected=$(tr -d '\n\r ' < "$checksum_file" | head -c 64)
if [ -z "$expected" ] || [ ${#expected} -ne 64 ]; then
echo "SHA256 verification failed: invalid checksum file."
exit 1
fi
line="${expected} ${downloaded_file}"
if command -v sha256sum >/dev/null 2>&1; then
echo "$line" | sha256sum --check
elif command -v shasum >/dev/null 2>&1; then
echo "$line" | shasum -a 256 --check
else
echo "SHA256 verification failed: need sha256sum or shasum."
exit 1
fi
}
case $OS in
CYGWIN* | MINGW64* | Windows*)
if [ "$ARCH" = "x86_64" ]; then
EXT=".exe"
OS_ARCH="windows_amd64"
BIN="${PACKAGE}.exe"
else
unsupported_arch "$OS" "$ARCH"
fi
;;
Darwin)
case $ARCH in
x86_64 | amd64)
OS_ARCH="darwin_amd64"
;;
arm64)
OS_ARCH="darwin_arm64"
;;
*)
unsupported_arch "$OS" "$ARCH"
;;
esac
;;
Linux)
case $ARCH in
x86_64 | amd64)
OS_ARCH="linux_amd64"
;;
arm64 | aarch64)
OS_ARCH="linux_arm64"
;;
arm)
OS_ARCH="linux_arm"
;;
ppc64le)
OS_ARCH="linux_ppc64le"
;;
*)
unsupported_arch "$OS" "$ARCH"
;;
esac
;;
*)
unsupported_arch "$OS" "$ARCH"
;;
esac
_compr=$(echo "$COMPRESSED" | tr '[:upper:]' '[:lower:]')
_verify=$(echo "$VERIFY_SHA" | tr '[:upper:]' '[:lower:]')
if [ "${_compr}" = "true" ]; then
url_file="${PACKAGE}_${OS_ARCH}.tar.gz"
url_error="a compressed file for "
else
url_file="${PACKAGE}_${OS_ARCH}${EXT}"
url_error=""
fi
if [ "$VERSION" = "latest" ]; then
url="https://github.com/crossplane-contrib/xprin/releases/latest/download/${url_file}"
else
url="https://github.com/crossplane-contrib/xprin/releases/download/${VERSION}/${url_file}"
fi
if ! curl -sfL "${url}" -o "${url_file}"; then
echo "Failed to download ${PACKAGE}. Please make sure ${url_error}version ${VERSION} exists."
echo " https://github.com/crossplane-contrib/xprin/releases"
exit 1
fi
if [ "${_verify}" = "true" ]; then
url_sha256="${url_file}.sha256"
if [ "$VERSION" = "latest" ]; then
url_sha256_full="https://github.com/crossplane-contrib/xprin/releases/latest/download/${url_sha256}"
else
url_sha256_full="https://github.com/crossplane-contrib/xprin/releases/download/${VERSION}/${url_sha256}"
fi
if ! curl -sfL "${url_sha256_full}" -o "${url_sha256}"; then
echo "Failed to download ${url_sha256} for verification."
exit 1
fi
verify_sha256 "${url_file}" "${url_sha256}"
fi
if [ "${_compr}" = "true" ]; then
if ! tar xzf "${url_file}"; then
echo "Failed to unpack the ${PACKAGE} compressed file."
exit 1
fi
if [ "${_verify}" = "true" ]; then
# Verify the extracted binary, then remove tarball and all .sha256 files
verify_sha256 "${BIN}" "${BIN}.sha256"
rm -f "${BIN}.sha256" "${url_file}.sha256" "${url_file}"
else
rm -f "${BIN}.sha256" "${url_file}"
fi
else
if [ "${_verify}" = "true" ]; then
rm -f "${url_file}.sha256"
fi
mv "${url_file}" "${BIN}"
fi
chmod +x "${BIN}"
echo "${PACKAGE} downloaded successfully!"
echo
echo "To finish installation, run:"
echo " sudo mv ${BIN} /usr/local/bin/"
echo " ${PACKAGE} --help"
echo
echo "Visit https://github.com/crossplane-contrib/xprin for more info. 🚀"