Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add installer script for linux/macOS #270

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions leetcode/qid.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ func ParseQID(qid string, c Client) ([]*QuestionData, error) {
return nil, err
}
}
if err == ErrQuestionNotFound {
if errors.Is(err, ErrQuestionNotFound) {
err = nil
}
if err != nil {
return nil, fmt.Errorf("invalid qid: %w", err)
}
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 {
Expand Down
89 changes: 89 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -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__