This commit is contained in:
kumar vaibhav 2025-11-21 18:39:07 +05:30
parent 724ea5f6d1
commit 0cf22d6e5e

177
forge
View file

@ -1,16 +1,16 @@
#!/bin/bash #!/bin/bash
# fedora-home-manager - A home-manager like script for Fedora # forge - A home-manager like script for Fedora
# Provides dotfile management and package installation functionality # Provides dotfile management and package installation functionality
set -euo pipefail set -euo pipefail
# Configuration # Configuration
HOME_MANAGER_DIR="${HOME_MANAGER_DIR:-$HOME/.config/fedora-home-manager}" FORGE_DIR="${FORGE_DIR:-$HOME/.config/forge}"
DOTFILES_DIR="$HOME_MANAGER_DIR/dotfiles" DOTFILES_DIR="$FORGE_DIR/dotfiles"
PACKAGES_FILE="$HOME_MANAGER_DIR/packages.toml" PACKAGES_FILE="$FORGE_DIR/packages.toml"
STATE_FILE="$HOME_MANAGER_DIR/state.json" STATE_FILE="$FORGE_DIR/state.json"
BACKUP_DIR="$HOME_MANAGER_DIR/backup" BACKUP_DIR="$FORGE_DIR/backup"
# Colors # Colors
RED='\033[0;31m' RED='\033[0;31m'
@ -38,10 +38,10 @@ log_error() {
# Initialize command - set up the configuration structure # Initialize command - set up the configuration structure
init() { init() {
log_info "Initializing fedora-home-manager..." log_info "Initializing forge..."
# Create directories # Create directories
mkdir -p "$HOME_MANAGER_DIR" mkdir -p "$FORGE_DIR"
mkdir -p "$DOTFILES_DIR" mkdir -p "$DOTFILES_DIR"
mkdir -p "$BACKUP_DIR" mkdir -p "$BACKUP_DIR"
@ -51,21 +51,20 @@ init() {
log_success "Created state file: $STATE_FILE" log_success "Created state file: $STATE_FILE"
fi fi
# Create initial packages file with TOML format if it doesn't exist # Create initial packages file with simplified format if it doesn't exist
if [[ ! -f "$PACKAGES_FILE" ]]; then if [[ ! -f "$PACKAGES_FILE" ]]; then
cat >"$PACKAGES_FILE" <<'EOF' cat >"$PACKAGES_FILE" <<'EOF'
# Fedora packages configuration # Forge packages configuration
# TOML format for better package management # Simple format: just list package names, one per line
[packages]
# Core development tools # Core development tools
# git = "latest" # git
# vim = "latest" # vim
# curl = "latest" # curl
# wget = "latest" # wget
# Additional packages can be added with version specifications # Additional packages can be added as simple package names
# Example: package_name = "version" or "latest" # Example: package_name
EOF EOF
log_success "Created packages file: $PACKAGES_FILE" log_success "Created packages file: $PACKAGES_FILE"
fi fi
@ -78,12 +77,12 @@ EOF
log_success "Initialization complete!" log_success "Initialization complete!"
log_info "Add packages to $PACKAGES_FILE and dotfiles to $DOTFILES_DIR/" log_info "Add packages to $PACKAGES_FILE and dotfiles to $DOTFILES_DIR/"
log_info "Run 'fedora-home-manager switch' to apply your configuration" log_info "Run 'forge switch' to apply your configuration"
} }
# Initialize directories # Initialize directories
init_dirs() { init_dirs() {
mkdir -p "$HOME_MANAGER_DIR" mkdir -p "$FORGE_DIR"
mkdir -p "$DOTFILES_DIR" mkdir -p "$DOTFILES_DIR"
mkdir -p "$BACKUP_DIR" mkdir -p "$BACKUP_DIR"
@ -135,14 +134,14 @@ install_packages() {
local current_packages=() local current_packages=()
# Parse TOML and extract package names # Parse simple format and extract package names
while IFS= read -r line; do while IFS= read -r line; do
# Skip empty lines, comments, and section headers # Skip empty lines and comments
[[ -z "$line" || "$line" == \#* || "$line" == \[* ]] && continue [[ -z "$line" || "$line" == \#* ]] && continue
# Extract package name (before =) # Remove whitespace and use as package name
if [[ "$line" =~ ^[[:space:]]*([^=]+)[[:space:]]*= ]]; then local package="${line// /}" # Remove whitespace
local package="${BASH_REMATCH[1]// /}" # Remove whitespace if [[ -n "$package" ]]; then
current_packages+=("$package") current_packages+=("$package")
log_info "Installing package: $package" log_info "Installing package: $package"
@ -166,12 +165,12 @@ cleanup_packages() {
local current_packages=() local current_packages=()
if [[ -f "$PACKAGES_FILE" ]]; then if [[ -f "$PACKAGES_FILE" ]]; then
while IFS= read -r line; do while IFS= read -r line; do
# Skip empty lines, comments, and section headers # Skip empty lines and comments
[[ -z "$line" || "$line" == \#* || "$line" == \[* ]] && continue [[ -z "$line" || "$line" == \#* ]] && continue
# Extract package name (before =) # Remove whitespace and use as package name
if [[ "$line" =~ ^[[:space:]]*([^=]+)[[:space:]]*= ]]; then local package="${line// /}" # Remove whitespace
local package="${BASH_REMATCH[1]// /}" # Remove whitespace if [[ -n "$package" ]]; then
current_packages+=("$package") current_packages+=("$package")
fi fi
done <"$PACKAGES_FILE" done <"$PACKAGES_FILE"
@ -282,7 +281,7 @@ get_installed_packages() {
# Switch command - main deployment function # Switch command - main deployment function
switch() { switch() {
log_info "Starting fedora-home-manager switch..." log_info "Starting forge switch..."
# Initialize directories # Initialize directories
init_dirs init_dirs
@ -358,47 +357,25 @@ remove_dotfile() {
fi fi
} }
# List managed files # Generate random Linux-inspired name
list_managed() { generate_forge_name() {
log_info "Managed dotfiles:" local adjectives=("swift" "bold" "noble" "fierce" "wise" "ancient" "crystal" "iron" "steel" "shadow" "light" "dark" "golden" "silver" "bronze" "cosmic" "quantum" "neural" "atomic" "stellar")
if [[ -d "$DOTFILES_DIR" ]]; then local nouns=("eagle" "wolf" "lion" "tiger" "dragon" "phoenix" "hawk" "falcon" "bear" "panther" "lynx" "owl" "raven" "cobra" "viper" "python" "jaguar" "leopard" "cheetah" "cougar")
find "$DOTFILES_DIR" -type f | sed "s|$DOTFILES_DIR/| |" | sort local suffix=("linux" "kernel" "system" "forge" "core" "engine" "matrix" "network" "protocol" "framework" "platform" "architecture" "interface" "module" "library" "runtime" "daemon" "service" "process" "thread")
fi
log_info "Managed packages:" local adj=${adjectives[$RANDOM % ${#adjectives[@]}]}
if [[ -f "$PACKAGES_FILE" ]]; then local noun=${nouns[$RANDOM % ${#nouns[@]}]}
while IFS= read -r line; do local suff=${suffix[$RANDOM % ${#suffix[@]}]}
# Skip empty lines, comments, and section headers
[[ -z "$line" || "$line" == \#* || "$line" == \[* ]] && continue
# Extract package name (before =) echo "${adj}-${noun}-${suff}"
if [[ "$line" =~ ^[[:space:]]*([^=]+)[[:space:]]*= ]]; then
local package="${BASH_REMATCH[1]// /}" # Remove whitespace
echo " $package"
fi
done <"$PACKAGES_FILE"
# Check if any packages were found
local package_count=0
while IFS= read -r line; do
[[ -z "$line" || "$line" == \#* || "$line" == \[* ]] && continue
if [[ "$line" =~ ^[[:space:]]*([^=]+)[[:space:]]*= ]]; then
((package_count++))
fi
done <"$PACKAGES_FILE"
if [[ $package_count -eq 0 ]]; then
log_info " No packages"
fi
else
log_info " No packages"
fi
} }
# Show status # List managed files with status
status() { list_managed() {
log_info "Fedora Home Manager Status:" local forge_name=$(generate_forge_name)
echo " Config directory: $HOME_MANAGER_DIR"
log_info "Forge Status: $forge_name"
echo " Config directory: $FORGE_DIR"
echo " Dotfiles directory: $DOTFILES_DIR" echo " Dotfiles directory: $DOTFILES_DIR"
echo " Packages file: $PACKAGES_FILE" echo " Packages file: $PACKAGES_FILE"
@ -412,16 +389,57 @@ status() {
fi fi
echo echo
log_info "Managed dotfiles:"
if [[ -d "$DOTFILES_DIR" ]]; then
local dotfile_count=$(find "$DOTFILES_DIR" -type f | wc -l)
if [[ $dotfile_count -gt 0 ]]; then
find "$DOTFILES_DIR" -type f | sed "s|$DOTFILES_DIR/| |" | sort
echo " Total: $dotfile_count dotfiles"
else
echo " No dotfiles"
fi
else
echo " No dotfiles directory"
fi
echo
log_info "Managed packages:"
if [[ -f "$PACKAGES_FILE" ]]; then
local package_count=0
while IFS= read -r line; do
# Skip empty lines and comments
[[ -z "$line" || "$line" == \#* ]] && continue
# Remove whitespace and count as package
local package="${line// /}" # Remove whitespace
if [[ -n "$package" ]]; then
echo " $package"
((package_count++))
fi
done <"$PACKAGES_FILE"
if [[ $package_count -eq 0 ]]; then
echo " No packages"
else
echo " Total: $package_count packages"
fi
else
echo " No packages file"
fi
}
# Show status
status() {
list_managed list_managed
} }
# Show help # Show help
show_help() { show_help() {
cat <<EOF cat <<EOF
fedora-home-manager - A home-manager like script for Fedora forge - A home-manager like script for Fedora
USAGE: USAGE:
fedora-home-manager <COMMAND> forge <COMMAND>
COMMANDS: COMMANDS:
init Initialize configuration structure init Initialize configuration structure
@ -429,7 +447,7 @@ show_help() {
build Alias for switch build Alias for switch
add <file> Add a file to dotfiles management add <file> Add a file to dotfiles management
remove <path> Remove a dotfile from management remove <path> Remove a dotfile from management
list List all managed files and packages list List all managed files and packages with status
status Show current status status Show current status
help Show this help message help Show this help message
@ -437,19 +455,20 @@ show_help() {
- Automatic package cleanup: When you remove a package from packages.toml and run switch, - Automatic package cleanup: When you remove a package from packages.toml and run switch,
the package will be automatically uninstalled from your system. the package will be automatically uninstalled from your system.
- Package state tracking: Previously installed packages are tracked to enable cleanup. - Package state tracking: Previously installed packages are tracked to enable cleanup.
- TOML format: Packages are managed in TOML format for better organization and version support. - Simple format: Packages are managed in simple format (no version tags required).
- Random Linux-inspired names: Each list command generates a unique forge name.
FILES: FILES:
$HOME_MANAGER_DIR/ Main configuration directory $FORGE_DIR/ Main configuration directory
$DOTFILES_DIR/ Dotfiles to deploy $DOTFILES_DIR/ Dotfiles to deploy
$PACKAGES_FILE TOML file with packages to install (supports version specifications) $PACKAGES_FILE Simple file with packages to install (one package per line)
$STATE_FILE State tracking file for managed packages and files $STATE_FILE State tracking file for managed packages and files
EXAMPLES: EXAMPLES:
fedora-home-manager init forge init
fedora-home-manager add ~/.bashrc forge add ~/.bashrc
fedora-home-manager switch forge switch
fedora-home-manager list forge list
EOF EOF
} }