main/lib/log.sh

23 lines
816 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 target non-root user — used for AUR builds, nvm, etc.
# precedence (when running as root): TARGET_USER (set by create-user) > SUDO_USER > USER.
# when not root: run directly as ourselves.
as_user() {
local u="${SUDO_USER:-$USER}"
if (( EUID == 0 )); then
u="${TARGET_USER:-${SUDO_USER:-$USER}}"
command -v runuser >/dev/null 2>&1 && runuser -u "$u" -- "$@" || su "$u" -c "$*"
else
"$@"
fi
}