forked from Masterminds/glide.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget
executable file
·186 lines (164 loc) · 4.7 KB
/
get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/sh
# The install script is licensed under the MIT license Glide itself is under.
# See https://github.com/Masterminds/glide/blob/master/LICENSE for more details.
# To run this script execute:
# `curl https://glide.sh/get | sh`
PROJECT_NAME="glide"
# LGOBIN represents the local bin location. This can be either the GOBIN, if set,
# or the GOPATH/bin.
LGOBIN=""
fail() {
echo "$1"
exit 1
}
verifyGoInstallation() {
GO=$(which go)
if [ "$?" = "1" ]; then
fail "$PROJECT_NAME needs go. Please install it first."
fi
if [ -z "$GOPATH" ]; then
# Verify if default GOPATH of Go >=1.8 is used
GOPATH=$(go env GOPATH)
if [ ! -d "$GOPATH" ]; then
fail "$PROJECT_NAME needs environment variable "'$GOPATH'". Set it before continue."
fi
fi
if [ -n "$GOBIN" ]; then
if [ ! -d "$GOBIN" ]; then
fail "$GOBIN "'($GOBIN)'" folder not found. Please create it before continue."
fi
LGOBIN="$GOBIN"
else
if [ ! -d "$GOPATH/bin" ]; then
fail "$GOPATH/bin "'($GOPATH/bin)'" folder not found. Please create it before continue."
fi
LGOBIN="$GOPATH/bin"
fi
}
initArch() {
ARCH=$(uname -m)
case $ARCH in
armv5*) ARCH="armv5";;
armv6*) ARCH="armv6";;
armv7*) ARCH="armv7";;
aarch64) ARCH="arm64";;
x86) ARCH="386";;
x86_64) ARCH="amd64";;
i686) ARCH="386";;
i386) ARCH="386";;
esac
echo "ARCH=$ARCH"
}
initOS() {
OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')
case "$OS" in
# Minimalist GNU for Windows
mingw*) OS='windows';;
msys*) OS='windows';;
esac
echo "OS=$OS"
}
initDownloadTool() {
if type "curl" > /dev/null; then
DOWNLOAD_TOOL="curl"
elif type "wget" > /dev/null; then
DOWNLOAD_TOOL="wget"
else
fail "You need curl or wget as download tool. Please install it first before continue"
fi
echo "Using $DOWNLOAD_TOOL as download tool"
}
get() {
local url="$2"
local body
local httpStatusCode
echo "Getting $url"
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
httpResponse=$(curl -sL --write-out HTTPSTATUS:%{http_code} "$url")
httpStatusCode=$(echo $httpResponse | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
body=$(echo "$httpResponse" | sed -e 's/HTTPSTATUS\:.*//g')
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
tmpFile=$(mktemp)
body=$(wget --server-response --content-on-error -q -O - "$url" 2> $tmpFile || true)
httpStatusCode=$(cat $tmpFile | awk '/^ HTTP/{print $2}')
fi
if [ "$httpStatusCode" != 200 ]; then
echo "Request fail with http status code $httpStatusCode"
fail "Body: $body"
fi
eval "$1='$body'"
}
getFile() {
local url="$1"
local filePath="$2"
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
httpStatusCode=$(curl -s -w '%{http_code}' -L "$url" -o "$filePath")
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
body=$(wget --server-response --content-on-error -q -O "$filePath" "$url")
httpStatusCode=$(cat $tmpFile | awk '/^ HTTP/{print $2}')
fi
echo "$httpStatusCode"
}
downloadFile() {
TAG=v0.13.3
echo "TAG=$TAG"
GLIDE_DIST="glide-$TAG-$OS-$ARCH.tar.gz"
echo "GLIDE_DIST=$GLIDE_DIST"
DOWNLOAD_URL="https://github.com/Masterminds/$PROJECT_NAME/releases/download/$TAG/$GLIDE_DIST"
GLIDE_TMP_FILE="/tmp/$GLIDE_DIST"
echo "Downloading $DOWNLOAD_URL"
httpStatusCode=$(getFile "$DOWNLOAD_URL" "$GLIDE_TMP_FILE")
if [ "$httpStatusCode" -ne 200 ]; then
echo "Did not find a release for your system: $OS $ARCH"
echo "Trying to find a release on the github api."
LATEST_RELEASE_URL="https://api.github.com/repos/Masterminds/$PROJECT_NAME/releases/tags/$TAG"
echo "LATEST_RELEASE_URL=$LATEST_RELEASE_URL"
get LATEST_RELEASE_JSON $LATEST_RELEASE_URL
# || true forces this command to not catch error if grep does not find anything
DOWNLOAD_URL=$(echo "$LATEST_RELEASE_JSON" | grep 'browser_' | cut -d\" -f4 | grep "$GLIDE_DIST") || true
if [ -z "$DOWNLOAD_URL" ]; then
echo "Sorry, we dont have a dist for your system: $OS $ARCH"
fail "You can ask one here: https://github.com/Masterminds/$PROJECT_NAME/issues"
else
echo "Downloading $DOWNLOAD_URL"
getFile "$DOWNLOAD_URL" "$GLIDE_TMP_FILE"
fi
fi
}
installFile() {
GLIDE_TMP="/tmp/$PROJECT_NAME"
mkdir -p "$GLIDE_TMP"
tar xf "$GLIDE_TMP_FILE" -C "$GLIDE_TMP"
GLIDE_TMP_BIN="$GLIDE_TMP/$OS-$ARCH/$PROJECT_NAME"
cp "$GLIDE_TMP_BIN" "$LGOBIN"
rm -rf $GLIDE_TMP
rm -f $GLIDE_TMP_FILE
}
bye() {
result=$?
if [ "$result" != "0" ]; then
echo "Fail to install $PROJECT_NAME"
fi
exit $result
}
testVersion() {
set +e
GLIDE="$(which $PROJECT_NAME)"
if [ "$?" = "1" ]; then
fail "$PROJECT_NAME not found. Did you add "'$GOBIN'" to your "'$PATH?'
fi
set -e
GLIDE_VERSION=$($PROJECT_NAME -v)
echo "$GLIDE_VERSION installed successfully"
}
# Execution
#Stop execution on any error
trap "bye" EXIT
verifyGoInstallation
set -e
initArch
initOS
initDownloadTool
downloadFile
installFile
testVersion