From fa2b2b2199652acfcd052307a4ef904e70316eac Mon Sep 17 00:00:00 2001 From: zyy17 Date: Thu, 8 Aug 2024 11:59:01 +0800 Subject: [PATCH] refactor: download the latest stable released version by default and do some small refactoring --- scripts/install.sh | 81 ++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/scripts/install.sh b/scripts/install.sh index 6c2db89709f2..1c0640d538d4 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,62 +1,68 @@ -#!/bin/sh +#!/usr/bin/env bash set -ue OS_TYPE= ARCH_TYPE= +# Set the GitHub token to avoid GitHub API rate limit. +GITHUB_TOKEN=${GITHUB_TOKEN:-} VERSION=${1:-latest} GITHUB_ORG=GreptimeTeam GITHUB_REPO=greptimedb BIN=greptime -get_os_type() { - os_type="$(uname -s)" +function get_os_type() { + os_type="$(uname -s)" - case "$os_type" in + case "$os_type" in Darwin) - OS_TYPE=darwin - ;; + OS_TYPE=darwin + ;; Linux) - OS_TYPE=linux - ;; + OS_TYPE=linux + ;; *) - echo "Error: Unknown OS type: $os_type" - exit 1 - esac + echo "Error: Unknown OS type: $os_type" + exit 1 + esac } -get_arch_type() { - arch_type="$(uname -m)" +function get_arch_type() { + arch_type="$(uname -m)" - case "$arch_type" in + case "$arch_type" in arm64) - ARCH_TYPE=arm64 - ;; + ARCH_TYPE=arm64 + ;; aarch64) - ARCH_TYPE=arm64 - ;; + ARCH_TYPE=arm64 + ;; x86_64) - ARCH_TYPE=amd64 - ;; + ARCH_TYPE=amd64 + ;; amd64) - ARCH_TYPE=amd64 - ;; + ARCH_TYPE=amd64 + ;; *) - echo "Error: Unknown CPU type: $arch_type" - exit 1 - esac + echo "Error: Unknown CPU type: $arch_type" + exit 1 + esac } -get_os_type -get_arch_type - -if [ -n "${OS_TYPE}" ] && [ -n "${ARCH_TYPE}" ]; then - # Use the latest nightly version. +function download_artifact() { + if [ -n "${OS_TYPE}" ] && [ -n "${ARCH_TYPE}" ]; then + # Use the latest stable released version. + # GitHub API reference: https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#get-the-latest-release. if [ "${VERSION}" = "latest" ]; then - VERSION=$(curl -s -XGET "https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/releases" | grep tag_name | grep nightly | cut -d: -f 2 | sed 's/.*"\(.*\)".*/\1/' | uniq | sort -rV | head -n 1) - if [ -z "${VERSION}" ]; then - echo "Failed to get the latest version." - exit 1 + # To avoid other tools dependency, we choose to use `curl` to get the version metadata and parsed by `sed`. + VERSION=$(curl -sL \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + ${GITHUB_TOKEN:+-H "Authorization: Bearer $GITHUB_TOKEN"} \ + "https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/releases/latest" | sed -n 's/.*"tag_name": "\([^"]*\)".*/\1/p') + if [ -z "${VERSION}" ]; then + echo "Failed to get the latest stable released version." + exit 1 fi fi @@ -73,4 +79,9 @@ if [ -n "${OS_TYPE}" ] && [ -n "${ARCH_TYPE}" ]; then rm -r "${PACKAGE_NAME%.tar.gz}" && \ echo "Run './${BIN} --help' to get started" fi -fi + fi +} + +get_os_type +get_arch_type +download_artifact