From b6866149e36dbceebcdb0dda322f2884cd2b6fb6 Mon Sep 17 00:00:00 2001 From: j178 <10510431+j178@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:10:48 +0800 Subject: [PATCH 1/2] feat: add installer script for linux/macOS --- 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..304e9954 --- /dev/null +++ b/scripts/install.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +set -exuo 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__ From 462674c9f79fcba2e9801a1ca87bd590ff23b44b Mon Sep 17 00:00:00 2001 From: j178 <10510431+j178@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:12:20 +0800 Subject: [PATCH 2/2] remove -e flag from script --- scripts/install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/install.sh b/scripts/install.sh index 304e9954..67ad84cc 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -set -exuo pipefail +set -euo pipefail # Most of this script is taken from https://github.com/mitsuhiko/rye/blob/main/scripts/install.sh