main/lib/log.sh

22 lines
734 B
Bash
Executable File

#!/usr/bin/env bash
# log.sh — tiny color log helpers.
log() { printf '\033[1;34m>>\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m!!\033[0m %s\n' "$*" >&2; }
err() { printf '\033[1;31mxx\033[0m %s\n' "$*" >&2; }
ok() { printf '\033[1;32mok\033[0m %s\n' "$*"; }
# run a command as root when not already root
sudo_() { if (( EUID == 0 )); then "$@"; else sudo "$@"; fi; }
# run a command AS the invoking (non-root) user — used for AUR builds, etc.
# when root: drop to SUDO_USER (or USER) via runuser; when not root: run directly.
as_user() {
local u="$USER"
if (( EUID == 0 )); then
u="${SUDO_USER:-$USER}"
command -v runuser >/dev/null 2>&1 && runuser -u "$u" -- "$@" || su "$u" -c "$*"
else
"$@"
fi
}