curator/forge

334 lines
7.9 KiB
Text
Raw Normal View History

2025-11-19 22:34:29 +05:30
#!/bin/bash
2025-11-21 18:39:07 +05:30
# forge - A home-manager like script for Fedora
2025-11-19 22:34:29 +05:30
# Provides dotfile management and package installation functionality
set -euo pipefail
# Configuration
2025-11-21 18:39:07 +05:30
FORGE_DIR="${FORGE_DIR:-$HOME/.config/forge}"
2025-11-21 19:45:32 +05:30
FORGE_TOML="$FORGE_DIR/forge.toml"
2025-11-19 22:34:29 +05:30
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
2025-11-21 19:45:32 +05:30
# Initialize command - creates forge.toml and .config directory
2025-11-19 22:34:29 +05:30
init() {
2025-11-21 18:39:07 +05:30
log_info "Initializing forge..."
2025-11-19 22:34:29 +05:30
2025-11-21 19:45:32 +05:30
# Create directory
2025-11-21 18:39:07 +05:30
mkdir -p "$FORGE_DIR"
2025-11-19 22:34:29 +05:30
2025-11-21 19:45:32 +05:30
# Create initial forge.toml if it doesn't exist
if [[ ! -f "$FORGE_TOML" ]]; then
cat >"$FORGE_TOML" <<'EOF'
# Forge Configuration File
# User-level configuration similar to home-manager/nixos
[forge]
version = "1.0"
last_switch = null
[packages]
# List of packages to install using dnf
# Examples:
# git = true
# vim = true
# curl = true
[dotfiles]
# Dotfiles to manage with symlinks
# Format: "target_path" = "source_path"
# target_path: where the symlink should be created (relative to home directory)
# source_path: where the actual file is stored (relative to forge directory)
# Examples:
# ".bashrc" = "dotfiles/.bashrc"
# ".config/vimrc" = "dotfiles/vimrc"
[options]
# Additional options
backup = true
backup_dir = "backup"
2025-11-19 22:34:29 +05:30
EOF
2025-11-21 19:45:32 +05:30
log_success "Created forge.toml: $FORGE_TOML"
2025-11-19 22:34:29 +05:30
fi
log_success "Initialization complete!"
2025-11-21 19:45:32 +05:30
log_info "Edit $FORGE_TOML to configure packages and dotfiles"
2025-11-21 18:39:07 +05:30
log_info "Run 'forge switch' to apply your configuration"
2025-11-19 22:34:29 +05:30
}
2025-11-21 19:45:32 +05:30
# Parse TOML file (basic implementation)
parse_toml() {
local section=""
while IFS= read -r line; do
# Skip comments and empty lines
[[ -z "$line" || "$line" == \#* ]] && continue
# Check for section headers
if [[ "$line" =~ ^\[(.+)\]$ ]]; then
section="${BASH_REMATCH[1]}"
continue
fi
# Parse key-value pairs
if [[ "$line" =~ ^([^=]+)=(.+)$ ]]; then
local key="${BASH_REMATCH[1]// /}"
local value="${BASH_REMATCH[2]// /}"
echo "$section:$key=$value"
fi
done <"$FORGE_TOML"
2025-11-19 22:34:29 +05:30
}
2025-11-21 19:45:32 +05:30
# Install packages from forge.toml
2025-11-19 22:34:29 +05:30
install_packages() {
log_info "Installing packages..."
2025-11-21 19:45:32 +05:30
local packages_installed=false
while IFS='=' read -r entry; do
local section="${entry%%:*}"
local key_value="${entry#*:}"
local package="${key_value%%=*}"
local enabled="${key_value##*=}"
2025-11-19 22:34:29 +05:30
2025-11-21 19:45:32 +05:30
if [[ "$section" == "packages" && "$enabled" == "true" ]]; then
packages_installed=true
2025-11-20 17:07:23 +05:30
log_info "Installing package: $package"
if sudo dnf install -y "$package"; then
log_success "Installed $package"
else
log_error "Failed to install $package"
fi
2025-11-19 22:34:29 +05:30
fi
2025-11-21 19:45:32 +05:30
done < <(parse_toml)
if [[ "$packages_installed" == false ]]; then
log_info "No packages to install"
fi
2025-11-19 22:34:29 +05:30
}
2025-11-21 19:45:32 +05:30
# Deploy dotfiles using symlinks
deploy_dotfiles() {
log_info "Deploying dotfiles..."
local dotfiles_deployed=false
while IFS='=' read -r entry; do
local section="${entry%%:*}"
local key_value="${entry#*:}"
local target="${key_value%%=*}"
local source="${key_value##*=}"
if [[ "$section" == "dotfiles" ]]; then
dotfiles_deployed=true
2025-11-20 17:07:23 +05:30
2025-11-21 19:45:32 +05:30
local target_path="$HOME/$target"
local source_path="$FORGE_DIR/$source"
# Create source directory if it doesn't exist
local source_dir=$(dirname "$source_path")
mkdir -p "$source_dir"
# Create target directory if it doesn't exist
local target_dir=$(dirname "$target_path")
mkdir -p "$target_dir"
# Backup existing file if it exists and is not a symlink
if [[ -f "$target_path" && ! -L "$target_path" ]]; then
local backup_dir="$FORGE_DIR/backup"
mkdir -p "$backup_dir"
local backup_path="$backup_dir/$(basename "$target").$(date +%Y%m%d_%H%M%S).bak"
cp "$target_path" "$backup_path"
log_info "Backed up $target_path to $backup_path"
2025-11-19 22:34:29 +05:30
fi
2025-11-21 19:45:32 +05:30
# Remove existing file/symlink
rm -f "$target_path"
# Create symlink
if ln -s "$source_path" "$target_path"; then
log_success "Created symlink: $target_path -> $source_path"
2025-11-19 22:34:29 +05:30
else
2025-11-21 19:45:32 +05:30
log_error "Failed to create symlink: $target_path -> $source_path"
2025-11-19 22:34:29 +05:30
fi
2025-11-21 19:45:32 +05:30
fi
done < <(parse_toml)
2025-11-19 22:34:29 +05:30
2025-11-21 19:45:32 +05:30
if [[ "$dotfiles_deployed" == false ]]; then
log_info "No dotfiles to deploy"
2025-11-19 22:34:29 +05:30
fi
}
2025-11-21 19:45:32 +05:30
# Switch command - apply configuration
2025-11-19 22:34:29 +05:30
switch() {
2025-11-21 18:39:07 +05:30
log_info "Starting forge switch..."
2025-11-21 19:45:32 +05:30
# Check if forge.toml exists
if [[ ! -f "$FORGE_TOML" ]]; then
log_error "forge.toml not found. Run 'forge init' first."
exit 1
fi
2025-11-19 22:34:29 +05:30
# Install packages
install_packages
2025-11-21 19:45:32 +05:30
2025-11-19 22:34:29 +05:30
# Deploy dotfiles
deploy_dotfiles
2025-11-21 18:39:07 +05:30
2025-11-21 19:45:32 +05:30
# Update last_switch timestamp
if command -v sed >/dev/null 2>&1; then
sed -i "s/last_switch = .*/last_switch = \"$(date -Iseconds)\"/" "$FORGE_TOML"
fi
2025-11-21 18:39:07 +05:30
2025-11-21 19:45:32 +05:30
log_success "Switch completed successfully!"
2025-11-21 18:39:07 +05:30
}
2025-11-21 19:45:32 +05:30
# Status command - show current configuration
status() {
log_info "Forge Status"
2025-11-21 18:39:07 +05:30
echo " Config directory: $FORGE_DIR"
2025-11-21 19:45:32 +05:30
echo " Config file: $FORGE_TOML"
if [[ -f "$FORGE_TOML" ]]; then
echo
log_info "Configuration:"
# Show last switch time
local last_switch=$(grep "last_switch" "$FORGE_TOML" | cut -d'=' -f2 | tr -d ' "')
if [[ -n "$last_switch" && "$last_switch" != "null" ]]; then
2025-11-21 18:39:07 +05:30
echo " Last switch: $last_switch"
else
echo " Last switch: Never"
fi
2025-11-21 19:45:32 +05:30
echo
log_info "Packages:"
2025-11-21 18:39:07 +05:30
local package_count=0
2025-11-21 19:45:32 +05:30
while IFS='=' read -r entry; do
local section="${entry%%:*}"
local key_value="${entry#*:}"
local package="${key_value%%=*}"
local enabled="${key_value##*=}"
2025-11-20 17:07:23 +05:30
2025-11-21 19:45:32 +05:30
if [[ "$section" == "packages" && "$enabled" == "true" ]]; then
2025-11-20 17:07:23 +05:30
echo " $package"
((package_count++))
fi
2025-11-21 19:45:32 +05:30
done < <(parse_toml)
2025-11-20 17:07:23 +05:30
if [[ $package_count -eq 0 ]]; then
2025-11-21 19:45:32 +05:30
echo " No packages configured"
2025-11-21 18:39:07 +05:30
else
echo " Total: $package_count packages"
2025-11-20 17:07:23 +05:30
fi
2025-11-21 19:45:32 +05:30
echo
log_info "Dotfiles:"
local dotfile_count=0
while IFS='=' read -r entry; do
local section="${entry%%:*}"
local key_value="${entry#*:}"
local target="${key_value%%=*}"
local source="${key_value##*=}"
if [[ "$section" == "dotfiles" ]]; then
echo " $target -> $source"
((dotfile_count++))
fi
done < <(parse_toml)
if [[ $dotfile_count -eq 0 ]]; then
echo " No dotfiles configured"
else
echo " Total: $dotfile_count dotfiles"
fi
2025-11-20 17:07:23 +05:30
else
2025-11-21 19:45:32 +05:30
echo " No configuration file found"
2025-11-19 22:34:29 +05:30
fi
}
# Show help
show_help() {
cat <<EOF
2025-11-21 18:39:07 +05:30
forge - A home-manager like script for Fedora
2025-11-19 22:34:29 +05:30
USAGE:
2025-11-21 18:39:07 +05:30
forge <COMMAND>
2025-11-19 22:34:29 +05:30
COMMANDS:
2025-11-21 19:45:32 +05:30
init Initialize configuration structure (creates forge.toml)
switch Apply configuration (install packages, deploy dotfiles)
status Show current configuration status
2025-11-19 22:34:29 +05:30
help Show this help message
FEATURES:
2025-11-21 19:45:32 +05:30
- TOML-based configuration in forge.toml
- Package management via dnf
- Dotfile management via symlinks
- Automatic backup of existing files
- User-level configuration like home-manager/nixos
2025-11-20 17:07:23 +05:30
2025-11-21 19:45:32 +05:30
FILES:
$FORGE_DIR/ Main configuration directory
$FORGE_TOML Configuration file for packages and dotfiles
2025-11-19 22:34:29 +05:30
EXAMPLES:
2025-11-21 18:39:07 +05:30
forge init
2025-11-21 19:45:32 +05:30
# Edit $FORGE_TOML to add packages and dotfiles
2025-11-21 18:39:07 +05:30
forge switch
2025-11-21 19:45:32 +05:30
forge status
2025-11-19 22:34:29 +05:30
EOF
}
# Main script logic
main() {
case "${1:-}" in
"init")
init
;;
"switch")
switch
;;
"status")
status
;;
"help" | "--help" | "-h")
show_help
;;
"")
log_error "No command specified. Use 'help' for usage information."
exit 1
;;
*)
log_error "Unknown command: $1"
show_help
exit 1
;;
esac
}
# Run main function with all arguments
2025-11-21 19:45:32 +05:30
main "$@"