diff options
| author | Craig Jennings <c@cjennings.net> | 2026-01-23 15:45:55 -0600 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-01-23 15:45:55 -0600 |
| commit | 74fb6ab9cc2b8d38ea75a6b52b67ec34281a2338 (patch) | |
| tree | 296bae39d02b8d20e250b71d9b2e6bc2c0ba5344 /custom/lib/common.sh | |
| parent | b95cf953ea13fe5adeb55322e6aa9f750906dd94 (diff) | |
| download | archangel-74fb6ab9cc2b8d38ea75a6b52b67ec34281a2338.tar.gz archangel-74fb6ab9cc2b8d38ea75a6b52b67ec34281a2338.zip | |
Phase 1.1: Create lib/ directory structure for archangel refactor
- Add custom/lib/common.sh: output, validation, fzf prompts, disk utils
- Add custom/lib/config.sh: argument parsing, config loading, validation
- Add custom/lib/disk.sh: partitioning, EFI management, disk selection
- Add custom/lib/zfs.sh: pool, datasets, ZFSBootMenu, services, hooks
- Update install-archzfs to source libs
- Remove duplicated output/config functions from main script
Prepares codebase for btrfs filesystem support (Phase 2).
Diffstat (limited to 'custom/lib/common.sh')
| -rw-r--r-- | custom/lib/common.sh | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/custom/lib/common.sh b/custom/lib/common.sh new file mode 100644 index 0000000..a41441d --- /dev/null +++ b/custom/lib/common.sh @@ -0,0 +1,136 @@ +#!/usr/bin/env bash +# common.sh - Shared functions for archangel installer +# Source this file: source "$(dirname "$0")/lib/common.sh" + +############################# +# Output Functions +############################# + +# Colors (optional, gracefully degrade if not supported) +if [[ -t 1 ]]; then + RED='\033[0;31m' + GREEN='\033[0;32m' + YELLOW='\033[0;33m' + BLUE='\033[0;34m' + BOLD='\033[1m' + NC='\033[0m' # No Color +else + RED='' + GREEN='' + YELLOW='' + BLUE='' + BOLD='' + NC='' +fi + +info() { echo -e "${GREEN}[INFO]${NC} $1"; } +warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } +error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; } +step() { echo ""; echo -e "${BOLD}==> $1${NC}"; } +prompt() { echo -e "${BLUE}$1${NC}"; } + +# Log to file if LOG_FILE is set +log() { + local msg="[$(date +'%Y-%m-%d %H:%M:%S')] $1" + if [[ -n "$LOG_FILE" ]]; then + echo "$msg" >> "$LOG_FILE" + fi +} + +############################# +# Validation Functions +############################# + +require_root() { + [[ $EUID -ne 0 ]] && error "This script must be run as root" +} + +command_exists() { + command -v "$1" &>/dev/null +} + +require_command() { + command_exists "$1" || error "Required command not found: $1" +} + +############################# +# FZF Prompts +############################# + +# Check if fzf is available +has_fzf() { + command_exists fzf +} + +# Generic fzf selection +# Usage: result=$(fzf_select "prompt" "option1" "option2" ...) +fzf_select() { + local prompt="$1" + shift + local options=("$@") + + if has_fzf; then + printf '%s\n' "${options[@]}" | fzf --prompt="$prompt " --height=15 --reverse + else + # Fallback to simple select + PS3="$prompt " + select opt in "${options[@]}"; do + if [[ -n "$opt" ]]; then + echo "$opt" + break + fi + done + fi +} + +# Multi-select with fzf +# Usage: readarray -t results < <(fzf_multi "prompt" "opt1" "opt2" ...) +fzf_multi() { + local prompt="$1" + shift + local options=("$@") + + if has_fzf; then + printf '%s\n' "${options[@]}" | fzf --prompt="$prompt " --height=20 --reverse --multi + else + # Fallback: just return all options (user must edit) + printf '%s\n' "${options[@]}" + fi +} + +############################# +# Disk Utilities +############################# + +# Get disk size in human-readable format +get_disk_size() { + local disk="$1" + lsblk -dno SIZE "$disk" 2>/dev/null | tr -d ' ' +} + +# Get disk model +get_disk_model() { + local disk="$1" + lsblk -dno MODEL "$disk" 2>/dev/null | tr -d ' ' | head -c 20 +} + +# Check if disk is in use (mounted or has holders) +disk_in_use() { + local disk="$1" + [[ -n "$(lsblk -no MOUNTPOINT "$disk" 2>/dev/null | grep -v '^$')" ]] && return 0 + [[ -n "$(ls /sys/block/"$(basename "$disk")"/holders/ 2>/dev/null)" ]] && return 0 + return 1 +} + +# List available disks (not in use) +list_available_disks() { + local disks=() + for disk in /dev/nvme[0-9]n[0-9] /dev/sd[a-z] /dev/vd[a-z]; do + [[ -b "$disk" ]] || continue + disk_in_use "$disk" && continue + local size=$(get_disk_size "$disk") + local model=$(get_disk_model "$disk") + disks+=("$disk ($size, $model)") + done + printf '%s\n' "${disks[@]}" +} |
