-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·128 lines (112 loc) · 3.09 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
#!/bin/bash
# The install script is licensed under the CC-0 1.0 license.
# See https://github.com/kdash-rs/kdash/blob/main/LICENSE for more details.
#
# To run this script execute:
# `curl https://raw.githubusercontent.com/kdash-rs/kdash/main/deployment/getLatest.sh | sh`
GITHUB_REPO="cloudcost"
GITHUB_USER="muandane"
EXE_FILENAME="cloudcost"
EXE_DEST_DIR="/usr/local/bin"
bye() {
result=$?
if [ "$result" != "0" ]; then
echo "Fail to install ${GITHUB_USER}/${GITHUB_REPO}"
fi
exit $result
}
fail() {
echo "$1"
exit 1
}
find_download_url() {
local SUFFIX=$1
local LATEST_URL="https://api.github.com/repos/${GITHUB_USER}/${GITHUB_REPO}/releases/latest"
local URL=$(curl -s "${LATEST_URL}" | grep "browser_download_url.*${SUFFIX}" | cut -d : -f 2,3 |
tr -d \" |
head -n 1)
echo "${URL//[[:space:]]/}"
}
find_arch() {
local 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
}
find_os() {
local OS=$(echo $(uname) | tr '[:upper:]' '[:lower:]')
case "$OS" in
# Minimalist GNU for Windows
mingw*) OS='windows' ;;
msys*) OS='windows' ;;
esac
echo $OS
}
find_suffix() {
local OS=$2
local SUFFIX="$OS.tar.gz"
# case "$OS" in
# "linux") SUFFIX='linux-musl.tar.gz' ;;
# "darwin") SUFFIX='macos.tar.gz' ;;
# "windows") SUFFIX='windows.tar.gz';;
# esac
echo $SUFFIX
}
download_file() {
local FILE_URL="$1"
local FILE_PATH="$2"
echo "Getting $FILE_URL ....."
httpStatusCode=$(curl -s -w '%{http_code}' -L "$FILE_URL" -o "$FILE_PATH")
if [ "$httpStatusCode" != 200 ]; then
echo "failed to download '${URL}'"
fail "Request fail with http status code $httpStatusCode"
fi
}
find_exec_dest_path() {
if [ ! -w $EXE_DEST_DIR ]; then
echo "Cannot write to ${EXE_DEST_DIR}. Run with 'sudo' to install to ${EXE_DEST_DIR}. Installing to current directory now ....."
EXE_DEST_DIR=$(pwd)
fi
}
install_file() {
local FILE_PATH=$1
local EXE_DEST_FILE=$2
TMP_DIR="/tmp/${GITHUB_USER}_${GITHUB_REPO}"
mkdir -p "$TMP_DIR" || true
tar xf "$FILE_PATH" -C "$TMP_DIR"
cp "$TMP_DIR/${EXE_FILENAME}" "${EXE_DEST_FILE}"
chmod +x "${EXE_DEST_FILE}"
rm -rf "$TMP_DIR"
}
main() {
find_exec_dest_path
local EXE_DEST_FILE="${EXE_DEST_DIR}/${EXE_FILENAME}"
local ARCH=$(find_arch)
local OS=$(find_os)
local SUFFIX=$(find_suffix $ARCH $OS)
local FILE_URL=$(find_download_url $SUFFIX)
if [ -z "${FILE_URL}" ]; then
fail "Did not find a latest release for your system: $OS $ARCH ($SUFFIX)"
fi
local FILE_PATH="/tmp/${GITHUB_USER}-${GITHUB_REPO}-latest-${SUFFIX}"
download_file "${FILE_URL}" "${FILE_PATH}"
install_file "${FILE_PATH}" "${EXE_DEST_FILE}"
rm -Rf ${FILE_PATH}
echo "executable installed at ${EXE_DEST_FILE}"
bye
}
#TODO check bash is used `readlink /proc/$$/exe`
# because the script is not compatible with dash (default sh on ubuntu), other posix only shell,...
#Stop execution on any error
trap "bye" EXIT
set -e
# set -x
main