major refactor
This commit is contained in:
69
lib/common.sh
Normal file
69
lib/common.sh
Normal file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env bash
|
||||
# Shared utility functions for bootstrap CLI
|
||||
|
||||
# Avoid double sourcing
|
||||
if [ -n "${_LIB_COMMON_SOURCED:-}" ]; then
|
||||
return 0
|
||||
fi
|
||||
_LIB_COMMON_SOURCED=1
|
||||
|
||||
# Ensure running in Bash
|
||||
require_bash() {
|
||||
if [ -z "${BASH_VERSION:-}" ]; then
|
||||
echo "Error: This script must be run using bash." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Color definitions (only if stdout is a TTY)
|
||||
if [ -t 1 ]; then
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[0;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
else
|
||||
RED=''
|
||||
GREEN=''
|
||||
YELLOW=''
|
||||
BLUE=''
|
||||
NC=''
|
||||
fi
|
||||
|
||||
log_info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
log_warn() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1" >&2
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1" >&2
|
||||
}
|
||||
|
||||
# Yes/No Confirmation prompt
|
||||
confirm() {
|
||||
local prompt="$1"
|
||||
local response
|
||||
|
||||
# Read from /dev/tty to support piped installations
|
||||
read -r -p "$prompt [y/N]: " response </dev/tty || true
|
||||
[[ "$response" =~ ^[Yy]$ ]]
|
||||
}
|
||||
|
||||
# Check if a command is available
|
||||
has_command() {
|
||||
command -v "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# Temporary directory helper with automatic cleanup on exit
|
||||
make_temp_dir() {
|
||||
local tmp_dir
|
||||
tmp_dir="$(mktemp -d)"
|
||||
echo "$tmp_dir"
|
||||
}
|
||||
Reference in New Issue
Block a user