/ install / install.sh
install.sh
  1  #!/usr/bin/env bash
  2  # shellcheck shell=dash
  3  
  4  set -euo pipefail
  5  
  6  
  7  DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}"
  8  APIBARA_ROOT_DIR="${APIBARA_ROOT_DIR:-$DATA_DIR/apibara}"
  9  APIBARA_REPO="${APIBARA_REPO:-apibara/dna}"
 10  
 11  main() {
 12      say "installing Apibara CLI to ${APIBARA_ROOT_DIR}"
 13      need_cmd curl
 14      need_cmd jq
 15      need_cmd gzip
 16  
 17      get_arch || exit 1
 18  
 19      local _arch="$RETVAL"
 20      assert_nz "$_arch" "arch"
 21  
 22      local _release_tag
 23      _release_tag=$(
 24          curl -s "https://api.github.com/repos/${APIBARA_REPO}/releases" \
 25          | jq -r '.[] | select((.prerelease==false) and (.tag_name | startswith("cli"))) | .tag_name' \
 26          | head -n 1
 27      )
 28      assert_nz "$_release_tag" "release tag"
 29  
 30      local _release_version="${_release_tag#cli/v}"
 31      assert_nz "$_release_version" "release version"
 32      say "installing CLI version $_release_version for $_arch"
 33  
 34      local _release_url="https://github.com/apibara/dna/releases/download/$_release_tag/cli-$_arch.gz"
 35      local _bin_dir="${APIBARA_ROOT_DIR}/bin"
 36      mkdir -p "$_bin_dir"
 37      ensure curl -Ls "$_release_url" > "$_bin_dir/apibara.gz"
 38      ensure gzip -f -d "$_bin_dir/apibara.gz"
 39      ensure chmod +x "$_bin_dir/apibara"
 40  
 41      say "checking installation"
 42      ensure "$_bin_dir/apibara" --version
 43  
 44      say "adding installation to PATH"
 45      local _profile _shell
 46      case "$SHELL" in
 47          */bash)
 48              _profile="$HOME/.bashrc"
 49              _shell="bash"
 50              ;;
 51          */zsh)
 52              _profile="${ZDOTDIR:-$HOME}/.zshenv"
 53              _shell="zsh"
 54              ;;
 55  
 56          */fish)
 57              _profile="$HOME/.config/fish/config.fish"
 58              _shell="fish"
 59              ;;
 60          *)
 61              err "could not detect shell. Add '$_bin_dir' to your PATH"
 62      esac
 63  
 64      say "detected your shell as $_shell."
 65  
 66      # Add only if not already in PATH
 67      # shellcheck disable=SC2035
 68      if test ":$PATH:" != *":$_bin_dir:"*; then
 69          ensure echo "# Added by the Apibara installer" >> "$_profile"
 70          ensure echo "export PATH=\"\$PATH:$_bin_dir\"" >> "$_profile"
 71      fi
 72  
 73      say "added the installation to your PATH. Run 'source $_profile' or start a new terminal to use apibara"
 74      say ""
 75      say "Documentation: https://www.apibara.com/docs"
 76      say "GitHub: https://github.com/apibara"
 77      say "Twitter: https://www.twitter.com/apibara_web3"
 78      say "Discord: https://discord.gg/m7B92CNFNt"
 79  
 80  }
 81  
 82  get_arch() {
 83      local _ostype _cputype _arch
 84      _ostype="$(uname -s)"
 85      _cputype="$(uname -m)"
 86  
 87      case "$_ostype" in
 88          Linux)
 89              _ostype=linux
 90              ;;
 91          Darwin)
 92              _ostype=macos
 93              ;;
 94          *)
 95              err "unrecognized OS type: $_ostype"
 96              ;;
 97      esac
 98  
 99      case "$_cputype" in
100          aarch64 | arm64)
101              _cputype=aarch64
102              ;;
103          x86_64 | x86-64 | x64 | amd64)
104              _cputype=x86_64
105              ;;
106          *)
107              err "unsupported CPU type: $_cputype"
108              ;;
109      esac
110  
111      _arch="${_cputype}-${_ostype}"
112  
113      RETVAL="$_arch"
114  }
115  
116  say() {
117      printf 'apibara-installer: %s\n' "$1"
118      need_cmd uname
119  }
120  
121  err() {
122      say "$1" >&2
123      exit 1
124  }
125  
126  need_cmd() {
127      if ! check_cmd "$1"; then
128          err "command '$1' is required but not available"
129      fi
130  }
131  
132  check_cmd() {
133      command -v "$1" > /dev/null 2>&1
134  }
135  
136  assert_nz() {
137      if [ -z "$1" ]; then
138          err "assert_nz failed: $2"
139      fi
140  }
141  
142  ensure() {
143      if ! "$@"; then
144          err "command failed: $*"
145      fi
146  }
147  
148  main "$@" || exit 1