...
1#!/bin/sh
2
3# Copyright 2023 Tetrate
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16set -e
17# Code generated by godownloader on 2021-05-25T10:02:41Z. DO NOT EDIT.
18#
19
20usage() {
21 this=$1
22 cat <<EOF
23$this: download go binaries for tetratelabs/wazero
24
25Usage: $this [-b] bindir [-d] [tag]
26 -b sets bindir or installation directory, Defaults to ./bin
27 -d turns on debug logging
28 [tag] is a tag from
29 https://github.com/tetratelabs/wazero/releases
30 If tag is missing, then the latest will be used.
31
32 Generated by godownloader
33 https://github.com/goreleaser/godownloader
34
35EOF
36 exit 2
37}
38
39parse_args() {
40 #BINDIR is ./bin unless set be ENV
41 # over-ridden by flag below
42
43 BINDIR=${BINDIR:-./bin}
44 while getopts "b:dh?x" arg; do
45 case "$arg" in
46 b) BINDIR="$OPTARG" ;;
47 d) log_set_priority 10 ;;
48 h | \?) usage "$0" ;;
49 x) set -x ;;
50 esac
51 done
52 shift $((OPTIND - 1))
53 TAG=$1
54}
55# this function wraps all the destructive operations
56# if a curl|bash cuts off the end of the script due to
57# network, either nothing will happen or will syntax error
58# out preventing half-done work
59execute() {
60 tmpdir=$(mktemp -d)
61 log_debug "downloading files into ${tmpdir}"
62 http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}"
63 http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}"
64 hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}"
65 srcdir="${tmpdir}"
66 (cd "${tmpdir}" && untar "${TARBALL}")
67 test ! -d "${BINDIR}" && install -d "${BINDIR}"
68 for binexe in $BINARIES; do
69 if [ "$OS" = "windows" ]; then
70 binexe="${binexe}.exe"
71 fi
72 install "${srcdir}/${binexe}" "${BINDIR}/"
73 log_info "installed ${BINDIR}/${binexe}"
74 done
75 rm -rf "${tmpdir}"
76}
77get_binaries() {
78 case "$PLATFORM" in
79 darwin/amd64) BINARIES="wazero" ;;
80 darwin/arm64) BINARIES="wazero" ;;
81 linux/amd64) BINARIES="wazero" ;;
82 linux/arm64) BINARIES="wazero" ;;
83 *)
84 log_crit "platform $PLATFORM is not supported. Make sure this script is up-to-date and file request at https://github.com/${PREFIX}/issues/new"
85 exit 1
86 ;;
87 esac
88}
89tag_to_version() {
90 if [ -z "${TAG}" ]; then
91 log_info "checking GitHub for latest tag"
92 else
93 log_info "checking GitHub for tag '${TAG}'"
94 fi
95 REALTAG=$(github_release "$OWNER/$REPO" "${TAG}") && true
96 if test -z "$REALTAG"; then
97 log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
98 exit 1
99 fi
100 # if version starts with 'v', remove it
101 TAG="$REALTAG"
102 VERSION=${TAG#v}
103}
104adjust_format() {
105 # change format (tar.gz or zip) based on OS
106 true
107}
108adjust_os() {
109 # adjust archive name based on OS
110 true
111}
112adjust_arch() {
113 # adjust archive name based on ARCH
114 true
115}
116
117cat /dev/null <<EOF
118------------------------------------------------------------------------
119https://github.com/client9/shlib - portable posix shell functions
120Public domain - http://unlicense.org
121https://github.com/client9/shlib/blob/master/LICENSE.md
122but credit (and pull requests) appreciated.
123------------------------------------------------------------------------
124EOF
125is_command() {
126 command -v "$1" >/dev/null
127}
128echoerr() {
129 echo "$@" 1>&2
130}
131log_prefix() {
132 echo "$0"
133}
134_logp=6
135log_set_priority() {
136 _logp="$1"
137}
138log_priority() {
139 if test -z "$1"; then
140 echo "$_logp"
141 return
142 fi
143 [ "$1" -le "$_logp" ]
144}
145log_tag() {
146 case $1 in
147 0) echo "emerg" ;;
148 1) echo "alert" ;;
149 2) echo "crit" ;;
150 3) echo "err" ;;
151 4) echo "warning" ;;
152 5) echo "notice" ;;
153 6) echo "info" ;;
154 7) echo "debug" ;;
155 *) echo "$1" ;;
156 esac
157}
158log_debug() {
159 log_priority 7 || return 0
160 echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
161}
162log_info() {
163 log_priority 6 || return 0
164 echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
165}
166log_err() {
167 log_priority 3 || return 0
168 echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
169}
170log_crit() {
171 log_priority 2 || return 0
172 echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
173}
174uname_os() {
175 os=$(uname -s | tr '[:upper:]' '[:lower:]')
176 case "$os" in
177 cygwin_nt*) os="windows" ;;
178 mingw*) os="windows" ;;
179 msys_nt*) os="windows" ;;
180 esac
181 echo "$os"
182}
183uname_arch() {
184 arch=$(uname -m)
185 case $arch in
186 x86_64) arch="amd64" ;;
187 x86) arch="386" ;;
188 i686) arch="386" ;;
189 i386) arch="386" ;;
190 aarch64) arch="arm64" ;;
191 armv5*) arch="armv5" ;;
192 armv6*) arch="armv6" ;;
193 armv7*) arch="armv7" ;;
194 esac
195 echo ${arch}
196}
197uname_os_check() {
198 os=$(uname_os)
199 case "$os" in
200 darwin) return 0 ;;
201 dragonfly) return 0 ;;
202 freebsd) return 0 ;;
203 linux) return 0 ;;
204 android) return 0 ;;
205 nacl) return 0 ;;
206 netbsd) return 0 ;;
207 openbsd) return 0 ;;
208 plan9) return 0 ;;
209 solaris) return 0 ;;
210 windows) return 0 ;;
211 esac
212 log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib"
213 return 1
214}
215uname_arch_check() {
216 arch=$(uname_arch)
217 case "$arch" in
218 386) return 0 ;;
219 amd64) return 0 ;;
220 arm64) return 0 ;;
221 armv5) return 0 ;;
222 armv6) return 0 ;;
223 armv7) return 0 ;;
224 ppc64) return 0 ;;
225 ppc64le) return 0 ;;
226 mips) return 0 ;;
227 mipsle) return 0 ;;
228 mips64) return 0 ;;
229 mips64le) return 0 ;;
230 s390x) return 0 ;;
231 amd64p32) return 0 ;;
232 esac
233 log_crit "uname_arch_check '$(uname -m)' got converted to '$arch' which is not a GOARCH value. Please file bug report at https://github.com/client9/shlib"
234 return 1
235}
236untar() {
237 tarball=$1
238 case "${tarball}" in
239 *.tar.gz | *.tgz) tar --no-same-owner -xzf "${tarball}" ;;
240 *.tar) tar --no-same-owner -xf "${tarball}" ;;
241 *.zip) unzip "${tarball}" ;;
242 *)
243 log_err "untar unknown archive format for ${tarball}"
244 return 1
245 ;;
246 esac
247}
248http_download_curl() {
249 local_file=$1
250 source_url=$2
251 header=$3
252 if [ -z "$header" ]; then
253 code=$(curl -w '%{http_code}' -sL -o "$local_file" "$source_url")
254 else
255 code=$(curl -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
256 fi
257 if [ "$code" != "200" ]; then
258 log_debug "http_download_curl received HTTP status $code"
259 return 1
260 fi
261 return 0
262}
263http_download_wget() {
264 local_file=$1
265 source_url=$2
266 header=$3
267 if [ -z "$header" ]; then
268 wget -q -O "$local_file" "$source_url"
269 else
270 wget -q --header "$header" -O "$local_file" "$source_url"
271 fi
272}
273http_download() {
274 log_debug "http_download $2"
275 if is_command curl; then
276 http_download_curl "$@"
277 return
278 elif is_command wget; then
279 http_download_wget "$@"
280 return
281 fi
282 log_crit "http_download unable to find wget or curl"
283 return 1
284}
285http_copy() {
286 tmp=$(mktemp)
287 http_download "${tmp}" "$1" "$2" || return 1
288 body=$(cat "$tmp")
289 rm -f "${tmp}"
290 echo "$body"
291}
292github_release() {
293 owner_repo=$1
294 version=$2
295 test -z "$version" && version="latest"
296 giturl="https://github.com/${owner_repo}/releases/${version}"
297 json=$(http_copy "$giturl" "Accept:application/json")
298 test -z "$json" && return 1
299 version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
300 test -z "$version" && return 1
301 echo "$version"
302}
303hash_sha256() {
304 TARGET=${1:-/dev/stdin}
305 if is_command gsha256sum; then
306 hash=$(gsha256sum "$TARGET") || return 1
307 echo "$hash" | cut -d ' ' -f 1
308 elif is_command sha256sum; then
309 hash=$(sha256sum "$TARGET") || return 1
310 echo "$hash" | cut -d ' ' -f 1
311 elif is_command shasum; then
312 hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
313 echo "$hash" | cut -d ' ' -f 1
314 elif is_command openssl; then
315 hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
316 echo "$hash" | cut -d ' ' -f a
317 else
318 log_crit "hash_sha256 unable to find command to compute sha-256 hash"
319 return 1
320 fi
321}
322hash_sha256_verify() {
323 TARGET=$1
324 checksums=$2
325 if [ -z "$checksums" ]; then
326 log_err "hash_sha256_verify checksum file not specified in arg2"
327 return 1
328 fi
329 BASENAME=${TARGET##*/}
330 want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
331 if [ -z "$want" ]; then
332 log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
333 return 1
334 fi
335 got=$(hash_sha256 "$TARGET")
336 if [ "$want" != "$got" ]; then
337 log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
338 return 1
339 fi
340}
341cat /dev/null <<EOF
342------------------------------------------------------------------------
343End of functions from https://github.com/client9/shlib
344------------------------------------------------------------------------
345EOF
346
347PROJECT_NAME="wazero"
348OWNER=tetratelabs
349REPO="wazero"
350BINARY=wazero
351FORMAT=tar.gz
352OS=$(uname_os)
353ARCH=$(uname_arch)
354PREFIX="$OWNER/$REPO"
355
356# use in logging routines
357log_prefix() {
358 echo "$PREFIX"
359}
360PLATFORM="${OS}/${ARCH}"
361GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
362
363uname_os_check "$OS"
364uname_arch_check "$ARCH"
365
366parse_args "$@"
367
368get_binaries
369
370tag_to_version
371
372adjust_format
373
374adjust_os
375
376adjust_arch
377
378log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
379
380NAME=${PROJECT_NAME}_${VERSION}_${OS}_${ARCH}
381TARBALL=${NAME}.${FORMAT}
382TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
383CHECKSUM=wazero_${VERSION}_checksums.txt
384CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
385
386
387execute
View as plain text