scripts/logging.sh
2025-04-14 09:22:15 +02:00

76 lines
No EOL
1.7 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# POSIX-compliant color codes
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Simple echo functions without any arrays or typeset
echo_error() {
printf "${RED}❌ %s${NC}\n" "$1" >&2
}
echo_warning() {
printf "${YELLOW}⚠️ %s${NC}\n" "$1"
}
echo_info() {
printf "${GREEN} %s${NC}\n" "$1"
}
echo_note() {
printf "${CYAN}📝 %s${NC}\n" "$1"
}
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
for i in which command; do
if command_exists $i; then
case $i in
command) command -v "$@" >/dev/null 2>&1 ;;
which) which "$@" >/dev/null 2>&1 ;;
esac
fi
done
}
_cd() {
cd "$1" || { echo_error "Cannot navigate to the directory: $1"; return 1; }
}
_exit() {
echo_error "There was an error, which caused the script to exit immediately"
echo_error "$1"
echo_error "Exiting now!"
exit 1
}
confirm_action() {
read -p "$1 - [y/n]: " confirm
case $confirm in
[yY] ) return 0 ;;
[nN] ) return 1 ;;
* ) echo_warning "Invalid input. Action cancelled." ; return 1 ;;
esac
}
create_dir() {
if [ ! -d "$1" ]; then
echo_info "Creating directory: $1"
mkdir -p "$1" || _exit "Failed to create directory $1"
else
echo_info "Directory $1 already exists"
fi
}
check_disk_space() {
threshold=1000000 # Minimum space in KB (e.g., 1GB)
available_space=$(df / | awk 'NR==2 {print $4}')
if [ "$available_space" -lt "$threshold" ]; then
echo_warning "Not enough disk space. Only $((available_space / 1024))MB remaining."
return 1
fi
echo_info "Sufficient disk space available."
}