|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# This install script is intended to download and install the latest available |
| 4 | +# release of the dep dependency manager for Golang. |
| 5 | +# |
| 6 | +# It attempts to identify the current platform and an error will be thrown if |
| 7 | +# the platform is not supported. |
| 8 | +# |
| 9 | +# Environment variables: |
| 10 | +# - INSTALL_DIRECTORY (optional): defaults to $GOPATH/bin |
| 11 | +# - DEP_RELEASE_TAG (optional): defaults to fetching the latest release |
| 12 | +# - DEP_OS (optional): use a specific value for OS (mostly for testing) |
| 13 | +# - DEP_ARCH (optional): use a specific value for ARCH (mostly for testing) |
| 14 | +# |
| 15 | +# You can install using this script: |
| 16 | +# $ curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh |
| 17 | + |
| 18 | +set -e |
| 19 | + |
| 20 | +RELEASES_URL="https://github.com/golang/dep/releases" |
| 21 | + |
| 22 | +downloadJSON() { |
| 23 | + url="$2" |
| 24 | + |
| 25 | + echo "Fetching $url.." |
| 26 | + if type curl > /dev/null; then |
| 27 | + response=$(curl -s -L -w 'HTTPSTATUS:%{http_code}' -H 'Accept: application/json' "$url") |
| 28 | + body=$(echo "$response" | sed -e 's/HTTPSTATUS\:.*//g') |
| 29 | + code=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') |
| 30 | + elif type wget > /dev/null; then |
| 31 | + temp=$(mktemp) |
| 32 | + body=$(wget -q --header='Accept: application/json' -O - --server-response --content-on-error "$url" 2> "$temp") |
| 33 | + code=$(awk '/^ HTTP/{print $2}' < "$temp") |
| 34 | + else |
| 35 | + echo "Neither curl nor wget was available to perform http requests." |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + if [ "$code" != 200 ]; then |
| 39 | + echo "Request failed with code $code" |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | + |
| 43 | + eval "$1='$body'" |
| 44 | +} |
| 45 | + |
| 46 | +downloadFile() { |
| 47 | + url="$1" |
| 48 | + destination="$2" |
| 49 | + |
| 50 | + echo "Fetching $url.." |
| 51 | + if type curl > /dev/null; then |
| 52 | + code=$(curl -s -w '%{http_code}' -L "$url" -o "$destination") |
| 53 | + elif type wget > /dev/null; then |
| 54 | + code=$(wget -q -O "$destination" --server-response "$url" 2>&1 | awk '/^ HTTP/{print $2}') |
| 55 | + else |
| 56 | + echo "Neither curl nor wget was available to perform http requests." |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + |
| 60 | + if [ "$code" != 200 ]; then |
| 61 | + echo "Request failed with code $code" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +findGoBinDirectory() { |
| 67 | + if [ -z "$GOPATH" ]; then |
| 68 | + echo "Installation requires \$GOPATH to be set for your Golang environment." |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + if [ -z "$GOBIN" ]; then |
| 72 | + GOBIN="$GOPATH/bin" |
| 73 | + fi |
| 74 | + if [ ! -d "$GOBIN" ]; then |
| 75 | + echo "Installation requires your GOBIN directory $GOBIN to exist. Please create it." |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + eval "$1='$GOBIN'" |
| 79 | +} |
| 80 | + |
| 81 | +initArch() { |
| 82 | + ARCH=$(uname -m) |
| 83 | + if [ -n "$DEP_ARCH" ]; then |
| 84 | + echo "Using DEP_ARCH" |
| 85 | + ARCH="$DEP_ARCH" |
| 86 | + fi |
| 87 | + case $ARCH in |
| 88 | + amd64) ARCH="amd64";; |
| 89 | + x86_64) ARCH="amd64";; |
| 90 | + i386) ARCH="386";; |
| 91 | + *) echo "Architecture ${ARCH} is not supported by this installation script"; exit 1;; |
| 92 | + esac |
| 93 | + echo "ARCH = $ARCH" |
| 94 | +} |
| 95 | + |
| 96 | +initOS() { |
| 97 | + OS=$(uname | tr '[:upper:]' '[:lower:]') |
| 98 | + if [ -n "$DEP_OS" ]; then |
| 99 | + echo "Using DEP_OS" |
| 100 | + OS="$DEP_OS" |
| 101 | + fi |
| 102 | + case "$OS" in |
| 103 | + darwin) OS='darwin';; |
| 104 | + linux) OS='linux';; |
| 105 | + freebsd) OS='freebsd';; |
| 106 | + mingw*) OS='windows';; |
| 107 | + msys*) OS='windows';; |
| 108 | + *) echo "OS ${OS} is not supported by this installation script"; exit 1;; |
| 109 | + esac |
| 110 | + echo "OS = $OS" |
| 111 | +} |
| 112 | + |
| 113 | +# identify platform based on uname output |
| 114 | +initArch |
| 115 | +initOS |
| 116 | + |
| 117 | +# determine install directory if required |
| 118 | +if [ -z "$INSTALL_DIRECTORY" ]; then |
| 119 | + findGoBinDirectory INSTALL_DIRECTORY |
| 120 | +fi |
| 121 | +echo "Will install into $INSTALL_DIRECTORY" |
| 122 | + |
| 123 | +# assemble expected release artifact name |
| 124 | +BINARY="dep-${OS}-${ARCH}" |
| 125 | + |
| 126 | +# add .exe if on windows |
| 127 | +if [ "$OS" = "windows" ]; then |
| 128 | + BINARY="$BINARY.exe" |
| 129 | +fi |
| 130 | + |
| 131 | +# if DEP_RELEASE_TAG was not provided, assume latest |
| 132 | +if [ -z "$DEP_RELEASE_TAG" ]; then |
| 133 | + downloadJSON LATEST_RELEASE "$RELEASES_URL/latest" |
| 134 | + DEP_RELEASE_TAG=$(echo "${LATEST_RELEASE}" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//' ) |
| 135 | +fi |
| 136 | +echo "Release Tag = $DEP_RELEASE_TAG" |
| 137 | + |
| 138 | +# fetch the real release data to make sure it exists before we attempt a download |
| 139 | +downloadJSON RELEASE_DATA "$RELEASES_URL/tag/$DEP_RELEASE_TAG" |
| 140 | + |
| 141 | +BINARY_URL="$RELEASES_URL/download/$DEP_RELEASE_TAG/$BINARY" |
| 142 | +DOWNLOAD_FILE=$(mktemp) |
| 143 | + |
| 144 | +downloadFile "$BINARY_URL" "$DOWNLOAD_FILE" |
| 145 | + |
| 146 | +echo "Setting executable permissions." |
| 147 | +chmod +x "$DOWNLOAD_FILE" |
| 148 | + |
| 149 | +echo "Moving executable to $INSTALL_DIRECTORY/dep" |
| 150 | +mv "$DOWNLOAD_FILE" "$INSTALL_DIRECTORY/dep" |
0 commit comments