Skip to content

Instantly share code, notes, and snippets.

@kamontat
Created March 24, 2025 06:07
Show Gist options
  • Save kamontat/1c8ab14d96b90ce3dffaa4e82986f724 to your computer and use it in GitHub Desktop.
Save kamontat/1c8ab14d96b90ce3dffaa4e82986f724 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
REPOSITORY="${R:?}"
DIRECTORY="${D:?}"
APP_NAME="${A:?}"
APP_VERSION="${V:-latest}"
APP_PATH="$DIRECTORY/bin/$APP_NAME"
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
echo ">> กำลังตั้งค่า... เครื่อง"
setup() {
if ! command -v brew >/dev/null; then
echo " กำลังติดตั้ง... Homebrew"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
if ! command -v gh >/dev/null; then
echo " กำลังติดตั้ง... GitHub CLI"
brew install gh
fi
if ! gh auth status --active | grep -q 'Logged in to'; then
echo " กำลังตั้งค่า GitHub Account"
gh auth login -h github.com --skip-ssh-key --web --git-protocol https
fi
if [[ $APP_VERSION == "" ]] || [[ $APP_VERSION == "latest" ]]; then
## find latest version from github
printf " กำลังหาแอพเวอร์ชั่นล่าสุด: "
APP_VERSION="$(gh release list --limit 1 --repo "$REPOSITORY" --json tagName --template '{{(index . 0).tagName}}')"
echo "$APP_VERSION"
fi
if ! test -d "$DIRECTORY"; then
echo " กำลังสร้างโฟลเดอร์ใหม่ $DIRECTORY"
mkdir -p "$DIRECTORY"
fi
if ! test -d "$DIRECTORY/bin"; then
mkdir -p "$DIRECTORY/bin"
fi
}
setup
echo ">> กำลังติดตั้ง... ระบบ $OS ($ARCH)"
install() {
local version="$DIRECTORY/.version"
if ! test -f "$version"; then
printf '' >"$version"
fi
if ! grep -q "$APP_VERSION" "$version"; then
echo " กำลังดาวน์โหลด... แอพเวอร์ชั่น $APP_VERSION"
gh release download "$APP_VERSION" \
--pattern "dcbot_*_${OS}_${ARCH}" \
--output "$APP_PATH" \
--clobber
chmod +x "$APP_PATH"
printf '%s' "$APP_VERSION" >"$version"
fi
}
install
validate() {
printf ">> กำลังตรวจสอบการติดตั้ง: "
if "$APP_PATH" --version | grep -q 'dcbot: '; then
echo "ถูกต้อง"
else
echo "มีปัญหา"
fi
}
validate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment