-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathboltup.sh
executable file
·217 lines (177 loc) · 5.45 KB
/
boltup.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env bash
set -eo pipefail
# 'boltup' script for installing and managing the bolt CLI tool.
#
# This script is a heavily inspired by 'foundryup' (https://github.com/foundry-rs/foundry/blob/master/foundryup/foundryup)
# since it is a great way to install CLI tools. Kudos to the Foundry team :)
BASE_DIR=${XDG_CONFIG_HOME:-$HOME}
BOLT_DIR=${BOLT_DIR:-"$BASE_DIR/.bolt"}
BOLT_BIN_DIR="$BOLT_DIR/bin"
main() {
need_cmd git
need_cmd curl
# Loop as long as $1 (the first positional argument) is not empty, and match its
# value against existing flags. If a flag is found, the argument is discarded
# (via shift) and the value for the flag is read.
while [[ -n $1 ]]; do
case $1 in
--) shift; break;;
-t|--tag) shift; BOLTUP_TAG=$1;;
--arch) shift; BOLTUP_ARCH=$1;;
--platform) shift; BOLTUP_PLATFORM=$1;;
-h|--help)
usage
exit 0
;;
*)
warn "unknown option: $1"
usage
exit 1
esac; shift
done
# Print the banner after successfully parsing args
banner
BOLTUP_REPO="chainbound/bolt"
# Install by downloading binaries (default: "latest" tag)
BOLTUP_TAG=${BOLTUP_TAG:-latest}
# Normalize versions (handle channels, versions without v prefix
if [[ "$BOLTUP_TAG" == [[:digit:]]* ]]; then
# Add v prefix
BOLTUP_TAG="v${BOLTUP_TAG}"
fi
# Add `cli-` prefix to the tag, that's were the release is located
BOLTUP_TAG="cli-${BOLTUP_TAG}"
say "installing bolt (tag ${BOLTUP_TAG})"
# Figure out the platform: one of "linux", "darwin" or "win32"
uname_s=$(uname -s)
PLATFORM=$(tolower "${BOLTUP_PLATFORM:-$uname_s}")
EXT="tar.gz"
case $PLATFORM in
linux) ;;
darwin | mac*)
PLATFORM="darwin"
;;
mingw* | win*)
# revert, as Windows is not supported currently
# TODO: add Windows binaries and support
err "Windows is not supported yet!"
EXT="zip"
PLATFORM="win32"
;;
*)
err "unsupported platform: $PLATFORM"
;;
esac
# Figure out the architecture: one of "amd64" or "arm64"
uname_m=$(uname -m)
ARCHITECTURE=$(tolower "${BOLTUP_ARCH:-$uname_m}")
if [ "${ARCHITECTURE}" = "x86_64" ]; then
# Redirect stderr to /dev/null to avoid printing errors if non Rosetta.
if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null)" = "1" ]; then
ARCHITECTURE="arm64" # Rosetta.
else
ARCHITECTURE="amd64" # Intel.
fi
elif [ "${ARCHITECTURE}" = "arm64" ] || [ "${ARCHITECTURE}" = "aarch64" ]; then
ARCHITECTURE="arm64" # Arm.
else
ARCHITECTURE="amd64" # Amd.
fi
# Compute the URL of the release tarball in the Bolt repository.
RELEASE_URL="https://github.com/${BOLTUP_REPO}/releases/download/${BOLTUP_TAG}/"
# Examples: "bolt-cli-amd64-darwin.tar.gz" or "bolt-cli-arm64-linux.tar.gz"
BIN_FILENAME="bolt-cli-${ARCHITECTURE}-${PLATFORM}.$EXT"
BIN_ARCHIVE_URL="${RELEASE_URL}${BIN_FILENAME}"
# Download and extract the tarball.
if [ "$PLATFORM" = "win32" ]; then
tmp="$(mktemp -d 2>/dev/null || echo ".")/bolt.zip"
ensure download "$BIN_ARCHIVE_URL" "$tmp"
ensure unzip "$tmp" -d "$BOLT_BIN_DIR"
rm -f "$tmp"
else
ensure download "$BIN_ARCHIVE_URL" | ensure tar -xzC "$BOLT_BIN_DIR"
fi
bin_path="$BOLT_BIN_DIR/bolt"
# Print installed msg
say "installed - $(ensure "$bin_path" --version)"
# Check if the default path of the binary is not in BOLT_BIN_DIR
which_path="$(command -v "$bin" || true)"
if [ -n "$which_path" ] && [ "$which_path" != "$bin_path" ]; then
warn ""
cat 1>&2 <<EOF
There are multiple binaries with the name 'bolt' present in your 'PATH'.
This may be the result of installing 'bolt' using another method,
like Cargo or other package managers.
You may need to run 'rm $which_path' or move '$BOLT_BIN_DIR'
in your 'PATH' to allow the newly installed version to take precedence!
EOF
fi
say "done!"
}
usage() {
cat 1>&2 <<EOF
The installer for bolt CLI.
Update or revert to a specific bolt version with ease.
By default, the latest unstable version is installed from built binaries.
USAGE:
boltup <OPTIONS>
OPTIONS:
-h, --help Print help information
-t, --tag Install a specific version from built binaries (default: latest)
--arch Install a specific architecture (supports amd64 and arm64)
--platform Install a specific platform (supports linux, and darwin)
EOF
}
say() {
printf "boltup: %s\n" "$1"
}
warn() {
say "warning: ${1}" >&2
}
err() {
say "$1" >&2
exit 1
}
tolower() {
echo "$1" | awk '{print tolower($0)}'
}
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
check_cmd() {
command -v "$1" &>/dev/null
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing command.
ensure() {
if ! "$@"; then err "command failed: $*"; fi
}
# Downloads $1 into $2 or stdout
download() {
if [ -n "$2" ]; then
# output into $2
if check_cmd curl; then
curl -#o "$2" -L "$1"
else
wget --show-progress -qO "$2" "$1"
fi
else
# output to stdout
if check_cmd curl; then
curl -#L "$1"
else
wget --show-progress -qO- "$1"
fi
fi
}
# Banner Function for Bolt
banner() {
printf '
.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx
Bolt: Permissionless proposr commitments on Ethereum.
.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx.xOx
'
}
main "$@"