aurora1658e95
add better error handling for install script, add --yes flag.
+126 −56
1 file changed
- M install.sh +126 −56
| @@ -1,4 +1,17 @@ | |||
| 1 | − | #!/bin/env bash | |
| 1 | + | #!/usr/bin/env bash | |
| 2 | + | ||
| 3 | + | trap 'log WARN "Interrupted by user"; exit 130' INT | |
| 4 | + | ||
| 5 | + | INSTALL_STATUS="none" # none | partial | complete | failed | |
| 6 | + | AUTO_YES=0 | |
| 7 | + | ||
| 8 | + | for arg in "$@"; do | |
| 9 | + | case "$arg" in | |
| 10 | + | --yes|--ci|--non-interactive) | |
| 11 | + | AUTO_YES=1 | |
| 12 | + | ;; | |
| 13 | + | esac | |
| 14 | + | done | |
| 2 | 15 | ||
| 3 | 16 | log() { | |
| 4 | 17 | local level="$1" | |
| @@ -51,10 +64,10 @@ choose_packages() { | |||
| 51 | 64 | } | |
| 52 | 65 | ||
| 53 | 66 | prompt_style() { | |
| 54 | − | gum style --border rounded \ | |
| 55 | − | --border double \ | |
| 56 | − | --padding "0 1" \ | |
| 57 | − | "$1" | |
| 67 | + | gum style \ | |
| 68 | + | --border rounded \ | |
| 69 | + | --padding "0 1" \ | |
| 70 | + | "$1" | |
| 58 | 71 | } | |
| 59 | 72 | ||
| 60 | 73 | gum_warn(){ | |
| @@ -62,7 +75,22 @@ gum_warn(){ | |||
| 62 | 75 | } | |
| 63 | 76 | ||
| 64 | 77 | gum_to_array() { | |
| 65 | − | mapfile -t "$1" | |
| 78 | + | local -n arr="$1" | |
| 79 | + | mapfile -t arr | |
| 80 | + | local cleaned=() | |
| 81 | + | for item in "${arr[@]}"; do | |
| 82 | + | [[ -n "$item" ]] && cleaned+=("$item") | |
| 83 | + | done | |
| 84 | + | arr=("${cleaned[@]}") | |
| 85 | + | } | |
| 86 | + | ||
| 87 | + | confirm() { | |
| 88 | + | local msg="$1" | |
| 89 | + | if ((AUTO_YES)); then | |
| 90 | + | log INFO "[auto] $msg → yes" | |
| 91 | + | return 0 | |
| 92 | + | fi | |
| 93 | + | gum confirm --default=false "$msg" | |
| 66 | 94 | } | |
| 67 | 95 | ||
| 68 | 96 | ################# | |
| @@ -92,7 +120,7 @@ You are STRONGLY ADVISED to back up: | |||
| 92 | 120 | Proceed at your OWN RISK." | |
| 93 | 121 | ) | |
| 94 | 122 | ||
| 95 | − | gum confirm --default=false "$DISCLAIMER | |
| 123 | + | confirm "$DISCLAIMER | |
| 96 | 124 | ||
| 97 | 125 | Do you understand the risks and want to continue?" || exit 1 | |
| 98 | 126 | ||
| @@ -102,7 +130,11 @@ Do you understand the risks and want to continue?" || exit 1 | |||
| 102 | 130 | ########### | |
| 103 | 131 | source "$currentDir/packages/pkg_utils.sh" | |
| 104 | 132 | prompt_style "Important utilities (Most likely go with defaults)" | |
| 105 | − | gum_to_array UTILITY_PKGS < <(choose_packages pkg_utils) | |
| 133 | + | if ((AUTO_YES)); then | |
| 134 | + | DEV_PKGS=("${pkg_utils[@]}") | |
| 135 | + | else | |
| 136 | + | gum_to_array UTILITY_PKGS < <(choose_packages pkg_utils) | |
| 137 | + | fi | |
| 106 | 138 | ||
| 107 | 139 | ||
| 108 | 140 | ############### | |
| @@ -110,7 +142,12 @@ gum_to_array UTILITY_PKGS < <(choose_packages pkg_utils) | |||
| 110 | 142 | ############### | |
| 111 | 143 | source "$currentDir/packages/pkg_dev_tools.sh" | |
| 112 | 144 | prompt_style "Select Development Tools" | |
| 113 | − | gum_to_array DEV_PKGS < <(choose_packages pkg_dev_tools) | |
| 145 | + | if ((AUTO_YES)); then | |
| 146 | + | DEV_PKGS=("${pkg_dev_tools[@]}") | |
| 147 | + | else | |
| 148 | + | gum_to_array DEV_PKGS < <(choose_packages pkg_dev_tools) | |
| 149 | + | fi | |
| 150 | + | ||
| 114 | 151 | ||
| 115 | 152 | ||
| 116 | 153 | ####################### | |
| @@ -118,7 +155,12 @@ gum_to_array DEV_PKGS < <(choose_packages pkg_dev_tools) | |||
| 118 | 155 | ####################### | |
| 119 | 156 | source "$currentDir/packages/pkg_optional.sh" | |
| 120 | 157 | prompt_style "Select Optional Desktop Packages" | |
| 121 | − | gum_to_array OPTIONAL_PKGS < <(choose_packages pkg_optional) | |
| 158 | + | if ((AUTO_YES)); then | |
| 159 | + | OPTIONAL_PKGS=("${pkg_optional[@]}") | |
| 160 | + | else | |
| 161 | + | gum_to_array OPTIONAL_PKGS < <(choose_packages pkg_optional) | |
| 162 | + | fi | |
| 163 | + | ||
| 122 | 164 | ||
| 123 | 165 | ||
| 124 | 166 | ################# | |
| @@ -126,7 +168,12 @@ gum_to_array OPTIONAL_PKGS < <(choose_packages pkg_optional) | |||
| 126 | 168 | ################# | |
| 127 | 169 | source "$currentDir/packages/pkg_gpu.sh" | |
| 128 | 170 | prompt_style "Select GPU drivers" | |
| 129 | − | gum_to_array GPU_PKGS < <(choose_packages pkg_gpu) | |
| 171 | + | if ((AUTO_YES)); then | |
| 172 | + | GPU_PKGS=("${pkg_gpu[@]}") | |
| 173 | + | else | |
| 174 | + | gum_to_array GPU_PKGS < <(choose_packages pkg_gpu) | |
| 175 | + | fi | |
| 176 | + | ||
| 130 | 177 | ||
| 131 | 178 | ########################## | |
| 132 | 179 | ## Configure Everything ## | |
| @@ -139,9 +186,9 @@ WARNING=$(gum style \ | |||
| 139 | 186 | "⚠️ This will DELETE any conflicting files and replace them with symlinks from this repo. | |
| 140 | 187 | Make sure you have already backed up all your existing config files (~/.config)") | |
| 141 | 188 | ||
| 142 | − | if gum confirm --default=false "$WARNING | |
| 189 | + | if confirm "$WARNING | |
| 143 | 190 | ||
| 144 | − | Proceed with system configuration (stow, nvim, shell, keyd)?"; then | |
| 191 | + | Proceed with system configuration (stow, shell)?"; then | |
| 145 | 192 | ################### | |
| 146 | 193 | ## Stow dotfiles ## | |
| 147 | 194 | ################### | |
| @@ -159,12 +206,13 @@ Proceed with system configuration (stow, nvim, shell, keyd)?"; then | |||
| 159 | 206 | printf ' %s\n' $conflicts | |
| 160 | 207 | read -rp "Proceed with deleting these files? (y/N): " ok | |
| 161 | 208 | if [[ ! "$ok" =~ ^[Yy]$ ]]; then | |
| 162 | − | echo "Aborted." | |
| 209 | + | log WARN "Aborted." | |
| 210 | + | exit 1 | |
| 163 | 211 | fi | |
| 164 | 212 | ||
| 165 | 213 | # Remove conflicts relative to $HOME | |
| 166 | 214 | for path in $conflicts; do | |
| 167 | − | rm -rf "$HOME/$path" && log INFO "Removed $HOME/$path" | |
| 215 | + | [[ -n "$path" && "$path" != "/" ]] && rm -rf "$HOME/$path" && log INFO "Deleted $path" | |
| 168 | 216 | done | |
| 169 | 217 | ||
| 170 | 218 | log INFO "Running stow..." | |
| @@ -173,22 +221,11 @@ Proceed with system configuration (stow, nvim, shell, keyd)?"; then | |||
| 173 | 221 | || log ERROR "Stow failed" | |
| 174 | 222 | fi | |
| 175 | 223 | ||
| 176 | − | ########################### | |
| 177 | − | ## Setup neovim dotfiles ## | |
| 178 | − | ########################### | |
| 179 | − | log INFO "Setting up neovim configuration" | |
| 180 | − | log INFO "Taking backup of neovim config (if already exists)" | |
| 181 | − | mv ~/.config/nvim{,.bak} | |
| 182 | − | mv ~/.local/share/nvim{,.bak} | |
| 183 | − | mv ~/.local/state/nvim{,.bak} | |
| 184 | − | mv ~/.cache/nvim{,.bak} | |
| 185 | − | git clone --depth=1 https://github.com/krolyxon/nvim.git ~/.config/nvim | |
| 186 | − | ||
| 187 | 224 | ################################# | |
| 188 | 225 | ## Change default shell to zsh ## | |
| 189 | 226 | ################################# | |
| 190 | 227 | log INFO "Changing the default shell to ZSH" | |
| 191 | − | if [[ "$SHELL" != "$(which zsh)" ]]; then | |
| 228 | + | if [[ "$SHELL" != "$(command -v zsh)" ]]; then | |
| 192 | 229 | chsh -s $(which zsh) \ | |
| 193 | 230 | && log SUCCESS "Default shell successfully set to zsh" \ | |
| 194 | 231 | || log ERROR "Default shell could not be set to zsh" | |
| @@ -205,26 +242,23 @@ fi | |||
| 205 | 242 | ################## | |
| 206 | 243 | source "$currentDir/packages/pkg_aur.sh" | |
| 207 | 244 | prompt_style "Select AUR packages" | |
| 208 | − | gum_to_array AUR_PKGS < <(choose_packages pkg_aur) | |
| 209 | − | if [[ ${#AUR_PKGS[@]} -eq 0 ]]; then | |
| 210 | − | log WARN "No AUR packages selected" | |
| 245 | + | ||
| 246 | + | if ((AUTO_YES)); then | |
| 247 | + | AUR_PKGS=("${pkg_aur[@]}") | |
| 211 | 248 | else | |
| 212 | − | if ! command -v paru >/dev/null 2>&1; then | |
| 249 | + | gum_to_array AUR_PKGS < <(choose_packages pkg_aur) | |
| 250 | + | fi | |
| 251 | + | ||
| 252 | + | if ((${#AUR_PKGS[@]})); then | |
| 253 | + | if ! command -v paru >/dev/null 2>&1; then | |
| 213 | 254 | log INFO "Installing Paru (AUR package manager)" | |
| 214 | 255 | git clone https://aur.archlinux.org/paru.git | |
| 215 | − | cd paru | |
| 216 | − | makepkg -sri | |
| 217 | − | cd .. | |
| 256 | + | (cd paru && makepkg -sri) | |
| 218 | 257 | rm -rf paru | |
| 219 | − | else | |
| 220 | − | log INFO "Paru already in PATH, Skipping...." | |
| 221 | − | fi | |
| 222 | − | ||
| 223 | − | if [[ ${#AUR_PKGS[@]} -eq 0 ]]; then | |
| 224 | − | log WARN "No AUR packages selected, Skipping...." | |
| 225 | − | else | |
| 226 | − | paru -S --needed "${AUR_PKGS[@]}" | |
| 227 | 258 | fi | |
| 259 | + | paru -S --needed "${AUR_PKGS[@]}" | |
| 260 | + | else | |
| 261 | + | log WARN "No AUR packages selected" | |
| 228 | 262 | fi | |
| 229 | 263 | ||
| 230 | 264 | ||
| @@ -232,7 +266,7 @@ fi | |||
| 232 | 266 | ## Setup Keyd ## | |
| 233 | 267 | ############################ | |
| 234 | 268 | if command -v keyd >/dev/null 2>&1; then | |
| 235 | − | if gum confirm --default=false "Configure and enable keyd? "; then | |
| 269 | + | if confirm "Configure and enable keyd? "; then | |
| 236 | 270 | log INFO "Copying keyd configuration to /etc/keyd/default.conf" | |
| 237 | 271 | sudo cp "$currentDir/system/etc/keyd/default.conf" /etc/keyd/ | |
| 238 | 272 | sudo systemctl enable --now keyd.service \ | |
| @@ -253,23 +287,59 @@ ALL_PKGS=( | |||
| 253 | 287 | "${pkg_desktop[@]}" | |
| 254 | 288 | ) | |
| 255 | 289 | ||
| 256 | − | if [[ ${#ALL_PKGS[@]} -eq 0 ]]; then | |
| 257 | − | log WARN "No packages selected." | |
| 258 | − | else | |
| 259 | − | if gum confirm --default=false "Install all the selected packages?"; then | |
| 290 | + | if ((${#ALL_PKGS[@]})); then | |
| 291 | + | if confirm "Install all the selected packages?"; then | |
| 260 | 292 | log INFO "Installing selected packages..." | |
| 261 | − | sudo pacman -Sy --needed "${ALL_PKGS[@]}" | |
| 293 | + | if sudo pacman -Syu --needed "${ALL_PKGS[@]}"; then | |
| 294 | + | INSTALL_STATUS="complete" | |
| 295 | + | else | |
| 296 | + | INSTALL_STATUS="failed" | |
| 297 | + | fi | |
| 298 | + | else | |
| 299 | + | log WARN "Package installation skipped by user" | |
| 300 | + | INSTALL_STATUS="partial" | |
| 262 | 301 | fi | |
| 302 | + | else | |
| 303 | + | log WARN "No packages selected" | |
| 304 | + | INSTALL_STATUS="partial" | |
| 263 | 305 | fi | |
| 264 | 306 | ||
| 265 | 307 | ||
| 266 | − | gum style \ | |
| 267 | − | --border rounded \ | |
| 268 | − | --padding "1 2" \ | |
| 269 | − | --bold \ | |
| 270 | − | --foreground 42 \ | |
| 271 | − | "🎉 Installation Complete! | |
| 272 | − | ||
| 273 | − | Your system has been successfully configured. | |
| 308 | + | case "$INSTALL_STATUS" in | |
| 309 | + | complete) | |
| 310 | + | gum style \ | |
| 311 | + | --border rounded \ | |
| 312 | + | --border-foreground 42 \ | |
| 313 | + | --padding "1 2" \ | |
| 314 | + | --bold \ | |
| 315 | + | "✔ Installation Complete! | |
| 274 | 316 | ||
| 317 | + | All selected packages were installed successfully. | |
| 275 | 318 | You may now reboot the system for all the changes to apply." | |
| 319 | + | ;; | |
| 320 | + | partial) | |
| 321 | + | gum style \ | |
| 322 | + | --border rounded \ | |
| 323 | + | --border-foreground 220 \ | |
| 324 | + | --padding "1 2" \ | |
| 325 | + | --bold \ | |
| 326 | + | "⚠ Setup Finished (Partial) | |
| 327 | + | ||
| 328 | + | Some steps were skipped by user choice. | |
| 329 | + | Your system was NOT fully configured." | |
| 330 | + | ;; | |
| 331 | + | failed) | |
| 332 | + | gum style \ | |
| 333 | + | --border rounded \ | |
| 334 | + | --border-foreground 196 \ | |
| 335 | + | --padding "1 2" \ | |
| 336 | + | --bold \ | |
| 337 | + | "❌ Installation Failed | |
| 338 | + | ||
| 339 | + | One or more steps did not complete successfully. | |
| 340 | + | Check the logs above." | |
| 341 | + | ;; | |
| 342 | + | *) | |
| 343 | + | gum style --bold "Finished." | |
| 344 | + | ;; | |
| 345 | + | esac | |