|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +die() { |
| 4 | + echo "$@" >&2 |
| 5 | + exit 1 |
| 6 | +} |
| 7 | + |
| 8 | +have_binary() { |
| 9 | + type "$1" > /dev/null 2> /dev/null |
| 10 | +} |
| 11 | + |
| 12 | +check_writeable() { |
| 13 | + printf "" > "$1" && rm "$1" |
| 14 | +} |
| 15 | + |
| 16 | +download() { |
| 17 | + dl_url="$1" |
| 18 | + dl_output="$2" |
| 19 | + |
| 20 | + test "$#" -eq "2" || die "download requires exactly two arguments, was given $@" |
| 21 | + |
| 22 | + if ! check_writeable "$dl_output"; then |
| 23 | + die "download error: cannot write to $dl_output" |
| 24 | + fi |
| 25 | + |
| 26 | + if have_binary wget; then |
| 27 | + printf '==> Using wget to download "%s" to "%s"\n' "$dl_url" "$dl_output" |
| 28 | + wget "$dl_url" -O "$dl_output" || return |
| 29 | + elif have_binary curl; then |
| 30 | + printf '==> Using curl to download "%s" to "%s"\n' "$dl_url" "$dl_output" |
| 31 | + curl --silent "$dl_url" > "$dl_output" || return |
| 32 | + elif have_binary fetch; then |
| 33 | + printf '==> Using fetch to download "%s" to "%s"\n' "$dl_url" "$dl_output" |
| 34 | + fetch "$dl_url" -o "$dl_output" || return |
| 35 | + else |
| 36 | + die "no binary found to download $dl_url. exiting." |
| 37 | + fi |
| 38 | + echo "==> download complete!" |
| 39 | +} |
| 40 | + |
| 41 | +unarchive() { |
| 42 | + ua_archivetype="$1" |
| 43 | + ua_infile="$2" |
| 44 | + ua_outfile="$3" |
| 45 | + ua_distname="$4" |
| 46 | + |
| 47 | + if ! check_writeable "$ua_outfile"; then |
| 48 | + die "unarchive error: cannot write to $ua_outfile" |
| 49 | + fi |
| 50 | + |
| 51 | + case "$ua_archivetype" in |
| 52 | + tar.gz) |
| 53 | + if have_binary tar; then |
| 54 | + echo "==> using 'tar' to extract binary from archive" |
| 55 | + cat "$ua_infile" | tar -O -z -x "$ua_distname/$ua_distname" > "$ua_outfile" |
| 56 | + else |
| 57 | + die "no binary on system for extracting tar files" |
| 58 | + fi |
| 59 | + ;; |
| 60 | + zip) |
| 61 | + if have_binary unzip; then |
| 62 | + echo "==> using 'unzip' to extract binary from archive" |
| 63 | + unzip -p "$ua_infile" "$ua_distname/$ua_distname" > "$ua_outfile" |
| 64 | + else |
| 65 | + die "no installed method for extracting .zip archives" |
| 66 | + fi |
| 67 | + ;; |
| 68 | + *) |
| 69 | + die "unrecognized archive type '$ua_archivetype'" |
| 70 | + esac |
| 71 | + |
| 72 | + chmod +x "$ua_outfile" |
| 73 | +} |
| 74 | + |
| 75 | +get_go_vars() { |
| 76 | + if [ ! -z "$GOOS" ] && [ ! -z "$GOARCH" ]; then |
| 77 | + printf "%s-%s" "$GOOS" "$GOARCH" |
| 78 | + fi |
| 79 | + |
| 80 | + if have_binary go; then |
| 81 | + printf "%s-%s" "$(go env GOOS)" "$(go env GOARCH)" |
| 82 | + else |
| 83 | + die "no way of determining system GOOS and GOARCH\nPlease manually set GOOS and GOARCH then retry." |
| 84 | + fi |
| 85 | +} |
| 86 | + |
| 87 | +mkurl() { |
| 88 | + m_name="$1" |
| 89 | + m_vers="$2" |
| 90 | + m_archive="$3" |
| 91 | + m_govars=$(get_go_vars) || die "could not get go env vars" |
| 92 | + |
| 93 | + echo "http://dist.ipfs.io/$m_name/$m_vers/${m_name}_${m_vers}_$m_govars.$m_archive" |
| 94 | +} |
| 95 | + |
| 96 | +distname="$1" |
| 97 | +outpath="$2" |
| 98 | +version="$3" |
| 99 | + |
| 100 | +if [ -z "$distname" ] || [ -z "$outpath" ] || [ -z "$version" ]; then |
| 101 | + die "usage: dist_get <distname> <outpath> <version>" |
| 102 | +fi |
| 103 | + |
| 104 | +case $version in |
| 105 | + v*) |
| 106 | + # correct input |
| 107 | + ;; |
| 108 | + *) |
| 109 | + echo "invalid version '$version'" >&2 |
| 110 | + die "versions must begin with 'v', for example: v0.4.0" |
| 111 | + ;; |
| 112 | +esac |
| 113 | + |
| 114 | +# TODO: don't depend on the go tool being installed to detect this |
| 115 | +goenv=$(get_go_vars) || die "could not get go env vars" |
| 116 | + |
| 117 | +case $goenv in |
| 118 | + linux-*) |
| 119 | + archive="tar.gz" |
| 120 | + ;; |
| 121 | + darwin-*) |
| 122 | + archive="tar.gz" |
| 123 | + ;; |
| 124 | + windows-*) |
| 125 | + archive="zip" |
| 126 | + ;; |
| 127 | + freebsd-*) |
| 128 | + archive="tar.gz" |
| 129 | + ;; |
| 130 | + *) |
| 131 | + echo "unrecognized system environment: $goenv" >&2 |
| 132 | + die "currently only linux, darwin, windows and freebsd are supported by this script" |
| 133 | +esac |
| 134 | + |
| 135 | + |
| 136 | +mkdir -p bin/tmp |
| 137 | + |
| 138 | +url=$(mkurl "$distname" "$version" "$archive") |
| 139 | +tmpfi="bin/tmp/$distname.$archive" |
| 140 | + |
| 141 | +download "$url" "$tmpfi" |
| 142 | +if [ $? -ne 0 ]; then |
| 143 | + die "failed to download $url to $tmpfi" |
| 144 | +fi |
| 145 | + |
| 146 | +unarchive "$archive" "$tmpfi" "$outpath" "$distname" |
| 147 | +if [ $? -ne 0 ]; then |
| 148 | + die "failed to extract archive $tmpfi" |
| 149 | +fi |
0 commit comments