github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/install.sh (about)

     1  #!/usr/bin/env bash
     2  set -euo pipefail
     3  
     4  # GitHub Org and Repo to get archives from
     5  GITHUB_ORG="micro"
     6  GITHUB_REPO="micro"
     7  
     8  # micro install directory
     9  MICRO_INSTALL_DIR="$HOME/bin"
    10  # micro cli name
    11  MICRO_CLI_NAME="micro"
    12  # micro cli install path
    13  MICRO_CLI_PATH="${MICRO_INSTALL_DIR}/${MICRO_CLI_NAME}"
    14  
    15  # get machine ARCH
    16  ARCH=$(uname -m)
    17  # get machine OS
    18  OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')
    19  # Linux requires sudo for $MICRO_INSTALL_DIR
    20  SUDO="false"
    21  
    22  # Http request CLI
    23  HTTP_CLIENT=curl
    24  
    25  getSystemInfo() {
    26      echo "Getting system information"
    27      case $ARCH in
    28          armv7*)
    29                  ARCH="arm7";;
    30          aarch64)
    31                  ARCH="arm64";;
    32          x86_64)
    33                  ARCH="amd64";;
    34      esac
    35  
    36      # linux requires sudo permissions
    37      if [ "$OS" == "linux" ]; then
    38          SUDO="true"
    39      fi
    40      echo "Your machine is running ${OS} on ${ARCH} CPU architecture"
    41  }
    42  
    43  checkSupported() {
    44      local supported_osarch=(darwin-amd64 linux-amd64 linux-arm7 linux-arm64)
    45      local machine_osarch="${OS}-${ARCH}"
    46  
    47      echo "Checking machine system support"
    48      for osarch in "${supported_osarch[@]}"; do
    49          if [ "$osarch" == "$machine_osarch" ]; then
    50              return
    51          fi
    52      done
    53  
    54      echo "No prebuilt binary for ${machine_osarch}"
    55      exit 1
    56  }
    57  
    58  checkHttpClient() {
    59      echo "Checking HTTP client"
    60      if type "curl" > /dev/null; then
    61          HTTP_CLIENT="curl"
    62      elif type "wget" > /dev/null; then
    63          HTTP_CLIENT="wget"
    64      else
    65          echo "Either curl or wget is required"
    66          exit 1
    67      fi
    68  }
    69  
    70  sudoRun() {
    71      local CMD="$*"
    72  
    73      if [ $EUID -ne 0 -a $SUDO = "true" ]; then
    74          CMD="sudo $CMD"
    75      fi
    76  
    77      $CMD
    78  }
    79  
    80  getLatestRelease() {
    81      local release_url="https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/releases"
    82      local latest_release=""
    83  
    84      echo "Getting the latest micro release"
    85      if [ "$HTTP_CLIENT" == "curl" ]; then
    86          latest_release=$(curl -s $release_url | grep \"tag_name\" | awk 'NR==1{print $2}' |  sed -n 's/\"\(.*\)\",/\1/p')
    87      else
    88          latest_release=$(wget -q --header="Accept: application/json" -O - $release_url | grep \"tag_name\" | awk 'NR==1{print $2}' |  sed -n 's/\"\(.*\)\",/\1/p')
    89      fi
    90      echo "Latest micro release found: ${latest_release}"
    91  
    92      LATEST_RELEASE_TAG=$latest_release
    93      CLI_ARCHIVE="${MICRO_CLI_NAME}-${LATEST_RELEASE_TAG}-${OS}-${ARCH}.tar.gz"
    94      DOWNLOAD_BASE="https://github.com/${GITHUB_ORG}/${GITHUB_REPO}/releases/download"
    95      DOWNLOAD_URL="${DOWNLOAD_BASE}/${LATEST_RELEASE_TAG}/${CLI_ARCHIVE}"
    96  
    97      TMP_ROOT=$(mktemp -dt micro-install-XXXXXX)
    98      TMP_FILE="$TMP_ROOT/$CLI_ARCHIVE"
    99  
   100      echo "Downloading $DOWNLOAD_URL ..."
   101      if [ "$HTTP_CLIENT" == "curl" ]; then
   102          curl -SsL "$DOWNLOAD_URL" -o "$TMP_FILE"
   103      else
   104          wget -q -O "$TMP_FILE" "$DOWNLOAD_URL"
   105      fi
   106  
   107      if [ ! -f "$TMP_FILE" ]; then
   108          echo "Failed to download $DOWNLOAD_URL ..."
   109          exit 1
   110      fi
   111  }
   112  
   113  installFile() {
   114      tar xf "$TMP_FILE" -C "$TMP_ROOT"
   115      local tmp_root_cli="$TMP_ROOT/$MICRO_CLI_NAME"
   116  
   117      if [ ! -f "$tmp_root_cli" ]; then
   118          echo "Failed to unpack micro binary."
   119          exit 1
   120      fi
   121  
   122      # mkdir if not exists
   123      if [ ! -d $MICRO_INSTALL_DIR ]; then
   124  	mkdir -p $MICRO_INSTALL_DIR
   125      fi
   126  
   127      chmod o+x $tmp_root_cli
   128      sudoRun cp "$tmp_root_cli" "$MICRO_INSTALL_DIR"
   129  
   130      if [ -f "$MICRO_CLI_PATH" ]; then
   131          echo "$MICRO_CLI_NAME installed into $MICRO_INSTALL_DIR successfully."
   132  
   133          $MICRO_CLI_PATH --version
   134      else
   135          echo "Failed to install $MICRO_CLI_NAME. Already exists."
   136          exit 1
   137      fi
   138  
   139      if [ "$MICRO_CLI_PATH" != "$(which micro)" ]; then
   140          # From https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
   141          YELLOW='\033[1;33m'
   142          NC='\033[0m' # No Color
   143          printf "${YELLOW}Looks like there is an other micro installation in your path under \"$(which micro)\" already.\nPlease put \"export PATH=$MICRO_INSTALL_DIR:\$PATH\" in your bashrc.${NC}\n"
   144      fi
   145  }
   146  
   147  fail_trap() {
   148      result=$?
   149      if [ "$result" != "0" ]; then
   150          echo "Failed to install micro"
   151      fi
   152      cleanup
   153      exit $result
   154  }
   155  
   156  cleanup() {
   157      if [[ -d "${TMP_ROOT:-}" ]]; then
   158          rm -rf "$TMP_ROOT"
   159      fi
   160  }
   161  
   162  # catch errors and print help
   163  trap "fail_trap" EXIT
   164  
   165  # execute installation
   166  getSystemInfo
   167  checkSupported
   168  checkHttpClient
   169  getLatestRelease
   170  installFile
   171  cleanup