feat: replace custom logging functions with direct println calls for improved readability
Some checks failed
Nix Format Check / check-format (push) Failing after 38s

This commit is contained in:
2025-03-11 11:00:01 +01:00
parent c58fa69e2e
commit 3854200b18

105
setup.sh
View File

@ -87,31 +87,15 @@ printfe() {
printf "$color$format$normal" "$message" printf "$color$format$normal" "$message"
} }
# Helper functions # Helper function for error & exit
log_info() {
println "$1" "green"
}
log_success() {
println "$1" "green"
}
log_error() {
println "$1" "red" >&2
}
log_warning() {
println "$1" "yellow" >&2
}
die() { die() {
log_error "$1" println "$1" "red" >&2
exit 1 exit 1
} }
# Request sudo credentials upfront # Request sudo credentials upfront
request_sudo() { request_sudo() {
log_info "Requesting sudo privileges to avoid interruptions later..." println "Requesting sudo privileges to avoid interruptions later..." "green"
sudo -v || die "Failed to obtain sudo privileges" sudo -v || die "Failed to obtain sudo privileges"
# Keep sudo credentials refreshed in the background # Keep sudo credentials refreshed in the background
@ -121,7 +105,7 @@ request_sudo() {
# Ensure we kill the keepalive process when the script exits # Ensure we kill the keepalive process when the script exits
trap 'kill $SUDO_KEEPALIVE_PID 2>/dev/null || true' EXIT trap 'kill $SUDO_KEEPALIVE_PID 2>/dev/null || true' EXIT
log_success "Sudo privileges obtained." println "Sudo privileges obtained." "green"
} }
# Ensure we're running interactively # Ensure we're running interactively
@ -145,7 +129,7 @@ backup_file() {
local need_sudo="${2:-false}" local need_sudo="${2:-false}"
if [ -f "$file" ]; then if [ -f "$file" ]; then
log_info "Backing up $file to $file.bak..." println "Backing up $file to $file.bak..." "green"
if [ "$need_sudo" = "true" ]; then if [ "$need_sudo" = "true" ]; then
sudo mv "$file" "$file.bak" || die "Failed to backup $file (sudo)" sudo mv "$file" "$file.bak" || die "Failed to backup $file (sudo)"
else else
@ -154,7 +138,6 @@ backup_file() {
fi fi
} }
check_prerequisites() { check_prerequisites() {
command -v git >/dev/null 2>&1 || die "Git is required but not installed" command -v git >/dev/null 2>&1 || die "Git is required but not installed"
command -v sudo >/dev/null 2>&1 || die "Sudo is required but not installed" command -v sudo >/dev/null 2>&1 || die "Sudo is required but not installed"
@ -213,16 +196,16 @@ update_home_manager_flake() {
# Replace original file # Replace original file
mv "$temp_file" "$flake_file" || return 1 mv "$temp_file" "$flake_file" || return 1
log_success "Home Manager Flake configuration added successfully." println "Home Manager Flake configuration added successfully." "green"
} }
install_nix() { install_nix() {
if command -v nix-channel >/dev/null 2>&1; then if command -v nix-channel >/dev/null 2>&1; then
log_success "Detected Nix, skipping Nix setup." println "Detected Nix, skipping Nix setup." "green"
return 0 return 0
fi fi
log_info "Nix not detected, installing Nix..." println "Nix not detected, installing Nix..." "green"
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix -o install-nix.sh || \ curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix -o install-nix.sh || \
die "Failed to download Nix installer" die "Failed to download Nix installer"
@ -232,34 +215,34 @@ install_nix() {
} }
setup_symlinks() { setup_symlinks() {
log_info "Setting up symlinks..." println "Setting up symlinks..." "green"
# Backup and create symlinks for user files # Backup and create symlinks for user files
backup_file "$HOME/.bashrc" backup_file "$HOME/.bashrc"
backup_file "$HOME/.profile" backup_file "$HOME/.profile"
if [ -d "$HOME/.config/home-manager" ]; then if [ -d "$HOME/.config/home-manager" ]; then
log_info "Backing up ~/.config/home-manager to ~/.config/home-manager.bak..." println "Backing up ~/.config/home-manager to ~/.config/home-manager.bak..." "green"
mv "$HOME/.config/home-manager" "$HOME/.config/home-manager.bak" || \ mv "$HOME/.config/home-manager" "$HOME/.config/home-manager.bak" || \
die "Failed to backup home-manager config" die "Failed to backup home-manager config"
fi fi
log_info "Linking ~/.config/home-manager to $DOTFILES_PATH/config/home-manager..." println "Linking ~/.config/home-manager to $DOTFILES_PATH/config/home-manager..." "green"
ln -s "$DOTFILES_PATH/config/home-manager" "$HOME/.config/home-manager" || \ ln -s "$DOTFILES_PATH/config/home-manager" "$HOME/.config/home-manager" || \
die "Failed to create home-manager symlink" die "Failed to create home-manager symlink"
# Verify symlinks # Verify symlinks
confirm_symlink "$HOME/.config/home-manager" "Failed to set up home-manager symlink" confirm_symlink "$HOME/.config/home-manager" "Failed to set up home-manager symlink"
log_success "Symlinks set up successfully." println "Symlinks set up successfully." "green"
} }
install_home_manager() { install_home_manager() {
if command -v home-manager >/dev/null 2>&1; then if command -v home-manager >/dev/null 2>&1; then
log_success "Home Manager already installed. Skipping..." println "Home Manager already installed. Skipping..." "green"
return 0 return 0
fi fi
log_info "Installing Home Manager..." println "Installing Home Manager..." "green"
nix-channel --add "https://github.com/nix-community/home-manager/archive/release-$NIXOS_RELEASE.tar.gz" home-manager || die "Failed to add home-manager channel" nix-channel --add "https://github.com/nix-community/home-manager/archive/release-$NIXOS_RELEASE.tar.gz" home-manager || die "Failed to add home-manager channel"
nix-channel --update || die "Failed to update channels" nix-channel --update || die "Failed to update channels"
nix-shell '<home-manager>' -A install || die "Failed to install home-manager" nix-shell '<home-manager>' -A install || die "Failed to install home-manager"
@ -274,57 +257,57 @@ prepare_hostname() {
if ! validate_hostname "$hostname"; then if ! validate_hostname "$hostname"; then
die "Invalid hostname provided. Please use a valid hostname." die "Invalid hostname provided. Please use a valid hostname."
fi fi
log_info "Using provided hostname: $hostname" println "Using provided hostname: $hostname" "green"
# Check if hostname is already set # Check if hostname is already set
elif [ -f "$hostname_file" ]; then elif [ -f "$hostname_file" ]; then
hostname=$(cat "$hostname_file") hostname=$(cat "$hostname_file")
log_success "Hostname already found in $hostname_file. Using $hostname." println "Hostname already found in $hostname_file. Using $hostname." "green"
return return
else else
die "No hostname provided. Please provide a hostname as the first argument." die "No hostname provided. Please provide a hostname as the first argument."
fi fi
log_info "Setting hostname to $hostname..." println "Setting hostname to $hostname..." "green"
sudo hostnamectl set-hostname "$hostname" || die "Failed to set hostname" sudo hostnamectl set-hostname "$hostname" || die "Failed to set hostname"
echo "$hostname" > "$hostname_file" || die "Failed to save hostname" echo "$hostname" > "$hostname_file" || die "Failed to save hostname"
log_success "Hostname set successfully." println "Hostname set successfully." "green"
} }
warning_prompt() { warning_prompt() {
log_success "This script will set up your machine using Menno's Dotfiles repository.\n" println "This script will set up your machine using Menno's Dotfiles repository.\n" "green"
log_error "Please ensure you have a backup of your data before proceeding." println "Please ensure you have a backup of your data before proceeding." "red"
log_error "This script will modify system files and may require sudo permissions.\n" println "This script will modify system files and may require sudo permissions.\n" "red"
log_info "This script works best on a fresh Fedora, Ubuntu or Arch Linux installation." println "This script works best on a fresh Fedora, Ubuntu or Arch Linux installation." "green"
log_info "Setup starts in 5 seconds, to abort use Ctrl+C to exit NOW." println "Setup starts in 5 seconds, to abort use Ctrl+C to exit NOW." "green"
sleep 5 sleep 5
echo "" echo ""
log_info "Starting setup..." println "Starting setup..." "green"
} }
check_selinux() { check_selinux() {
# Check if distro has SELinux at all: # Check if distro has SELinux at all:
if [ ! -d /etc/selinux ]; then if [ ! -d /etc/selinux ]; then
log_success "SELinux not found. Skipping..." println "SELinux not found. Skipping..." "green"
return 0 return 0
fi fi
# Check if getenforce exists, if not it means we don't have SELinux # Check if getenforce exists, if not it means we don't have SELinux
if ! command -v getenforce >/dev/null 2>&1; then if ! command -v getenforce >/dev/null 2>&1; then
log_success "SELinux not found. Skipping..." println "SELinux not found. Skipping..." "green"
return 0 return 0
fi fi
# Check if getenforce is returning Enforcing # Check if getenforce is returning Enforcing
if [ "$(getenforce)" = "Enforcing" ]; then if [ "$(getenforce)" = "Enforcing" ]; then
log_warning "SELinux is enabled. Adjusting SELinux to permissive mode..." println "SELinux is enabled. Adjusting SELinux to permissive mode..." "yellow"
sudo setenforce Permissive || die "Failed to disable SELinux" sudo setenforce Permissive || die "Failed to disable SELinux"
sudo tee /etc/selinux/config << EOF > /dev/null || die "Failed to write to /etc/selinux/config" sudo tee /etc/selinux/config << EOF > /dev/null || die "Failed to write to /etc/selinux/config"
SELINUX=permissive SELINUX=permissive
SELINUXTYPE=targeted SELINUXTYPE=targeted
EOF EOF
log_success "SELinux disabled successfully." println "SELinux disabled successfully." "green"
fi fi
} }
@ -353,12 +336,12 @@ attempt_package_install() {
elif command -v pacman >/dev/null; then elif command -v pacman >/dev/null; then
package_manager="pacman" package_manager="pacman"
else else
log_error "No supported package manager was found, aborting setup..." println "No supported package manager was found, aborting setup..." "red"
exit 1 exit 1
fi fi
if ! command -v "$package" >/dev/null 2>&1; then if ! command -v "$package" >/dev/null 2>&1; then
log_info "Installing $package using $package_manager..." println "Installing $package using $package_manager..." "green"
if [ "$package_manager" = "dnf" ]; then if [ "$package_manager" = "dnf" ]; then
sudo dnf install "$package" -y || die "Failed to install $package" sudo dnf install "$package" -y || die "Failed to install $package"
elif [ "$package_manager" = "apt" ]; then elif [ "$package_manager" = "apt" ]; then
@ -395,16 +378,16 @@ check_compatibility() {
case "$distro" in case "$distro" in
Fedora*) Fedora*)
log_success "Detected Fedora. Proceeding with setup..." println "Detected Fedora. Proceeding with setup..." "green"
check_command_availibility "dnf" check_command_availibility "dnf"
;; ;;
Ubuntu) Ubuntu)
log_success "Detected Ubuntu. Proceeding with setup..." println "Detected Ubuntu. Proceeding with setup..." "green"
check_command_availibility "apt" check_command_availibility "apt"
;; ;;
Arch*) Arch*)
log_warning "Detected Arch Linux. Setup has not been tested on Arch Linux." println "Detected Arch Linux. Setup has not been tested on Arch Linux." "yellow"
log_warning "Proceed at your own risk..." println "Proceed at your own risk..." "yellow"
check_command_availibility "pacman" check_command_availibility "pacman"
;; ;;
*) *)
@ -418,9 +401,9 @@ ensure_shell() {
local shell local shell
shell=$(getent passwd "$USER" | cut -d: -f7) shell=$(getent passwd "$USER" | cut -d: -f7)
if [ "$shell" != "/bin/bash" ]; then if [ "$shell" != "/bin/bash" ]; then
log_info "Setting default shell to bash..." println "Setting default shell to bash..." "green"
chsh -s /bin/bash || die "Failed to set default shell to bash" chsh -s /bin/bash || die "Failed to set default shell to bash"
log_success "Default shell set to bash." println "Default shell set to bash." "green"
fi fi
# Ensure shell is set for root user # Ensure shell is set for root user
@ -428,9 +411,9 @@ ensure_shell() {
local root_shell local root_shell
root_shell=$(getent passwd root | cut -d: -f7) root_shell=$(getent passwd root | cut -d: -f7)
if [ "$root_shell" != "/bin/bash" ]; then if [ "$root_shell" != "/bin/bash" ]; then
log_info "Setting default shell for root to bash..." println "Setting default shell for root to bash..." "green"
sudo chsh -s /bin/bash root || die "Failed to set default shell for root to bash" sudo chsh -s /bin/bash root || die "Failed to set default shell for root to bash"
log_success "Default shell for root set to bash." println "Default shell for root set to bash." "green"
fi fi
fi fi
} }
@ -440,7 +423,7 @@ main() {
# Check if setup has already been run # Check if setup has already been run
if [ -f "$SETUP_MARKER" ]; then if [ -f "$SETUP_MARKER" ]; then
log_info "Setup has already been run, exiting..." println "Setup has already been run, exiting..." "green"
exit 0 exit 0
fi fi
@ -461,12 +444,12 @@ main() {
# Clone dotfiles if needed # Clone dotfiles if needed
if [ ! -d "$DOTFILES_PATH" ]; then if [ ! -d "$DOTFILES_PATH" ]; then
log_info "Cloning dotfiles repo..." println "Cloning dotfiles repo..." "green"
git clone "$GIT_REPO" "$DOTFILES_PATH" || die "Failed to clone dotfiles repository" git clone "$GIT_REPO" "$DOTFILES_PATH" || die "Failed to clone dotfiles repository"
fi fi
if [ "$continue_flag" = "--continue" ]; then if [ "$continue_flag" = "--continue" ]; then
log_info "Continuing setup..." println "Continuing setup..." "green"
else else
warning_prompt warning_prompt
prepare_hostname "$hostname" prepare_hostname "$hostname"
@ -497,9 +480,9 @@ EOF
touch "$SETUP_MARKER" || die "Failed to create setup marker" touch "$SETUP_MARKER" || die "Failed to create setup marker"
# Final success message # Final success message
log_success "\nSetup complete. Please logout / restart to continue with 'dotf update'.\n" println "\nSetup complete. Please logout / restart to continue with 'dotf update'.\n" "green"
log_error "\n!!! Please logout / restart to continue !!!" println "\n!!! Please logout / restart to continue !!!" "red"
log_error "~~~ Proceed by running 'dotf update' ~~~\n" println "~~~ Proceed by running 'dotf update' ~~~\n" "red"
} }
main "$@" main "$@"