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