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
18set -e
19
20RELEASES_URL="https://github.com/golang/dep/releases"
21
22downloadJSON() {
23    url="$2"
24
25    echo "Fetching $url.."
26    if test -x "$(command -v curl)"; 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 test -x "$(command -v wget)"; then
31        temp=$(mktemp)
32        body=$(wget -q --header='Accept: application/json' -O - --server-response "$url" 2> "$temp")
33        code=$(awk '/^  HTTP/{print $2}' < "$temp" | tail -1)
34        rm "$temp"
35    else
36        echo "Neither curl nor wget was available to perform http requests."
37        exit 1
38    fi
39    if [ "$code" != 200 ]; then
40        echo "Request failed with code $code"
41        exit 1
42    fi
43
44    eval "$1='$body'"
45}
46
47downloadFile() {
48    url="$1"
49    destination="$2"
50
51    echo "Fetching $url.."
52    if test -x "$(command -v curl)"; then
53        code=$(curl -s -w '%{http_code}' -L "$url" -o "$destination")
54    elif test -x "$(command -v wget)"; then
55        code=$(wget -q -O "$destination" --server-response "$url" 2>&1 | awk '/^  HTTP/{print $2}' | tail -1)
56    else
57        echo "Neither curl nor wget was available to perform http requests."
58        exit 1
59    fi
60
61    if [ "$code" != 200 ]; then
62        echo "Request failed with code $code"
63        exit 1
64    fi
65}
66
67findGoBinDirectory() {
68    EFFECTIVE_GOPATH=$(go env GOPATH)
69    # CYGWIN: Convert Windows-style path into sh-compatible path
70    if [ "$OS_CYGWIN" = "1" ]; then
71	EFFECTIVE_GOPATH=$(cygpath "$EFFECTIVE_GOPATH")
72    fi
73    if [ -z "$EFFECTIVE_GOPATH" ]; then
74        echo "Installation could not determine your \$GOPATH."
75        exit 1
76    fi
77    if [ -z "$GOBIN" ]; then
78        GOBIN=$(echo "${EFFECTIVE_GOPATH%%:*}/bin" | sed s#//*#/#g)
79    fi
80    if [ ! -d "$GOBIN" ]; then
81        echo "Installation requires your GOBIN directory $GOBIN to exist. Please create it."
82        exit 1
83    fi
84    eval "$1='$GOBIN'"
85}
86
87initArch() {
88    ARCH=$(uname -m)
89    if [ -n "$DEP_ARCH" ]; then
90        echo "Using DEP_ARCH"
91        ARCH="$DEP_ARCH"
92    fi
93    case $ARCH in
94        amd64) ARCH="amd64";;
95        x86_64) ARCH="amd64";;
96        i386) ARCH="386";;
97        ppc64) ARCH="ppc64";;
98        ppc64le) ARCH="ppc64le";;
99        s390x) ARCH="s390x";;
100        armv6*) ARCH="arm";;
101        armv7*) ARCH="arm";;
102        aarch64) ARCH="arm64";;
103        *) echo "Architecture ${ARCH} is not supported by this installation script"; exit 1;;
104    esac
105    echo "ARCH = $ARCH"
106}
107
108initOS() {
109    OS=$(uname | tr '[:upper:]' '[:lower:]')
110    OS_CYGWIN=0
111    if [ -n "$DEP_OS" ]; then
112        echo "Using DEP_OS"
113        OS="$DEP_OS"
114    fi
115    case "$OS" in
116        darwin) OS='darwin';;
117        linux) OS='linux';;
118        freebsd) OS='freebsd';;
119        mingw*) OS='windows';;
120        msys*) OS='windows';;
121	cygwin*)
122	    OS='windows'
123	    OS_CYGWIN=1
124	    ;;
125        *) echo "OS ${OS} is not supported by this installation script"; exit 1;;
126    esac
127    echo "OS = $OS"
128}
129
130# identify platform based on uname output
131initArch
132initOS
133
134# determine install directory if required
135if [ -z "$INSTALL_DIRECTORY" ]; then
136    findGoBinDirectory INSTALL_DIRECTORY
137fi
138echo "Will install into $INSTALL_DIRECTORY"
139
140# assemble expected release artifact name
141if [ "${OS}" != "linux" ] && { [ "${ARCH}" = "ppc64" ] || [ "${ARCH}" = "ppc64le" ];}; then
142    # ppc64 and ppc64le are only supported on Linux.
143    echo "${OS}-${ARCH} is not supported by this instalation script"
144else
145    BINARY="dep-${OS}-${ARCH}"
146fi
147
148# add .exe if on windows
149if [ "$OS" = "windows" ]; then
150    BINARY="$BINARY.exe"
151fi
152
153# if DEP_RELEASE_TAG was not provided, assume latest
154if [ -z "$DEP_RELEASE_TAG" ]; then
155    downloadJSON LATEST_RELEASE "$RELEASES_URL/latest"
156    DEP_RELEASE_TAG=$(echo "${LATEST_RELEASE}" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//' )
157fi
158echo "Release Tag = $DEP_RELEASE_TAG"
159
160# fetch the real release data to make sure it exists before we attempt a download
161downloadJSON RELEASE_DATA "$RELEASES_URL/tag/$DEP_RELEASE_TAG"
162
163BINARY_URL="$RELEASES_URL/download/$DEP_RELEASE_TAG/$BINARY"
164DOWNLOAD_FILE=$(mktemp)
165
166downloadFile "$BINARY_URL" "$DOWNLOAD_FILE"
167
168echo "Setting executable permissions."
169chmod +x "$DOWNLOAD_FILE"
170
171INSTALL_NAME="dep"
172
173if [ "$OS" = "windows" ]; then
174    INSTALL_NAME="$INSTALL_NAME.exe"
175fi
176
177echo "Moving executable to $INSTALL_DIRECTORY/$INSTALL_NAME"
178mv "$DOWNLOAD_FILE" "$INSTALL_DIRECTORY/$INSTALL_NAME"
179