#!/usr/bin/env bash # shellcheck disable=SC2086 # GitHub Releases에서 최신 ttyd 안정 버전을 다운로드하고 설치합니다. # ttyd: https://github.com/tsl0922/ttyd # # 지원 플랫폼: # macOS : Intel (x86_64), Apple Silicon (arm64) — Homebrew를 통해 설치 # Linux : x86_64, aarch64(arm64), armhf(32bit), i686 # FreeBSD : 지원 안 함 (필요 시 소스 빌드) # Windows : Git Bash / MSYS2 / Cygwin (윈도우 네이티브 바이너리 설치) set -euo pipefail # ──────────────────────────────────────────── # 상수 정의 # ──────────────────────────────────────────── REPO="tsl0922/ttyd" API_URL="https://api.github.com/repos/${REPO}/releases/latest" INSTALL_DIR="${HOME}/.local/bin" # ──────────────────────────────────────────── # 유틸리티 함수 (모두 stderr 출력) # ──────────────────────────────────────────── info() { echo "[INFO] $*" >&2; } warn() { echo "[WARN] $*" >&2; } error() { echo "[ERROR] $*" >&2; exit 1; } success() { echo "[OK] $*" >&2; } # ──────────────────────────────────────────── # macOS용 Homebrew 확인 및 설치 # ──────────────────────────────────────────── ensure_brew() { if command -v brew &>/dev/null; then return 0 fi info "Homebrew가 설치되어 있지 않습니다. Homebrew 설치를 진행합니다..." /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" if [ -d /opt/homebrew/bin ]; then export PATH="/opt/homebrew/bin:$PATH" fi if [ -d /usr/local/bin ]; then export PATH="/usr/local/bin:$PATH" fi command -v brew &>/dev/null || error "Homebrew 설치에 실패했습니다. 수동으로 설치한 후 다시 실행해 주세요: https://brew.sh/" success "Homebrew 설치 완료" } # ──────────────────────────────────────────── # 필수 명령어 확인 # ──────────────────────────────────────────── for cmd in curl uname; do command -v "$cmd" &>/dev/null || error "'$cmd' 명령어가 필요합니다." done # Fallback: jq가 없을 경우 python3를 사용해 JSON 정보 분석 if command -v jq &>/dev/null; then _json_latest_tag() { jq -r '.tag_name' } else command -v python3 &>/dev/null || error "'jq' 또는 'python3' 명령어가 설치되어 있어야 합니다." _json_latest_tag() { python3 -c " import sys, json data = json.load(sys.stdin) print(data.get('tag_name', '')) " } fi # ──────────────────────────────────────────── # OS 및 아키텍처 분석 # ──────────────────────────────────────────── # 반환 형식: "바이너리명:설치방식" # 설치방식 = brew | binary detect_platform() { local os arch os="$(uname -s)" arch="$(uname -m)" case "$os" in # ── macOS ────────────────────────────────────────────── Darwin) # macOS는 brew 설치 방식을 권장 echo "brew:brew" ;; # ── Linux ────────────────────────────────────────────── Linux) case "$arch" in x86_64|amd64) echo "ttyd.x86_64:binary" ;; aarch64|arm64) echo "ttyd.aarch64:binary" ;; armv7l|armv6l) echo "ttyd.armhf:binary" ;; i686|i386) echo "ttyd.i686:binary" ;; *) error "지원하지 않는 Linux 아키텍처입니다: $arch" ;; esac ;; # ── Windows (Git Bash / MSYS2 / Cygwin) ─────────────── MINGW*|MSYS*|CYGWIN*) warn "Windows 터미널 환경(Git Bash/MSYS2/Cygwin)이 감지되었습니다." echo "ttyd.win32.exe:binary" ;; *) error "지원하지 않는 운영체제입니다: $os (uname -a: $(uname -a))" ;; esac } # ──────────────────────────────────────────── # 최신 릴리스 태그 가져오기 # ──────────────────────────────────────────── get_latest_release_tag() { info "GitHub API를 통해 ttyd 최신 릴리스 버전을 가져오는 중..." local release_json tag release_json="$(curl -fsSL "${API_URL}")" || error "GitHub API 호출에 실패했습니다." tag="$(echo "$release_json" | _json_latest_tag)" if [[ -z "$tag" || "$tag" == "null" ]]; then error "최신 릴리스 태그를 확인하는 데 실패했습니다." fi echo "$tag" } # ──────────────────────────────────────────── # 메인 설치 로직 # ──────────────────────────────────────────── main() { local platform_info asset_name install_method platform_info="$(detect_platform)" asset_name="${platform_info%%:*}" install_method="${platform_info##*:}" info "설치 환경 감지 완료: $asset_name ($install_method 방식)" # 1) macOS (Homebrew 설치 방식) if [[ "$install_method" == "brew" ]]; then info "macOS 환경에서 brew를 통해 ttyd 설치를 진행합니다." if command -v ttyd &>/dev/null; then success "ttyd가 이미 설치되어 있습니다: $(command -v ttyd)" exit 0 fi ensure_brew brew install ttyd success "Homebrew를 통해 ttyd 설치를 완료했습니다." exit 0 fi # 2) Linux / Windows Bash (바이너리 직접 설치 방식) local tag download_url tag="$(get_latest_release_tag)" info "최신 버전 감지: $tag" download_url="https://github.com/tsl0922/ttyd/releases/download/${tag}/${asset_name}" # 설치 디렉토리 생성 mkdir -p "$INSTALL_DIR" local dest_file="${INSTALL_DIR}/ttyd" if [[ "$asset_name" == *.exe ]]; then dest_file="${INSTALL_DIR}/ttyd.exe" fi local tmp_dir tmp_dir="$(mktemp -d)" local tmp_file="${tmp_dir}/${asset_name}" info "바이너리 다운로드 중: $download_url" if ! curl -fsSL "$download_url" -o "$tmp_file"; then rm -rf "$tmp_dir" error "바이너리 다운로드에 실패했습니다. URL: $download_url" fi info "설치 경로로 복사 중: $dest_file" mv "$tmp_file" "$dest_file" chmod +x "$dest_file" rm -rf "$tmp_dir" success "ttyd ($tag) 설치가 성공적으로 완료되었습니다!" info "설치 경로: $dest_file" # PATH 확인 및 안내 if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then warn "설치 경로($INSTALL_DIR)가 현재 PATH 환경 변수에 등록되어 있지 않습니다." warn "사용하시는 쉘 프로필 파일(예: ~/.bashrc 또는 ~/.zshrc)에 아래 설정을 추가해 주세요:" warn " export PATH=\"\$HOME/.local/bin:\$PATH\"" fi } main "$@"