forked from fossas/fossa-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·129 lines (112 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
#!/bin/bash
# This is derived from https://github.com/jpillora/installer
# Original license notice:
# ---
# Copyright © 2016 Jaime Pillora <[email protected]>
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ---
TMP_DIR="/tmp/install-fossa-cli"
function cleanup {
rm -rf $TMP_DIR > /dev/null
}
trap cleanup EXIT
function fail {
msg=$1
echo "============"
echo "Error: $msg" 1>&2
exit 1
}
# This function will ask for root privileges before executing a command
# The goal is to allow the user to run this script as a normal user and
# to be asked for authorizations as needed
function askRoot {
if [ $(id -u) -eq 0 ]; then
"$@"
else
echo "The following command needs administrator privileges:"
echo
echo -e "\\t$*"
echo
# The -k flag forces sudo to re-ask the user for their authorization
if command -v sudo > /dev/null; then
sudo -k "$@"
elif command -v su > /dev/null; then
su root -c "/bin/bash $@"
else
fail "neither sudo nor su are installed"
fi
fi
}
function install {
# Settings
USER="fossas"
REPO="fossa-cli"
BIN="fossa"
INSECURE="false"
OUT_DIR="/usr/local/bin"
GH="https://github.com"
GH_API="https://api.github.com"
# `bash` check
[ ! "$BASH_VERSION" ] && fail "Please use bash instead"
[ ! -d $OUT_DIR ] && fail "output directory missing: $OUT_DIR"
# Check for non-POSIX dependencies
GET=""
if command -v curl > /dev/null; then
GET="curl"
if [[ $INSECURE = "true" ]]; then GET="$GET --insecure"; fi
GET="$GET --fail -# -L"
elif command -v wget > /dev/null; then
GET="wget"
if [[ $INSECURE = "true" ]]; then GET="$GET --no-check-certificate"; fi
GET="$GET -qO-"
else
fail "neither wget nor curl are installed"
fi
command -v tar > /dev/null || fail "tar is not installed"
command -v gzip > /dev/null || fail "gzip is not installed"
# Detect OS
case $(uname -s) in
Darwin) OS="darwin";;
Linux) OS="linux";;
*) fail "unknown os: $(uname -s)";;
esac
# Detect architecture
if uname -m | grep 64 > /dev/null; then
ARCH="amd64"
elif uname -m | grep arm > /dev/null; then
ARCH="arm"
elif uname -m | grep 386 > /dev/null; then
ARCH="386"
else
fail "unknown arch: $(uname -m)"
fi
# Fail for unsupported OS/architecture combinations
case "${OS}_${ARCH}" in
"darwin_amd64") ;;
"linux_amd64") ;;
"windows_amd64") ;;
*) fail "No asset for platform ${OS}-${ARCH}";;
esac
# Enter temporary directory
mkdir -p $TMP_DIR
cd $TMP_DIR || fail "changing directory to $TMP_DIR failed"
# Download and validate release
bash -c "$GET $GH_API/repos/$USER/$REPO/releases/latest" > latest || fail "downloading latest release metadata failed"
RELEASE=$(grep tag_name latest | cut -d'"' -f4)
VERSION=${RELEASE#v} # remove prefix "v"
echo "Installing $USER/$REPO $RELEASE..."
RELEASE_URL="$GH/$USER/$REPO/releases/download/$RELEASE"
bash -c "$GET $RELEASE_URL/${REPO}_${VERSION}_${OS}_${ARCH}.tar.gz" > release.tar.gz || fail "downloading release failed"
bash -c "$GET $RELEASE_URL/${REPO}_${VERSION}_checksums.txt" > checksums.txt || fail "downloading checksums failed"
# Extract release
tar zxf release.tar.gz || fail "tar failed"
rm release.tar.gz
# Move binary into output directory
chmod +x $BIN || fail "chmod +x failed"
# Admin privileges are required to run this command
askRoot mv $BIN $OUT_DIR/$BIN || fail "mv failed"
echo "Installed at $OUT_DIR/$BIN"
}
install