removed sudo from install/unistall script

This commit is contained in:
Uber Veng
2026-05-22 02:03:40 +07:00
parent bb0eb3189b
commit 4d0438a2cb
3 changed files with 72 additions and 80 deletions

View File

@@ -4,7 +4,7 @@ set -e
# ── Configuration ────────────────────────────────────────────────────────────
BINARY="ostiary"
REPO_API="${OSTIARY_API:-https://git.vainend.com/api/v1/repos/admin/ostiary}"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
INSTALL_DIR="$HOME/.local/bin"
# ── Colors ───────────────────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
@@ -47,7 +47,6 @@ else
fi
# ── Fetch latest release tag ─────────────────────────────────────────────────
# Uses /releases/latest which returns the newest non-prerelease, non-draft release.
info "Fetching latest release info..."
LATEST_JSON=$(fetch "${REPO_API}/releases/latest") \
@@ -63,52 +62,55 @@ VERSION=$(printf '%s' "$LATEST_JSON" \
ok "Latest version: ${VERSION}"
# ── Build download URL ───────────────────────────────────────────────────────
# Strip /api/v1/repos from the API base to get the Gitea web URL:
# https://git.vainend.com/api/v1/repos/admin/ostiary
# → https://git.vainend.com/admin/ostiary
BASE_URL=$(printf '%s' "$REPO_API" | sed 's|/api/v1/repos||')
DOWNLOAD_URL="${BASE_URL}/releases/download/${VERSION}/${ASSET}"
info "Downloading ${DOWNLOAD_URL}..."
# Quick existence check before downloading the full binary
CODE=$(http_code "$DOWNLOAD_URL")
[ "$CODE" = "200" ] || die "Asset not found (HTTP ${CODE}): ${ASSET}\nCheck that the release contains a file named '${ASSET}'"
[ "$CODE" = "200" ] || die "Asset not found (HTTP ${CODE}): ${ASSET}"
# ── Download to temp file ────────────────────────────────────────────────────
# ── Download ─────────────────────────────────────────────────────────────────
TMP=$(mktemp)
trap 'rm -f "$TMP"' EXIT
fetch_file "$DOWNLOAD_URL" "$TMP" || die "Download failed"
chmod +x "$TMP"
# ── Install ──────────────────────────────────────────────────────────────────
do_install() {
local dest="$1/$BINARY"
if [ -w "$1" ]; then
mv "$TMP" "$dest"
ok "Installed → $dest"
return 0
elif command -v sudo &>/dev/null; then
warn "Writing to $1 requires sudo..."
sudo mv "$TMP" "$dest"
ok "Installed → $dest"
return 0
fi
return 1
}
# ── Install to ~/.local/bin ───────────────────────────────────────────────────
mkdir -p "$INSTALL_DIR"
mv "$TMP" "$INSTALL_DIR/$BINARY"
ok "Installed → $INSTALL_DIR/$BINARY"
if ! do_install "$INSTALL_DIR"; then
FALLBACK="$HOME/.local/bin"
warn "${INSTALL_DIR} is not writable and sudo is unavailable"
warn "Installing to ${FALLBACK} instead"
mkdir -p "$FALLBACK"
mv "$TMP" "$FALLBACK/$BINARY"
ok "Installed → $FALLBACK/$BINARY"
if [[ ":$PATH:" != *":$FALLBACK:"* ]]; then
warn "Add to your shell profile:"
warn " export PATH=\"\$HOME/.local/bin:\$PATH\""
# ── Add ~/.local/bin to PATH if missing ──────────────────────────────────────
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
warn "$INSTALL_DIR is not in PATH — adding it now..."
EXPORT_LINE="export PATH=\"\$HOME/.local/bin:\$PATH\""
# Determine which profile file to update
if [ -f "$HOME/.bashrc" ]; then
PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
PROFILE="$HOME/.bash_profile"
elif [ -f "$HOME/.profile" ]; then
PROFILE="$HOME/.profile"
else
# Create .bashrc if nothing exists
PROFILE="$HOME/.bashrc"
fi
# Avoid duplicate entries
if ! grep -qF "$INSTALL_DIR" "$PROFILE" 2>/dev/null; then
printf '\n# Added by ostiary installer\n%s\n' "$EXPORT_LINE" >> "$PROFILE"
ok "Added PATH entry to $PROFILE"
fi
warn "Run the following to use ostiary in this session:"
warn " export PATH=\"\$HOME/.local/bin:\$PATH\""
warn "New shells will pick it up automatically."
else
ok "$INSTALL_DIR is already in PATH"
fi
echo ""