-
Notifications
You must be signed in to change notification settings - Fork 2
/
apibara_install.sh
145 lines (117 loc) · 3.32 KB
/
apibara_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
#!/usr/bin/env bash
# shellcheck shell=dash
set -euo pipefail
DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}"
APIBARA_ROOT_DIR="${APIBARA_ROOT_DIR:-$DATA_DIR/apibara}"
APIBARA_REPO="${APIBARA_REPO:-apibara/dna}"
main() {
say "installing Apibara CLI to ${APIBARA_ROOT_DIR}"
need_cmd curl
need_cmd jq
need_cmd gzip
get_arch || exit 1
local _arch="$RETVAL"
assert_nz "$_arch" "arch"
local _release_tag="cli/v0.4.2"
echo "release tag: $_release_tag"
assert_nz "$_release_tag" "release tag"
local _release_version="${_release_tag#cli/v}"
assert_nz "$_release_version" "release version"
say "installing CLI version $_release_version for $_arch"
# https://github.com/apibara/dna/releases/download/cli%2Fv0.4.2/cli-aarch64-linux.gz
local _release_url="https://github.com/apibara/dna/releases/download/$_release_tag/cli-$_arch.gz"
local _bin_dir="${APIBARA_ROOT_DIR}/bin"
mkdir -p "$_bin_dir"
ensure curl -Ls "$_release_url" > "$_bin_dir/apibara.gz"
ensure gzip -f -d "$_bin_dir/apibara.gz"
ensure chmod +x "$_bin_dir/apibara"
say "checking installation"
ensure "$_bin_dir/apibara" --version
say "adding installation to PATH"
local _profile _shell
case "$SHELL" in
*/bash)
_profile="$HOME/.bashrc"
_shell="bash"
;;
*/zsh)
_profile="${ZDOTDIR:-$HOME}/.zshenv"
_shell="zsh"
;;
*/fish)
_profile="$HOME/.config/fish/config.fish"
_shell="fish"
;;
*)
err "could not detect shell. Add '$_bin_dir' to your PATH"
esac
say "detected your shell as $_shell."
# Add only if not already in PATH
# shellcheck disable=SC2035
if test ":$PATH:" != *":$_bin_dir:"*; then
ensure echo "# Added by the Apibara installer" >> "$_profile"
ensure echo "export PATH=\"\$PATH:$_bin_dir\"" >> "$_profile"
fi
say "added the installation to your PATH. Run 'source $_profile' or start a new terminal to use apibara"
say ""
say "Documentation: https://www.apibara.com/docs"
say "GitHub: https://github.com/apibara"
say "Twitter: https://www.twitter.com/apibara_web3"
say "Discord: https://discord.gg/m7B92CNFNt"
}
get_arch() {
local _ostype _cputype _arch
_ostype="$(uname -s)"
_cputype="$(uname -m)"
case "$_ostype" in
Linux)
_ostype=linux
;;
Darwin)
_ostype=macos
;;
*)
err "unrecognized OS type: $_ostype"
;;
esac
case "$_cputype" in
aarch64 | arm64)
_cputype=aarch64
;;
x86_64 | x86-64 | x64 | amd64)
_cputype=x86_64
;;
*)
err "unsupported CPU type: $_cputype"
;;
esac
_arch="${_cputype}-${_ostype}"
RETVAL="$_arch"
}
say() {
printf 'apibara-installer: %s\n' "$1"
need_cmd uname
}
err() {
say "$1" >&2
exit 1
}
need_cmd() {
if ! check_cmd "$1"; then
err "command '$1' is required but not available"
fi
}
check_cmd() {
command -v "$1" > /dev/null 2>&1
}
assert_nz() {
if [ -z "$1" ]; then
err "assert_nz failed: $2"
fi
}
ensure() {
if ! "$@"; then
err "command failed: $*"
fi
}
main "$@" || exit 1