Files
miaur/lib/common.sh
T
Cursor AgentandFrank Vitrano a4f4767c47 Add self-update command to refresh miaur from Git
Introduce `miaur self-update` / `--self-update` that clones RepoUrl@Branch,
detects user vs system install prefix, and reinstalls in place. Configurable
via conf/env; supports --noconfirm, --repo-url, and --branch.

Co-authored-by: Frank Vitrano <WHC2006@users.noreply.github.com>
2026-07-23 14:44:24 +00:00

131 lines
2.9 KiB
Bash

#!/usr/bin/env bash
# shellcheck disable=SC2034
# Common utilities for miaur
MIAUR_VERSION="0.2.0"
# Colors (respect pacman Color / NO_COLOR / non-tty)
_miaur_color_enabled() {
[[ -n ${NO_COLOR:-} ]] && return 1
[[ ! -t 1 ]] && return 1
if [[ -f /etc/pacman.conf ]] && grep -Eq '^[[:space:]]*Color([[:space:]]|$)' /etc/pacman.conf 2>/dev/null; then
return 0
fi
# Default: enable on tty even if pacman Color is commented
return 0
}
if _miaur_color_enabled; then
C_RED=$'\033[31m'
C_GREEN=$'\033[32m'
C_YELLOW=$'\033[33m'
C_BLUE=$'\033[34m'
C_CYAN=$'\033[36m'
C_BOLD=$'\033[1m'
C_DIM=$'\033[2m'
C_RESET=$'\033[0m'
else
C_RED= C_GREEN= C_YELLOW= C_BLUE= C_CYAN= C_BOLD= C_DIM= C_RESET=
fi
msg() { printf '%s::%s %s\n' "${C_BOLD}${C_BLUE}" "${C_RESET}" "$*" >&2; }
msg2() { printf '%s ->%s %s\n' "${C_BOLD}${C_BLUE}" "${C_RESET}" "$*" >&2; }
warn() { printf '%s==>%s WARNING: %s\n' "${C_BOLD}${C_YELLOW}" "${C_RESET}" "$*" >&2; }
error() { printf '%s==>%s ERROR: %s\n' "${C_BOLD}${C_RED}" "${C_RESET}" "$*" >&2; }
die() { error "$*"; exit 1; }
# Append --noconfirm when requested (default: ask, like pacman/paru).
pac_noconfirm_args() {
if [[ ${MIAUR_NOCONFIRM:-0} -eq 1 ]]; then
printf '%s\n' --noconfirm
fi
}
# Prompt Y/n; returns 0 to continue. Skipped when --noconfirm.
confirm_ask() {
local prompt=${1:-Continue?}
if [[ ${MIAUR_NOCONFIRM:-0} -eq 1 ]]; then
return 0
fi
printf '%s::%s %s [Y/n] ' "${C_BOLD}${C_BLUE}" "${C_RESET}" "$prompt" >&2
local ans=
read -r ans || return 1
case "${ans:-y}" in
y | Y | yes | YES | '') return 0 ;;
*) return 1 ;;
esac
}
need_cmd() {
local c
for c in "$@"; do
command -v "$c" >/dev/null 2>&1 || die "required command not found: $c"
done
}
# Prefer system curl — some user PATH wrappers break AUR HTTPS
resolve_curl() {
if [[ -n ${MIAUR_CURL:-} ]]; then
printf '%s' "$MIAUR_CURL"
elif [[ -x /usr/bin/curl ]]; then
printf '%s' /usr/bin/curl
else
command -v curl
fi
}
# Strip version constraints from a dep string: foo>=1.0 -> foo
dep_name() {
local d=$1
d=${d%%[><=]*}
d=${d%%:*}
printf '%s' "$d"
}
vercmp_safe() {
# pacman --print returns 0 if equal, 1 if first >, -1 if second >
/usr/bin/vercmp "$1" "$2"
}
is_pkg_installed() {
pacman -Q "$1" &>/dev/null
}
pkg_local_version() {
pacman -Q "$1" 2>/dev/null | awk '{print $2}'
}
# Run command as root via sudo
as_root() {
if [[ ${EUID:-$(id -u)} -eq 0 ]]; then
"$@"
else
${MIAUR_SUDO:-sudo} ${MIAUR_SUDOFLAGS:-} "$@"
fi
}
# Keep sudo credentials alive in background
_sudo_loop_pid=
sudoloop_start() {
[[ ${MIAUR_SUDOLOOP:-0} -eq 1 ]] || return 0
[[ ${EUID:-$(id -u)} -eq 0 ]] && return 0
${MIAUR_SUDO:-sudo} -v || return 1
(
while true; do
sleep 60
${MIAUR_SUDO:-sudo} -v || exit
done
) &
_sudo_loop_pid=$!
}
sudoloop_stop() {
if [[ -n ${_sudo_loop_pid:-} ]]; then
kill "$_sudo_loop_pid" 2>/dev/null || true
wait "$_sudo_loop_pid" 2>/dev/null || true
_sudo_loop_pid=
fi
}
trap sudoloop_stop EXIT