-
Notifications
You must be signed in to change notification settings - Fork 241
/
install.sh
executable file
·138 lines (120 loc) · 3.51 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
#!/bin/bash
set -euo pipefail
declare BINARY_NAME
E_UNSUPPORTED_ARCH=5
CHECKSUM_FILE_NAME='checksums'
DEBUG=${DEBUG:-''}
VERSION=${VERSION:-'latest'}
INSTALL_ORG_REPO=${INSTALL_ORG_REPO:-'thoughtworks/talisman'}
INSTALL_LOCATION=${INSTALL_LOCATION:-'/usr/local/bin'}
function echo_error() {
echo -ne "$(tput setaf 1)" >&2
echo "$1" >&2
echo -ne "$(tput sgr0)" >&2
}
function echo_debug() {
[[ -z "$DEBUG" ]] && return
echo -ne "$(tput setaf 3)" >&2
echo "$1" >&2
echo -ne "$(tput sgr0)" >&2
}
function echo_success() {
echo -ne "$(tput setaf 2)"
echo "$1" >&2
echo -ne "$(tput sgr0)"
}
function operating_system() {
OS=$(uname -s)
case $OS in
"Linux")
echo "linux"
;;
"Darwin")
echo "darwin"
;;
MINGW32_NT-* | MINGW64_NT-* | MSYS_NT-*)
echo "windows"
;;
*)
echo_error "Talisman currently only supports Windows, Linux, and MacOS (darwin) systems."
echo_error "If this is a problem for you, please open an issue: https://github.com/$INSTALL_ORG_REPO/issues/new"
exit $E_UNSUPPORTED_ARCH
;;
esac
}
function architecture() {
ARCH=$(uname -m)
case $ARCH in
"x86_64")
echo "amd64"
;;
"i686" | "i386")
echo "386"
;;
"arm64" | "aarch64")
echo "arm64"
;;
*)
echo_error "Talisman currently only supports x86 and x86_64 and arm64 architectures."
echo_error "If this is a problem for you, please open an issue: https://github.com/$INSTALL_ORG_REPO/issues/new"
exit $E_UNSUPPORTED_ARCH
;;
esac
}
function set_binary_name() {
BINARY_NAME="talisman_$(operating_system)_$(architecture)"
if [ "$(operating_system)" = "windows" ]; then
BINARY_NAME="$BINARY_NAME.exe"
fi
echo_success "Selected $BINARY_NAME"
}
function download() {
ASSETS=$(curl -Ls https://api.github.com/repos/"$INSTALL_ORG_REPO"/releases/latest |
grep download_url | awk '{print $2}' | tr -d '"')
BINARY_URL=$(echo "$ASSETS" | grep "$BINARY_NAME")
CHECKSUM_URL=$(echo "$ASSETS" | grep $CHECKSUM_FILE_NAME)
echo_debug "Downloading $BINARY_NAME and from $BINARY_URL"
curl --location --silent "$BINARY_URL" >"$TEMP_DIR/$BINARY_NAME"
echo_debug "Downloading $CHECKSUM_FILE_NAME and from $CHECKSUM_URL"
curl --location --silent "$CHECKSUM_URL" >"$TEMP_DIR/$CHECKSUM_FILE_NAME"
echo_success "Downloaded talisman binary and checksums"
}
function verify_checksum() {
pushd "$TEMP_DIR" >/dev/null 2>&1
if ! command -v shasum &> /dev/null; then
sha256sum --ignore-missing -c $CHECKSUM_FILE_NAME
else
shasum -a 256 --ignore-missing -c $CHECKSUM_FILE_NAME
fi
popd >/dev/null 2>&1
echo_success "Checksum OK"
}
function install() {
if (touch "$INSTALL_LOCATION/$BINARY_NAME" &>/dev/null); then
cp "$TEMP_DIR/$BINARY_NAME" "$INSTALL_LOCATION/$BINARY_NAME"
chmod +x "$INSTALL_LOCATION/$BINARY_NAME"
ln -s "$INSTALL_LOCATION/$BINARY_NAME" "$INSTALL_LOCATION/talisman"
elif (which sudo &>/dev/null); then
sudo cp "$TEMP_DIR/$BINARY_NAME" "$INSTALL_LOCATION/$BINARY_NAME"
sudo chmod +x "$INSTALL_LOCATION/$BINARY_NAME"
sudo ln -s "$INSTALL_LOCATION/$BINARY_NAME" "$INSTALL_LOCATION/talisman"
else
echo_error "Insufficient permission to install to $INSTALL_LOCATION"
exit 126
fi
}
function run() {
TEMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'talisman_setup')
# shellcheck disable=SC2064
trap "rm -r $TEMP_DIR" EXIT
chmod 0700 "$TEMP_DIR"
if [ ! -d "$INSTALL_LOCATION" ]; then
echo_error "$INSTALL_LOCATION is not a directory!"
exit 1
fi
set_binary_name
download
verify_checksum
install
}
run