complete-config

This commit is contained in:
kumar vaibhav 2025-11-21 22:16:20 +05:30
parent 02ac070c2e
commit 47c58ae7eb

95
forge
View file

@ -36,7 +36,7 @@ log_error() {
# Initialize command - creates forge.toml and .config directory # Initialize command - creates forge.toml and .config directory
init() { init() {
log_info "Initializing forge..." log_info "Initializing forge..."
# Create directory # Create directory
mkdir -p "$FORGE_DIR" mkdir -p "$FORGE_DIR"
@ -93,22 +93,22 @@ parse_toml() {
while IFS= read -r line; do while IFS= read -r line; do
# Skip comments and empty lines # Skip comments and empty lines
[[ -z "$line" || "$line" == \#* ]] && continue [[ -z "$line" || "$line" == \#* ]] && continue
# Check for section headers # Check for section headers
if [[ "$line" =~ ^\[(.+)\]$ ]]; then if [[ "$line" =~ ^\[(.+)\]$ ]]; then
section="${BASH_REMATCH[1]}" section="${BASH_REMATCH[1]}"
continue continue
fi fi
# Parse key-value pairs for non-packages sections # Parse key-value pairs for non-packages sections
if [[ "$section" != "packages" && "$line" =~ ^([^=]+)=(.+)$ ]]; then if [[ "$section" != "packages" && "$line" =~ ^([^=]+)=(.+)$ ]]; then
local key="${BASH_REMATCH[1]}" local key="${BASH_REMATCH[1]}"
local value="${BASH_REMATCH[2]}" local value="${BASH_REMATCH[2]}"
# Trim leading/trailing whitespace from key and value # Trim leading/trailing whitespace from key and value
key=$(echo "$key" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') key=$(echo "$key" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
value=$(echo "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') value=$(echo "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
# Remove quotes from quoted strings # Remove quotes from quoted strings
if [[ "$key" =~ ^\"(.*)\"$ ]]; then if [[ "$key" =~ ^\"(.*)\"$ ]]; then
key="${BASH_REMATCH[1]}" key="${BASH_REMATCH[1]}"
@ -116,7 +116,7 @@ parse_toml() {
if [[ "$value" =~ ^\"(.*)\"$ ]]; then if [[ "$value" =~ ^\"(.*)\"$ ]]; then
value="${BASH_REMATCH[1]}" value="${BASH_REMATCH[1]}"
fi fi
echo "$section:$key=$value" echo "$section:$key=$value"
# Parse package names (just keys without values in packages section) # Parse package names (just keys without values in packages section)
elif [[ "$section" == "packages" && "$line" =~ ^([^=]+)$ ]]; then elif [[ "$section" == "packages" && "$line" =~ ^([^=]+)$ ]]; then
@ -133,13 +133,13 @@ parse_toml() {
# Enable COPR repositories from forge.toml # Enable COPR repositories from forge.toml
enable_copr_repos() { enable_copr_repos() {
log_info "Enabling COPR repositories..." log_info "Enabling COPR repositories..."
local copr_enabled=false local copr_enabled=false
while IFS='=' read -r entry; do while IFS='=' read -r entry; do
local section="${entry%%:*}" local section="${entry%%:*}"
local copr_repo="${entry#*:}" local copr_repo="${entry#*:}"
if [[ "$section" == "copr" && -n "$copr_repo" ]]; then if [[ "$section" == "copr" && -n "$copr_repo" ]]; then
copr_enabled=true copr_enabled=true
log_info "Enabling COPR repository: $copr_repo" log_info "Enabling COPR repository: $copr_repo"
@ -150,7 +150,7 @@ enable_copr_repos() {
fi fi
fi fi
done < <(parse_toml) done < <(parse_toml)
if [[ "$copr_enabled" == false ]]; then if [[ "$copr_enabled" == false ]]; then
log_info "No COPR repositories to enable" log_info "No COPR repositories to enable"
fi fi
@ -159,21 +159,21 @@ enable_copr_repos() {
# Disable COPR repositories that are no longer in forge.toml # Disable COPR repositories that are no longer in forge.toml
disable_copr_repos() { disable_copr_repos() {
log_info "Checking for COPR repositories to disable..." log_info "Checking for COPR repositories to disable..."
# Get currently enabled COPR repos # Get currently enabled COPR repos
local enabled_repos=$(sudo dnf copr list --enabled 2>/dev/null | grep -E "copr\.fedorainfracloud\.org" | awk '{print $1}' || true) local enabled_repos=$(sudo dnf copr list --enabled 2>/dev/null | grep -E "copr\.fedorainfracloud\.org" | awk '{print $1}' || true)
# Get configured COPR repos from forge.toml # Get configured COPR repos from forge.toml
local configured_repos="" local configured_repos=""
while IFS='=' read -r entry; do while IFS='=' read -r entry; do
local section="${entry%%:*}" local section="${entry%%:*}"
local copr_repo="${entry#*:}" local copr_repo="${entry#*:}"
if [[ "$section" == "copr" && -n "$copr_repo" ]]; then if [[ "$section" == "copr" && -n "$copr_repo" ]]; then
configured_repos="$configured_repos $copr_repo" configured_repos="$configured_repos $copr_repo"
fi fi
done < <(parse_toml) done < <(parse_toml)
# Disable repos that are enabled but not configured # Disable repos that are enabled but not configured
for repo in $enabled_repos; do for repo in $enabled_repos; do
if [[ ! " $configured_repos " =~ " $repo " ]]; then if [[ ! " $configured_repos " =~ " $repo " ]]; then
@ -190,13 +190,13 @@ disable_copr_repos() {
# Install packages from forge.toml # Install packages from forge.toml
install_packages() { install_packages() {
log_info "Installing packages..." log_info "Installing packages..."
local packages_installed=false local packages_installed=false
while IFS='=' read -r entry; do while IFS='=' read -r entry; do
local section="${entry%%:*}" local section="${entry%%:*}"
local package="${entry#*:}" local package="${entry#*:}"
if [[ "$section" == "packages" && -n "$package" ]]; then if [[ "$section" == "packages" && -n "$package" ]]; then
packages_installed=true packages_installed=true
log_info "Installing package: $package" log_info "Installing package: $package"
@ -207,7 +207,7 @@ install_packages() {
fi fi
fi fi
done < <(parse_toml) done < <(parse_toml)
if [[ "$packages_installed" == false ]]; then if [[ "$packages_installed" == false ]]; then
log_info "No packages to install" log_info "No packages to install"
fi fi
@ -216,29 +216,29 @@ install_packages() {
# Deploy dotfiles using symlinks # Deploy dotfiles using symlinks
deploy_dotfiles() { deploy_dotfiles() {
log_info "Deploying dotfiles..." log_info "Deploying dotfiles..."
local dotfiles_deployed=false local dotfiles_deployed=false
while IFS='=' read -r entry; do while IFS='=' read -r entry; do
local section="${entry%%:*}" local section="${entry%%:*}"
local key_value="${entry#*:}" local key_value="${entry#*:}"
local target="${key_value%%=*}" local target="${key_value%%=*}"
local source="${key_value##*=}" local source="${key_value##*=}"
if [[ "$section" == "dotfiles" ]]; then if [[ "$section" == "dotfiles" ]]; then
dotfiles_deployed=true dotfiles_deployed=true
local target_path="$HOME/$target" local target_path="$HOME/$target"
local source_path="$FORGE_DIR/$source" local source_path="$FORGE_DIR/$source"
# Create source directory if it doesn't exist # Create source directory if it doesn't exist
local source_dir=$(dirname "$source_path") local source_dir=$(dirname "$source_path")
mkdir -p "$source_dir" mkdir -p "$source_dir"
# Create target directory if it doesn't exist # Create target directory if it doesn't exist
local target_dir=$(dirname "$target_path") local target_dir=$(dirname "$target_path")
mkdir -p "$target_dir" mkdir -p "$target_dir"
# Backup existing file if it exists and is not a symlink # Backup existing file if it exists and is not a symlink
if [[ -f "$target_path" && ! -L "$target_path" ]]; then if [[ -f "$target_path" && ! -L "$target_path" ]]; then
local backup_dir="$FORGE_DIR/backup" local backup_dir="$FORGE_DIR/backup"
@ -247,10 +247,10 @@ deploy_dotfiles() {
cp "$target_path" "$backup_path" cp "$target_path" "$backup_path"
log_info "Backed up $target_path to $backup_path" log_info "Backed up $target_path to $backup_path"
fi fi
# Remove existing file/symlink # Remove existing file/symlink
rm -f "$target_path" rm -f "$target_path"
# Create symlink # Create symlink
if ln -s "$source_path" "$target_path"; then if ln -s "$source_path" "$target_path"; then
log_success "Created symlink: $target_path -> $source_path" log_success "Created symlink: $target_path -> $source_path"
@ -259,7 +259,7 @@ deploy_dotfiles() {
fi fi
fi fi
done < <(parse_toml) done < <(parse_toml)
if [[ "$dotfiles_deployed" == false ]]; then if [[ "$dotfiles_deployed" == false ]]; then
log_info "No dotfiles to deploy" log_info "No dotfiles to deploy"
fi fi
@ -268,30 +268,30 @@ deploy_dotfiles() {
# Switch command - apply configuration # Switch command - apply configuration
switch() { switch() {
log_info "Starting forge switch..." log_info "Starting forge switch..."
# Check if forge.toml exists # Check if forge.toml exists
if [[ ! -f "$FORGE_TOML" ]]; then if [[ ! -f "$FORGE_TOML" ]]; then
log_error "forge.toml not found. Run 'forge init' first." log_error "forge.toml not found. Run 'forge init' first."
exit 1 exit 1
fi fi
# Enable COPR repositories # Enable COPR repositories
enable_copr_repos enable_copr_repos
# Disable COPR repositories that are no longer configured # Disable COPR repositories that are no longer configured
disable_copr_repos disable_copr_repos
# Install packages # Install packages
install_packages install_packages
# Deploy dotfiles # Deploy dotfiles
deploy_dotfiles deploy_dotfiles
# Update last_switch timestamp # Update last_switch timestamp
if command -v sed >/dev/null 2>&1; then if command -v sed >/dev/null 2>&1; then
sed -i "s/last_switch = .*/last_switch = \"$(date -Iseconds)\"/" "$FORGE_TOML" sed -i "s/last_switch = .*/last_switch = \"$(date -Iseconds)\"/" "$FORGE_TOML"
fi fi
log_success "Switch completed successfully!" log_success "Switch completed successfully!"
} }
@ -300,11 +300,11 @@ status() {
log_info "Forge Status" log_info "Forge Status"
echo " Config directory: $FORGE_DIR" echo " Config directory: $FORGE_DIR"
echo " Config file: $FORGE_TOML" echo " Config file: $FORGE_TOML"
if [[ -f "$FORGE_TOML" ]]; then if [[ -f "$FORGE_TOML" ]]; then
echo echo
log_info "Configuration:" log_info "Configuration:"
# Show last switch time # Show last switch time
local last_switch=$(grep "last_switch" "$FORGE_TOML" | cut -d'=' -f2 | tr -d ' "') local last_switch=$(grep "last_switch" "$FORGE_TOML" | cut -d'=' -f2 | tr -d ' "')
if [[ -n "$last_switch" && "$last_switch" != "null" ]]; then if [[ -n "$last_switch" && "$last_switch" != "null" ]]; then
@ -312,45 +312,45 @@ status() {
else else
echo " Last switch: Never" echo " Last switch: Never"
fi fi
echo echo
log_info "COPR Repositories:" log_info "COPR Repositories:"
local copr_count=0 local copr_count=0
while IFS='=' read -r entry; do while IFS='=' read -r entry; do
local section="${entry%%:*}" local section="${entry%%:*}"
local copr_repo="${entry#*:}" local copr_repo="${entry#*:}"
if [[ "$section" == "copr" && -n "$copr_repo" ]]; then if [[ "$section" == "copr" && -n "$copr_repo" ]]; then
echo " $copr_repo" echo " $copr_repo"
((copr_count++)) ((copr_count++))
fi fi
done < <(parse_toml) done < <(parse_toml)
if [[ $copr_count -eq 0 ]]; then if [[ $copr_count -eq 0 ]]; then
echo " No COPR repositories configured" echo " No COPR repositories configured"
else else
echo " Total: $copr_count COPR repositories" echo " Total: $copr_count COPR repositories"
fi fi
echo echo
log_info "Packages:" log_info "Packages:"
local package_count=0 local package_count=0
while IFS='=' read -r entry; do while IFS='=' read -r entry; do
local section="${entry%%:*}" local section="${entry%%:*}"
local package="${entry#*:}" local package="${entry#*:}"
if [[ "$section" == "packages" && -n "$package" ]]; then if [[ "$section" == "packages" && -n "$package" ]]; then
echo " $package" echo " $package"
((package_count++)) ((package_count++))
fi fi
done < <(parse_toml) done < <(parse_toml)
if [[ $package_count -eq 0 ]]; then if [[ $package_count -eq 0 ]]; then
echo " No packages configured" echo " No packages configured"
else else
echo " Total: $package_count packages" echo " Total: $package_count packages"
fi fi
echo echo
log_info "Dotfiles:" log_info "Dotfiles:"
local dotfile_count=0 local dotfile_count=0
@ -359,13 +359,13 @@ status() {
local key_value="${entry#*:}" local key_value="${entry#*:}"
local target="${key_value%%=*}" local target="${key_value%%=*}"
local source="${key_value##*=}" local source="${key_value##*=}"
if [[ "$section" == "dotfiles" ]]; then if [[ "$section" == "dotfiles" ]]; then
echo " $target -> $source" echo " $target -> $source"
((dotfile_count++)) ((dotfile_count++))
fi fi
done < <(parse_toml) done < <(parse_toml)
if [[ $dotfile_count -eq 0 ]]; then if [[ $dotfile_count -eq 0 ]]; then
echo " No dotfiles configured" echo " No dotfiles configured"
else else
@ -439,4 +439,5 @@ main() {
} }
# Run main function with all arguments # Run main function with all arguments
main "$@" main "$@"