diff --git a/install.sh b/install.sh index 8ea38a6..8607f19 100755 --- a/install.sh +++ b/install.sh @@ -98,25 +98,30 @@ if [ "${1:-}" = "-update" ]; then info "Stopping service..." sudo_if systemctl stop "$SERVICE_NAME" 2>/dev/null || true - info "Rebuilding binary..." - if command -v go &>/dev/null; then - export GOMODCACHE="${INSTALL_DIR}/.go/mod" - export GOPATH="${INSTALL_DIR}/.go" - export GOCACHE="${INSTALL_DIR}/.go/build" - mkdir -p "${GOMODCACHE}" "${GOCACHE}" 2>/dev/null || sudo_if mkdir -p "${GOMODCACHE}" "${GOCACHE}" - # Ensure build cache and old binary are writable by the app user. - sudo_if chown -R "${APP_USER:-${SUDO_USER:-$(whoami)}}" "${INSTALL_DIR}/.go" "${INSTALL_DIR}/app" 2>/dev/null || true - sudo_if rm -f app - CGO_ENABLED=0 go build -buildvcs=false -ldflags="-s -w" -o app . - else - warn "Go not found — downloading pre-built binary..." - TAG=$(curl -fsSL https://git.lohmar.co.uk/api/v1/repos/cclohmar/ReceiptNext/releases/latest \ - | python3 -c "import json,sys; print(json.load(sys.stdin).get('tag_name','v1.0.0'))" 2>/dev/null || echo "v1.0.0") - ARCH="$(uname -m)"; [ "$ARCH" = "x86_64" ] && ARCH="amd64"; [ "$ARCH" = "aarch64" ] && ARCH="arm64" - sudo_if curl -fsSL -o app "https://git.lohmar.co.uk/cclohmar/ReceiptNext/releases/download/${TAG}/app-linux-${ARCH}" - sudo_if chmod +x app + # Ensure Go is installed for building from source. + if ! command -v go &>/dev/null; then + info "Installing Go..." + if command -v apt-get &>/dev/null; then + sudo_if apt-get update -qq && sudo_if apt-get install -y -qq golang-go 2>&1 | tail -1 + elif command -v dnf &>/dev/null; then + sudo_if dnf install -y golang 2>&1 | tail -1 + elif command -v apk &>/dev/null; then + sudo_if apk add go 2>&1 | tail -1 + else + err "No package manager found. Please install Go 1.23+ manually." + exit 1 + fi fi + info "Rebuilding binary..." + export GOMODCACHE="${INSTALL_DIR}/.go/mod" + export GOPATH="${INSTALL_DIR}/.go" + export GOCACHE="${INSTALL_DIR}/.go/build" + mkdir -p "${GOMODCACHE}" "${GOCACHE}" 2>/dev/null || sudo_if mkdir -p "${GOMODCACHE}" "${GOCACHE}" + sudo_if chown -R "${APP_USER:-${SUDO_USER:-$(whoami)}}" "${INSTALL_DIR}/.go" "${INSTALL_DIR}/app" 2>/dev/null || true + sudo_if rm -f app + CGO_ENABLED=0 go build -buildvcs=false -ldflags="-s -w" -o app . + # Fix ownership of all files (build cache may be root-owned from earlier builds). sudo_if chown -R "${APP_USER:-${SUDO_USER:-$(whoami)}}:${APP_USER:-${SUDO_USER:-$(whoami)}}" "$INSTALL_DIR" 2>/dev/null || true @@ -205,25 +210,29 @@ case "$ARCH" in *) err "Unsupported architecture: $ARCH"; exit 1 ;; esac -if command -v go &>/dev/null; then - info "Building binary from source (pure Go, no CGO)..." - # Use a writable cache — the user's home (/app/) may not be writable - export GOMODCACHE="${INSTALL_DIR}/.go/mod" - export GOPATH="${INSTALL_DIR}/.go" - export GOCACHE="${INSTALL_DIR}/.go/build" - mkdir -p "${GOMODCACHE}" "${GOCACHE}" 2>/dev/null || sudo_if mkdir -p "${GOMODCACHE}" "${GOCACHE}" - # Ensure cache is owned by the app user. - sudo_if chown -R "$(whoami):$(whoami)" "${INSTALL_DIR}/.go" "${INSTALL_DIR}/app" 2>/dev/null || true - sudo_if rm -f app - CGO_ENABLED=0 go build -buildvcs=false -ldflags="-s -w" -o app . -else - info "Go not found — downloading pre-built binary..." - TAG=$(curl -fsSL https://git.lohmar.co.uk/api/v1/repos/cclohmar/ReceiptNext/releases/latest \ - | python3 -c "import json,sys; print(json.load(sys.stdin).get('tag_name','v1.0.0'))" 2>/dev/null || echo "v1.0.0") - URL="https://git.lohmar.co.uk/cclohmar/ReceiptNext/releases/download/${TAG}/app-linux-${ARCH}" - sudo_if curl -fsSL -o app "$URL" - sudo_if chmod +x app +# Ensure Go is installed for building from source. +if ! command -v go &>/dev/null; then + info "Installing Go..." + if command -v apt-get &>/dev/null; then + sudo_if apt-get update -qq && sudo_if apt-get install -y -qq golang-go 2>&1 | tail -1 + elif command -v dnf &>/dev/null; then + sudo_if dnf install -y golang 2>&1 | tail -1 + elif command -v apk &>/dev/null; then + sudo_if apk add go 2>&1 | tail -1 + else + err "No package manager found. Please install Go 1.23+ manually." + exit 1 + fi fi + +info "Building binary from source (pure Go, no CGO)..." +export GOMODCACHE="${INSTALL_DIR}/.go/mod" +export GOPATH="${INSTALL_DIR}/.go" +export GOCACHE="${INSTALL_DIR}/.go/build" +mkdir -p "${GOMODCACHE}" "${GOCACHE}" 2>/dev/null || sudo_if mkdir -p "${GOMODCACHE}" "${GOCACHE}" +sudo_if chown -R "$(whoami):$(whoami)" "${INSTALL_DIR}/.go" "${INSTALL_DIR}/app" 2>/dev/null || true +sudo_if rm -f app +CGO_ENABLED=0 go build -buildvcs=false -ldflags="-s -w" -o app . info "Binary ready: $INSTALL_DIR/app" # ── 3. Create runtime directories ────────────────────────────────