From b07b977b9697756bb1d920293382727d5c987902 Mon Sep 17 00:00:00 2001 From: Jo <10510431+j178@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:13:25 +0800 Subject: [PATCH] feat: add installer script for linux/macOS (#270) * feat: add installer script for linux/macOS * remove -e flag from script --- README.md | 8 ++++- README_zh.md | 8 ++++- leetcode/qid.go | 4 +-- scripts/install.sh | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100755 scripts/install.sh diff --git a/README.md b/README.md index 5d1d6fc2..ff9ee6a7 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,13 @@ scoop bucket add j178 https://github.com/j178/scoop-bucket.git scoop install j178/leetgo ``` -### Install via go +### Install via installer script on macOS/Linux + +```shell +curl -fsSL https://raw.githubusercontent.com/j178/leetgo/master/scripts/install.sh | bash +``` + +### Install from source via `go install` ```shell go install github.com/j178/leetgo@latest diff --git a/README_zh.md b/README_zh.md index d8b11b78..94132828 100644 --- a/README_zh.md +++ b/README_zh.md @@ -120,7 +120,13 @@ scoop bucket add j178 https://github.com/j178/scoop-bucket.git scoop install j178/leetgo ``` -### 使用 `go install` +### macOS/Linux 使用脚本安装 + +```shell +curl -fsSL https://raw.githubusercontent.com/j178/leetgo/master/scripts/install.sh | bash +``` + +### 使用 `go install` 从源码安装 ```shell go install github.com/j178/leetgo@latest diff --git a/leetcode/qid.go b/leetcode/qid.go index 73d63a4c..e962e456 100644 --- a/leetcode/qid.go +++ b/leetcode/qid.go @@ -73,7 +73,7 @@ func ParseQID(qid string, c Client) ([]*QuestionData, error) { return nil, err } } - if err == ErrQuestionNotFound { + if errors.Is(err, ErrQuestionNotFound) { err = nil } if err != nil { @@ -81,7 +81,7 @@ func ParseQID(qid string, c Client) ([]*QuestionData, error) { } if q == nil && len(qs) == 0 { q, err = QuestionBySlug(qid, c) - if err == ErrQuestionNotFound { + if errors.Is(err, ErrQuestionNotFound) { q, err = QuestionFromCacheByID(qid, c) } if err != nil { diff --git a/scripts/install.sh b/scripts/install.sh new file mode 100755 index 00000000..67ad84cc --- /dev/null +++ b/scripts/install.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Most of this script is taken from https://github.com/mitsuhiko/rye/blob/main/scripts/install.sh + +# Wrap everything in a function so that a truncated script +# does not have the chance to cause issues. +__wrap__() { + +# allow overriding the version +VERSION=${LEETGO_VERSION:-latest} +PREFIX=${LEETGO_PREFIX:-${HOME}/.local} + +REPO=j178/leetgo +PLATFORM=`uname -s` +ARCH=`uname -m` + +if [[ $PLATFORM == "Darwin" ]]; then + PLATFORM="macOS" +elif [[ $PLATFORM == "Linux" ]]; then + PLATFORM="linux" +fi + +if [[ $ARCH == armv8* ]] || [[ $ARCH == arm64* ]] || [[ $ARCH == aarch64* ]]; then + ARCH="arm64" +elif [[ $ARCH == i686* ]]; then + ARCH="x86_64" +fi + +BINARY="leetgo_${PLATFORM}_${ARCH}" + +# Oddly enough GitHub has different URLs for latest vs specific version +if [[ $VERSION == "latest" ]]; then + DOWNLOAD_URL=https://github.com/${REPO}/releases/latest/download/${BINARY}.tar.gz +else + DOWNLOAD_URL=https://github.com/${REPO}/releases/download/${VERSION}/${BINARY}.tar.gz +fi + +echo "This script will automatically download and install leetgo (${VERSION}) for you." +if [ "x$(id -u)" == "x0" ]; then + echo "warning: this script is running as root. This is dangerous and unnecessary!" +fi + +if ! hash curl 2> /dev/null; then + echo "error: you do not have 'curl' installed which is required for this script." + exit 1 +fi + +if ! hash tar 2> /dev/null; then + echo "error: you do not have 'tar' installed which is required for this script." + exit 1 +fi + +TEMP_INSTALL_DIR=`mktemp "${TMPDIR:-/tmp/}.leetgoinstall.XXXXXXXX"` +TEMP_FILE_GZ="${TEMP_INSTALL_DIR}.tar.gz" + +rm -rf "$TEMP_INSTALL_DIR" +mkdir -p "$TEMP_INSTALL_DIR" + +cleanup() { + rm -rf "$TEMP_INSTALL_DIR" + rm -f "$TEMP_FILE_GZ" +} + +trap cleanup EXIT +echo "Downloading $DOWNLOAD_URL" +HTTP_CODE=$(curl -SL --progress-bar "$DOWNLOAD_URL" --output "$TEMP_FILE_GZ" --write-out "%{http_code}") +if [[ ${HTTP_CODE} -lt 200 || ${HTTP_CODE} -gt 299 ]]; then + echo "error: platform ${PLATFORM} (${ARCH}) is unsupported." + exit 1 +fi + +tar -xzf "$TEMP_FILE_GZ" -C "$TEMP_INSTALL_DIR" + +DEST="$PREFIX/bin/leetgo" + +# Continue? +echo "leetgo will be installed into '$DEST'." +echo -n "Continue? (y/n) " +read -r yn +case $yn in + [Yy]* ) ;; + * ) exit;; +esac + +chmod +x "$TEMP_INSTALL_DIR/leetgo" +mv "$TEMP_INSTALL_DIR/leetgo" "$DEST" + +}; __wrap__