Skip to content

Instantly share code, notes, and snippets.

@therealparmesh
Created October 26, 2023 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therealparmesh/999abce4361c97b7d4aada2c05098884 to your computer and use it in GitHub Desktop.
Save therealparmesh/999abce4361c97b7d4aada2c05098884 to your computer and use it in GitHub Desktop.
Install
#!/bin/bash
set -euo pipefail
# Variables
REPO="user/repo"
ASSET_NAME=""
# Detect OS & set installation path
detect_os() {
case "$(uname -s)" in
Linux) OS="linux"; INSTALL_PATH="$HOME/.local/bin" ;;
Darwin) OS="macos"; INSTALL_PATH="$HOME/.local/bin" ;;
MINGW*|MSYS*) OS="windows"; INSTALL_PATH="/c/Windows" ;; # Git Bash & MSYS
CYGWIN*) OS="windows"; INSTALL_PATH="/cygdrive/c/Windows" ;; # Cygwin
*) echo "Unsupported OS"; exit 1 ;;
esac
ASSET_NAME="asset-$OS"
[[ "$OS" == "windows" ]] && ASSET_NAME="$ASSET_NAME.exe"
}
# Warn if install path not in PATH (Linux & macOS)
path_warning() {
[[ "$OS" == "linux" || "$OS" == "macos" ]] && [[ ! ":$PATH:" == *":$INSTALL_PATH:"* ]] && echo "Warning: Add $INSTALL_PATH to PATH."
}
# Download & move asset to install path
download_install() {
ASSET_URL=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | jq -r ".assets[] | select(.name == \"$ASSET_NAME\").browser_download_url")
[[ ! "$ASSET_URL" ]] && echo "Error fetching asset URL!" && exit 1
curl -L -o "$ASSET_NAME" "$ASSET_URL"
[[ ! -f "$ASSET_NAME" ]] && echo "Download error!" && exit 1
mv "$ASSET_NAME" "$INSTALL_PATH/"
}
# Execute
detect_os
path_warning
download_install
echo "Installation complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment