major refactor

This commit is contained in:
2026-06-19 23:42:18 +05:30
parent a786af12c9
commit e9fbd08423
12 changed files with 621 additions and 356 deletions

69
lib/common.sh Normal file
View 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"
}

90
lib/platform.sh Normal file
View File

@@ -0,0 +1,90 @@
#!/usr/bin/env bash
# Platform and package manager detection for bootstrap CLI
if [ -n "${_LIB_PLATFORM_SOURCED:-}" ]; then
return 0
fi
_LIB_PLATFORM_SOURCED=1
# Source common utilities if not already loaded
if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then
# Assumes common.sh is in the same directory as platform.sh
# We resolve the directory of the current script
_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$_LIB_DIR/common.sh"
fi
detect_distro() {
if has_command pacman; then
echo "arch"
elif has_command apt; then
echo "debian"
elif has_command dnf; then
echo "fedora"
else
echo "unknown"
fi
}
detect_arch() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64) echo "x86_64" ;;
aarch64|arm64) echo "arm64" ;;
*) echo "$arch" ;;
esac
}
# Install packages depending on detected distro
# Usage: pkg_install <package_name_arch> <package_name_debian> <package_name_fedora>
# Or simpler: map common packages to their distro equivalents
pkg_install() {
local distro
distro=$(detect_distro)
if [ "$distro" = "unknown" ]; then
log_error "Unsupported distribution. Cannot install packages automatically."
return 1
fi
local pkgs=()
for arg in "$@"; do
# Format can be "pkg" or "arch:pkg_a|debian:pkg_d|fedora:pkg_f"
if [[ "$arg" =~ : ]]; then
IFS='|' read -ra PARTS <<< "$arg"
local mapped=""
for part in "${PARTS[@]}"; do
local d_prefix="${part%%:*}"
local d_pkg="${part#*:}"
if [ "$d_prefix" = "$distro" ]; then
mapped="$d_pkg"
break
fi
done
if [ -n "$mapped" ]; then
pkgs+=("$mapped")
fi
else
pkgs+=("$arg")
fi
done
if [ ${#pkgs[@]} -eq 0 ]; then
return 0
fi
log_info "Installing packages via $distro package manager: ${pkgs[*]}"
case "$distro" in
arch)
sudo pacman -Sy --needed --noconfirm "${pkgs[@]}"
;;
debian)
sudo apt update
sudo apt install -y "${pkgs[@]}"
;;
fedora)
sudo dnf install -y "${pkgs[@]}"
;;
esac
}

94
lib/shell_config.sh Normal file
View File

@@ -0,0 +1,94 @@
#!/usr/bin/env bash
# Shell configuration file manipulation utilities for bootstrap CLI
if [ -n "${_LIB_SHELL_CONFIG_SOURCED:-}" ]; then
return 0
fi
_LIB_SHELL_CONFIG_SOURCED=1
# Source common utilities if not already loaded
if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then
_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$_LIB_DIR/common.sh"
fi
# Find existing target shell RC files
get_shell_configs() {
local target_files=()
[ -f "$HOME/.bashrc" ] && target_files+=("$HOME/.bashrc")
[ -f "$HOME/.zshrc" ] && target_files+=("$HOME/.zshrc")
echo "${target_files[@]}"
}
# Remove block from a shell RC file
# Usage: remove_block <config_file> <block_name>
remove_block() {
local config_file="$1"
local block_name="$2"
if [ -f "$config_file" ] && grep -q "# >>> $block_name >>>" "$config_file" 2>/dev/null; then
log_info "Removing block '$block_name' from $config_file"
# We use a temporary file to avoid issues with sed in-place options across BSD/GNU
local tmp_file
tmp_file=$(mktemp)
sed "/# >>> $block_name >>>/,/# <<< $block_name <<</d" "$config_file" > "$tmp_file"
cat "$tmp_file" > "$config_file"
rm -f "$tmp_file"
fi
}
# Append block to a shell RC file
# Usage: inject_block <config_file> <block_name> <content>
inject_block() {
local config_file="$1"
local block_name="$2"
local content="$3"
remove_block "$config_file" "$block_name"
log_info "Adding block '$block_name' to $config_file"
{
echo ""
echo "# >>> $block_name >>>"
echo "$content"
echo "# <<< $block_name <<<"
} >> "$config_file"
}
# Add alias if not present
# Usage: add_alias_if_missing <config_file> <alias_name> <alias_value>
add_alias_if_missing() {
local config_file="$1"
local name="$2"
local val="$3"
if [ -f "$config_file" ] && ! grep -q "alias $name=" "$config_file" 2>/dev/null; then
log_info "Adding alias $name to $config_file"
echo "alias ${name}=\"${val}\"" >> "$config_file"
return 0 # Added
fi
return 1 # Not added (already existed or file doesn't exist)
}
# Add environment variable if not present
# Usage: add_env_if_missing <config_file> <var_name> <var_value>
add_env_if_missing() {
local config_file="$1"
local name="$2"
local val="$3"
if [ -f "$config_file" ] && ! grep -q "export $name=" "$config_file" 2>/dev/null; then
log_info "Setting $name in $config_file"
echo "export ${name}=\"${val}\"" >> "$config_file"
return 0 # Added
fi
return 1 # Not added
}
# Setup fd symlink for Debian/Ubuntu (fdfind -> fd)
create_fd_symlink() {
if ! has_command fd && has_command fdfind; then
log_info "Creating symlink for fd..."
sudo ln -sf "$(command -v fdfind)" /usr/local/bin/fd
fi
}