forked from civo/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·201 lines (163 loc) · 4.38 KB
/
install.sh
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/sh
set -e
# Copyright Civo Ltd 2020, all rights reserved
export VERIFY_CHECKSUM=0
export OWNER="civo"
export REPO="cli"
export SUCCESS_CMD="$OWNER version"
export BINLOCATION="/usr/local/bin"
###############################
# Get the last version #
###############################
get_last_version() {
VERSION=""
echo "Finding latest version from GitHub"
VERSION=$(curl -sI https://github.com/$OWNER/$REPO/releases/latest | grep -i "location:" | awk -F"/" '{ printf "%s", $NF }' | tr -d '\r')
VERSION_NUMBER=$(echo "$VERSION" | cut -d "v" -f 2)
echo "$VERSION_NUMBER"
if [ ! "$VERSION" ]; then
echo "Failed while attempting to install $REPO. Please manually install:"
echo ""
echo "1. Open your web browser and go to https://github.com/$OWNER/$REPO/releases"
echo "2. Download the latest release for your platform. Call it '$REPO'."
echo "3. chmod +x ./$REPO"
echo "4. mv ./$REPO $BINLOCATION"
if [ -n "$ALIAS_NAME" ]; then
echo "5. ln -sf $BINLOCATION/$REPO /usr/local/bin/$ALIAS_NAME"
fi
exit 1
fi
}
###############################
# Check for curl #
###############################
hasCurl() {
which curl
if [ "$?" = "1" ]; then
echo "You need curl to use this script."
exit 1
fi
}
# --- set arch and suffix, fatal if architecture not supported ---
setup_verify_arch() {
if [ -z "$ARCH" ]; then
ARCH=$(uname -m)
fi
case $ARCH in
amd64)
ARCH=-amd64
SUFFIX=
;;
x86_64)
ARCH=-amd64
SUFFIX=
;;
arm64)
ARCH=-arm64
;;
aarch64)
ARCH=-arm64
;;
arm*)
ARCH=-arm
SUFFIX=
;;
*)
fatal "Unsupported architecture $ARCH"
;;
esac
}
setup_verify_os() {
if [ -z "$SUFFIX" ]; then
SUFFIX=$(uname -s)
fi
case $SUFFIX in
"Darwin")
SUFFIX="-darwin"
;;
"MINGW"*)
SUFFIX="windows"
;;
"Linux")
SUFFIX="-linux"
;;
*)
fatal "Unsupported OS $SUFFIX"
;;
esac
}
download() {
URL=https://github.com/$OWNER/$REPO/releases/download/$VERSION/$OWNER-$VERSION_NUMBER$SUFFIX$ARCH.tar.gz
TARGETFILE="/tmp/$OWNER-$VERSION_NUMBER$SUFFIX$ARCH.tar.gz"
echo "Downloading package $URL to $TARGETFILE"
curl -sSL "$URL" --output "$TARGETFILE"
if [ "$VERIFY_CHECKSUM" = "1" ]; then
check_hash
fi
tar -xf /tmp/$OWNER-$VERSION_NUMBER$SUFFIX$ARCH.tar.gz -C /tmp
chmod +x /tmp/$OWNER
echo "Download complete."
if [ ! -w "$BINLOCATION" ]; then
echo
echo "============================================================"
echo " The script was run as a user who is unable to write"
echo " to $BINLOCATION. To complete the installation the"
echo " following commands may need to be run manually."
echo "============================================================"
echo
echo " sudo mv /tmp/civo $BINLOCATION/$OWNER"
if [ -n "$ALIAS_NAME" ]; then
echo " sudo ln -sf $BINLOCATION/$OWNER $BINLOCATION/$ALIAS_NAME"
fi
echo
else
echo
echo "Running with sufficient permissions to attempt to move $OWNER to $BINLOCATION"
if [ ! -w "$BINLOCATION/$OWNER" ] && [ -f "$BINLOCATION/$OWNER" ]; then
echo
echo "================================================================"
echo " $BINLOCATION/$OWNER already exists and is not writeable"
echo " by the current user. Please adjust the binary ownership"
echo " or run sh/bash with sudo."
echo "================================================================"
echo
exit 1
fi
mv /tmp/$OWNER $BINLOCATION/$OWNER
if [ "$?" = "0" ]; then
echo "New version of $OWNER installed to $BINLOCATION"
fi
if [ -e TARGETFILE ]; then
rm TARGETFILE
rm /tmp/$OWNER
fi
${SUCCESS_CMD}
fi
}
check_hash() {
SHACMD="sha256sum"
if [ ! -x "$(command -v $SHACMD)" ]; then
SHACMD="shasum -a 256"
fi
if [ -x "$(command -v "$SHACMD")" ]; then
TARGETFILEDIR=${TARGETFILE%/*}
(cd "$TARGETFILEDIR" && curl -sSL https://github.com/$OWNER/$REPO/releases/download/$VERSION/$OWNER-$VERSION_NUMBER-checksums.sha256 | $SHACMD -c >/dev/null)
if [ "$?" != "0" ]; then
rm TARGETFILE
echo "Binary checksum didn't match. Exiting"
exit 1
fi
fi
}
# Error: Show error message in red and exit
fatal() {
printf "Error: \033[31m${1}\033[39m\n"
exit 1
}
{
hasCurl
setup_verify_arch
setup_verify_os
get_last_version
download
}