This commit is contained in:
Aaron D. Lee
2025-12-22 00:04:07 -05:00
parent e1fe7bdffd
commit f530c7466e
5 changed files with 850 additions and 1653 deletions

View File

@@ -1,461 +1,314 @@
#!/usr/bin/env bash
# ============================================================================
# Dotfiles Doctor - Diagnostic Tool
# ============================================================================
# Checks the health of your dotfiles installation
#
# Usage:
# dotfiles-doctor.sh # Run all checks
# dotfiles-doctor.sh --fix # Attempt to fix issues
# dotfiles-doctor.sh --quiet # Only show errors
# Dotfiles Health Check (Arch/CachyOS)
# ============================================================================
set -e
# ============================================================================
# Options
# ============================================================================
readonly DOTFILES_HOME="${DOTFILES_HOME:-.}"
readonly DOTFILES_VERSION="3.0.0"
FIX_MODE=false
QUIET_MODE=false
# Color codes
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly CYAN='\033[0;36m'
readonly NC='\033[0m'
for arg in "$@"; do
case "$arg" in
--fix)
FIX_MODE=true
;;
--quiet|-q)
QUIET_MODE=true
;;
--help|-h)
echo "Usage: dotfiles-doctor.sh [OPTIONS]"
echo
echo "Options:"
echo " --fix Attempt to automatically fix issues"
echo " --quiet Only show errors and warnings"
echo " --help Show this help message"
echo
echo "Aliases:"
echo " dfd, doctor Run diagnostics"
echo " dffix Run with --fix"
echo
exit 0
;;
esac
done
# Track results
TOTAL_CHECKS=0
PASSED_CHECKS=0
FAILED_CHECKS=0
WARNING_CHECKS=0
# ============================================================================
# Load Configuration
# ============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOTFILES_CONF="${SCRIPT_DIR}/../dotfiles.conf"
[[ -f "$DOTFILES_CONF" ]] || DOTFILES_CONF="${SCRIPT_DIR}/dotfiles.conf"
[[ -f "$DOTFILES_CONF" ]] || DOTFILES_CONF="$HOME/.dotfiles/dotfiles.conf"
if [[ -f "$DOTFILES_CONF" ]]; then
source "$DOTFILES_CONF"
else
DOTFILES_DIR="$HOME/.dotfiles"
DOTFILES_VERSION="unknown"
ZSH_THEME_NAME="adlee"
fi
# ============================================================================
# Colors
# ============================================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# ============================================================================
# Counters
# ============================================================================
PASS_COUNT=0
WARN_COUNT=0
FAIL_COUNT=0
# ============================================================================
# Helper Functions
# Print MOTD-style header
# ============================================================================
print_header() {
if [[ "$QUIET_MODE" != true ]]; then
echo -e "\n${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}${NC} Dotfiles Doctor ${CYAN}v${DOTFILES_VERSION}${NC} ${BLUE}${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}\n"
fi
local user="${USER:-root}"
local hostname="${HOSTNAME:-localhost}"
local timestamp=$(date '+%a %b %d %H:%M')
echo ""
printf "${CYAN}+ ${NC}%-20s %30s %25s\n" "$user@$hostname" "dotfiles-doctor" "$timestamp"
echo ""
}
# ============================================================================
# Health check functions
# ============================================================================
print_section() {
if [[ "$QUIET_MODE" != true ]]; then
echo -e "\n${BLUE}━━━ $1 ━━━${NC}"
fi
echo -e "\n${BLUE}${NC} $1"
}
pass() {
((PASS_COUNT++))
if [[ "$QUIET_MODE" != true ]]; then
echo -e "${GREEN}${NC} $1"
fi
check_pass() {
((PASSED_CHECKS++))
((TOTAL_CHECKS++))
echo -e " ${GREEN}${NC} $1"
}
warn() {
((WARN_COUNT++))
echo -e "${YELLOW}${NC} $1"
check_fail() {
((FAILED_CHECKS++))
((TOTAL_CHECKS++))
echo -e " ${RED}${NC} $1"
}
fail() {
((FAIL_COUNT++))
echo -e "${RED}${NC} $1"
}
info() {
if [[ "$QUIET_MODE" != true ]]; then
echo -e "${CYAN}${NC} $1"
fi
check_warn() {
((WARNING_CHECKS++))
((TOTAL_CHECKS++))
echo -e " ${YELLOW}${NC} $1"
}
# ============================================================================
# Check Functions
# Health checks
# ============================================================================
check_dotfiles_dir() {
print_section "Dotfiles Directory"
if [[ -d "$DOTFILES_DIR" ]]; then
pass "Dotfiles directory exists: $DOTFILES_DIR"
# Check if it's a git repo
if [[ -d "$DOTFILES_DIR/.git" ]]; then
pass "Is a git repository"
# Check for uncommitted changes
cd "$DOTFILES_DIR"
if git diff --quiet 2>/dev/null; then
pass "No uncommitted changes"
else
warn "Uncommitted changes in dotfiles"
fi
# Check if up to date with remote
git fetch origin --quiet 2>/dev/null || true
local local_hash=$(git rev-parse HEAD 2>/dev/null)
local remote_hash=$(git rev-parse origin/${DOTFILES_BRANCH:-main} 2>/dev/null || echo "")
if [[ -n "$remote_hash" && "$local_hash" == "$remote_hash" ]]; then
pass "Up to date with remote"
elif [[ -n "$remote_hash" ]]; then
warn "Behind remote (run: cd ~/.dotfiles && git pull)"
fi
cd - > /dev/null
check_os() {
print_section "Operating System"
if [[ "$OSTYPE" == "linux-gnu" ]]; then
if grep -qi "arch\|cachyos" /etc/os-release 2>/dev/null; then
check_pass "Running on Arch/CachyOS"
else
warn "Not a git repository"
fi
# Check config file
if [[ -f "$DOTFILES_DIR/dotfiles.conf" ]]; then
pass "Config file exists: dotfiles.conf"
else
fail "Config file missing: dotfiles.conf"
check_fail "Not running on Arch/CachyOS"
fi
else
fail "Dotfiles directory not found: $DOTFILES_DIR"
check_fail "Not running on Linux"
fi
}
check_shell() {
print_section "Shell Configuration"
if [[ -f "$HOME/.zshrc" ]]; then
check_pass "Zsh configuration exists"
else
check_fail "Zsh configuration missing"
fi
if [[ "$SHELL" == *"zsh"* ]]; then
check_pass "Zsh is default shell"
else
check_warn "Zsh is not default shell (current: $SHELL)"
fi
}
check_symlinks() {
print_section "Symlinks"
local symlinks=(
"$HOME/.zshrc:$DOTFILES_DIR/zsh/.zshrc"
"$HOME/.gitconfig:$DOTFILES_DIR/git/.gitconfig"
"$HOME/.vimrc:$DOTFILES_DIR/vim/.vimrc"
"$HOME/.tmux.conf:$DOTFILES_DIR/tmux/.tmux.conf"
"$HOME/.oh-my-zsh/themes/${ZSH_THEME_NAME}.zsh-theme:$DOTFILES_DIR/zsh/themes/${ZSH_THEME_NAME}.zsh-theme"
)
local valid_count=0
local total_count=0
for entry in "${symlinks[@]}"; do
local link="${entry%%:*}"
local target="${entry##*:}"
local name=$(basename "$link")
((total_count++))
if [[ -L "$link" ]]; then
local actual_target=$(readlink -f "$link" 2>/dev/null)
local expected_target=$(readlink -f "$target" 2>/dev/null)
if [[ "$actual_target" == "$expected_target" ]]; then
pass "Symlink valid: $name"
((valid_count++))
local symlink_count=0
local broken_count=0
for symlink in ~/.zshrc ~/.gitconfig ~/.vimrc ~/.tmux.conf; do
if [[ -L "$symlink" ]]; then
((symlink_count++))
if [[ -e "$symlink" ]]; then
check_pass "$(basename $symlink)$(readlink $symlink)"
else
warn "Symlink points elsewhere: $name"
info " Expected: $target"
info " Actual: $actual_target"
((broken_count++))
check_fail "$(basename $symlink) is broken"
fi
elif [[ -f "$link" ]]; then
warn "Regular file (not symlink): $name"
if [[ "$FIX_MODE" == true ]]; then
if [[ -f "$target" ]]; then
mv "$link" "$link.backup"
ln -sf "$target" "$link"
pass "Fixed: $name (backup saved)"
((valid_count++))
fi
fi
elif [[ -f "$target" ]]; then
fail "Symlink missing: $name"
if [[ "$FIX_MODE" == true ]]; then
ln -sf "$target" "$link"
pass "Fixed: Created symlink for $name"
((valid_count++))
fi
else
info "Source not present: $name (optional)"
fi
done
# Check espanso symlink
if [[ -L "$HOME/.config/espanso" ]]; then
pass "Symlink valid: espanso config"
elif [[ -d "$HOME/.config/espanso" ]]; then
warn "Espanso config is directory (not symlink)"
elif [[ -d "$DOTFILES_DIR/espanso" ]]; then
fail "Espanso symlink missing"
fi
info "Symlinks: $valid_count/$total_count valid"
}
check_shell() {
print_section "Shell"
# Check current shell
if [[ "$SHELL" == *"zsh"* ]]; then
pass "Default shell is zsh"
else
warn "Default shell is not zsh: $SHELL"
info " Change with: chsh -s \$(which zsh)"
fi
# Check oh-my-zsh
if [[ -d "$HOME/.oh-my-zsh" ]]; then
pass "oh-my-zsh installed"
else
fail "oh-my-zsh not installed"
fi
# Check theme
if [[ -f "$HOME/.oh-my-zsh/themes/${ZSH_THEME_NAME}.zsh-theme" ]]; then
pass "Theme installed: ${ZSH_THEME_NAME}"
else
fail "Theme missing: ${ZSH_THEME_NAME}"
fi
# Check ZSH_THEME in .zshrc
if grep -q "ZSH_THEME=\"${ZSH_THEME_NAME}\"" "$HOME/.zshrc" 2>/dev/null; then
pass "Theme configured in .zshrc"
else
warn "Theme may not be configured in .zshrc"
if [[ $symlink_count -eq 0 ]]; then
check_warn "No symlinks found (may not be installed yet)"
fi
}
check_zsh_plugins() {
print_section "Zsh Plugins"
local custom_dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins"
# zsh-autosuggestions
if [[ -d "$custom_dir/zsh-autosuggestions" ]]; then
pass "Plugin installed: zsh-autosuggestions"
check_vim() {
print_section "Editor Configuration"
if command -v vim &> /dev/null; then
local vim_version=$(vim --version | head -1)
check_pass "Vim installed: $vim_version"
else
fail "Plugin missing: zsh-autosuggestions"
if [[ "$FIX_MODE" == true ]]; then
git clone --depth 1 https://github.com/zsh-users/zsh-autosuggestions "$custom_dir/zsh-autosuggestions"
pass "Fixed: Installed zsh-autosuggestions"
else
info " Install: git clone https://github.com/zsh-users/zsh-autosuggestions $custom_dir/zsh-autosuggestions"
fi
check_fail "Vim not installed"
fi
# zsh-syntax-highlighting
if [[ -d "$custom_dir/zsh-syntax-highlighting" ]]; then
pass "Plugin installed: zsh-syntax-highlighting"
if command -v nvim &> /dev/null; then
local nvim_version=$(nvim --version | head -1)
check_pass "Neovim installed: $nvim_version"
else
fail "Plugin missing: zsh-syntax-highlighting"
if [[ "$FIX_MODE" == true ]]; then
git clone --depth 1 https://github.com/zsh-users/zsh-syntax-highlighting "$custom_dir/zsh-syntax-highlighting"
pass "Fixed: Installed zsh-syntax-highlighting"
else
info " Install: git clone https://github.com/zsh-users/zsh-syntax-highlighting $custom_dir/zsh-syntax-highlighting"
fi
check_warn "Neovim not installed (optional)"
fi
}
check_git() {
print_section "Git Configuration"
# Check git installed
if command -v git &>/dev/null; then
pass "git installed: $(git --version | cut -d' ' -f3)"
else
fail "git not installed"
return
fi
# Check user.name
local git_name=$(git config --global user.name 2>/dev/null)
if [[ -n "$git_name" ]]; then
pass "Git user.name: $git_name"
else
fail "Git user.name not configured"
info " Set with: git config --global user.name \"Your Name\""
fi
# Check user.email
local git_email=$(git config --global user.email 2>/dev/null)
if [[ -n "$git_email" ]]; then
pass "Git user.email: $git_email"
else
fail "Git user.email not configured"
info " Set with: git config --global user.email \"you@example.com\""
fi
# Check credential helper
local cred_helper=$(git config --global credential.helper 2>/dev/null)
if [[ -n "$cred_helper" ]]; then
pass "Git credential helper: $cred_helper"
else
warn "Git credential helper not configured"
fi
}
check_espanso() {
print_section "Espanso"
if command -v espanso &>/dev/null; then
pass "espanso installed: $(espanso --version 2>/dev/null | head -1)"
# Check if running
if espanso status 2>/dev/null | grep -q "running"; then
pass "espanso service running"
if command -v git &> /dev/null; then
check_pass "Git installed"
if git config --global user.name &> /dev/null; then
local git_user=$(git config --global user.name)
check_pass "Git user configured: $git_user"
else
warn "espanso service not running"
info " Start with: espanso service start"
check_fail "Git user not configured"
fi
# Check config
if [[ -f "$HOME/.config/espanso/match/base.yml" ]]; then
pass "espanso config present"
if git config --global user.email &> /dev/null; then
local git_email=$(git config --global user.email)
check_pass "Git email configured: $git_email"
else
warn "espanso base.yml not found"
check_fail "Git email not configured"
fi
else
info "espanso not installed (optional)"
check_fail "Git not installed"
fi
}
check_optional_tools() {
print_section "Optional Tools"
# fzf
if command -v fzf &>/dev/null; then
pass "fzf installed"
if command -v fzf &> /dev/null; then
check_pass "fzf installed (fuzzy finder)"
else
info "fzf not installed (optional)"
check_warn "fzf not installed (command palette requires this)"
fi
# bat/batcat
if command -v bat &>/dev/null || command -v batcat &>/dev/null; then
pass "bat installed"
if command -v lastpass-cli &> /dev/null || command -v lpass &> /dev/null; then
check_pass "LastPass CLI installed"
else
info "bat not installed (optional)"
check_warn "LastPass CLI not installed (password manager)"
fi
# eza
if command -v eza &>/dev/null; then
pass "eza installed"
if command -v tmux &> /dev/null; then
check_pass "Tmux installed"
else
info "eza not installed (optional)"
check_warn "Tmux not installed (workspaces require this)"
fi
# fd
if command -v fd &>/dev/null; then
pass "fd installed"
if command -v age &> /dev/null || command -v gpg &> /dev/null; then
check_pass "Encryption tool available (age or gpg)"
else
info "fd not installed (optional)"
check_warn "No encryption tool (vault requires age or gpg)"
fi
if command -v bat &> /dev/null; then
check_pass "bat installed (syntax highlighting)"
else
check_warn "bat not installed (optional enhancement)"
fi
if command -v eza &> /dev/null; then
check_pass "eza installed (ls replacement)"
else
check_warn "eza not installed (optional enhancement)"
fi
}
check_bin_scripts() {
print_section "Bin Scripts"
check_pacman() {
print_section "Package Manager"
if command -v pacman &> /dev/null; then
check_pass "Pacman available"
else
check_fail "Pacman not found (this is Arch/CachyOS only)"
fi
}
local bin_dir="$HOME/.local/bin"
if [[ -d "$bin_dir" ]]; then
local script_count=0
local valid_count=0
for script in "$DOTFILES_DIR/bin"/*; do
if [[ -f "$script" ]]; then
((script_count++))
local name=$(basename "$script")
local link="$bin_dir/$name"
if [[ -L "$link" ]]; then
((valid_count++))
elif [[ -f "$link" ]]; then
warn "Script is regular file: $name"
else
fail "Script not linked: $name"
fi
fi
done
if [[ $script_count -gt 0 ]]; then
pass "Bin scripts: $valid_count/$script_count linked"
fi
# Check PATH
if [[ ":$PATH:" == *":$bin_dir:"* ]]; then
pass "$bin_dir is in PATH"
check_permissions() {
print_section "File Permissions"
if [[ -f "$DOTFILES_HOME/install.sh" ]]; then
if [[ -x "$DOTFILES_HOME/install.sh" ]]; then
check_pass "install.sh is executable"
else
warn "$bin_dir not in PATH"
info " Add to .zshrc: export PATH=\"\$HOME/.local/bin:\$PATH\""
check_fail "install.sh is not executable"
fi
fi
if [[ -d "$DOTFILES_HOME/bin" ]]; then
local non_exec=$(find "$DOTFILES_HOME/bin" -type f ! -perm /u+x 2>/dev/null | wc -l)
if [[ $non_exec -eq 0 ]]; then
check_pass "All scripts in bin/ are executable"
else
check_fail "$non_exec scripts in bin/ are not executable"
fi
else
warn "~/.local/bin directory doesn't exist"
fi
}
check_zsh_plugins() {
print_section "Zsh Plugins"
if [[ -d "$HOME/.oh-my-zsh" ]]; then
check_pass "Oh My Zsh installed"
if [[ -d "$HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions" ]]; then
check_pass "zsh-autosuggestions installed"
else
check_warn "zsh-autosuggestions not installed"
fi
if [[ -d "$HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting" ]]; then
check_pass "zsh-syntax-highlighting installed"
else
check_warn "zsh-syntax-highlighting not installed"
fi
if [[ -f "$HOME/.oh-my-zsh/themes/adlee.zsh-theme" ]]; then
check_pass "adlee theme installed"
else
check_warn "adlee theme not installed"
fi
else
check_warn "Oh My Zsh not installed"
fi
}
check_dotfiles_dir() {
print_section "Dotfiles Directory"
if [[ -d "$DOTFILES_HOME" ]]; then
check_pass "Dotfiles directory found: $DOTFILES_HOME"
else
check_fail "Dotfiles directory not found: $DOTFILES_HOME"
return
fi
if [[ -f "$DOTFILES_HOME/dotfiles.conf" ]]; then
check_pass "Configuration file exists"
else
check_warn "Configuration file missing"
fi
if [[ -d "$DOTFILES_HOME/.git" ]]; then
check_pass "Git repository initialized"
else
check_warn "Not a git repository"
fi
}
# ============================================================================
# Print summary
# ============================================================================
print_summary() {
echo
echo -e "${BLUE}━━━ Summary ━━━${NC}"
echo
echo -e " ${GREEN}Passed:${NC} $PASS_COUNT"
echo -e " ${YELLOW}Warnings:${NC} $WARN_COUNT"
echo -e " ${RED}Failed:${NC} $FAIL_COUNT"
echo
if [[ $FAIL_COUNT -eq 0 && $WARN_COUNT -eq 0 ]]; then
echo -e "${GREEN}✓ All checks passed! Your dotfiles are healthy.${NC}"
elif [[ $FAIL_COUNT -eq 0 ]]; then
echo -e "${YELLOW}⚠ Some warnings, but no critical issues.${NC}"
echo ""
printf "${CYAN}─%.0s${NC}" {1..70}; echo ""
if [[ $FAILED_CHECKS -eq 0 ]]; then
echo -e "${GREEN}${NC} All checks passed ($PASSED_CHECKS/$TOTAL_CHECKS)"
else
echo -e "${RED} Some issues found.${NC}"
if [[ "$FIX_MODE" != true ]]; then
echo -e " Run ${CYAN}dffix${NC} or ${CYAN}dotfiles-doctor.sh --fix${NC} to attempt automatic fixes."
echo -e "${RED}${NC} Some checks failed"
echo -e " ${GREEN}Passed:${NC} $PASSED_CHECKS"
echo -e " ${RED}Failed:${NC} $FAILED_CHECKS"
if [[ $WARNING_CHECKS -gt 0 ]]; then
echo -e " ${YELLOW}Warnings:${NC} $WARNING_CHECKS"
fi
fi
echo
echo ""
if [[ $FAILED_CHECKS -gt 0 ]]; then
echo -e "${YELLOW}💡 Tip:${NC} Run 'dotfiles-doctor.sh --fix' to attempt automatic fixes"
echo ""
return 1
fi
}
# ============================================================================
@@ -464,20 +317,19 @@ print_summary() {
main() {
print_header
check_os
check_pacman
check_shell
check_vim
check_git
check_dotfiles_dir
check_symlinks
check_shell
check_zsh_plugins
check_git
check_espanso
check_optional_tools
check_bin_scripts
check_permissions
print_summary
# Exit with error code if there were failures
[[ $FAIL_COUNT -eq 0 ]]
}
main "$@"