Ver código fonte

Sync: 2026-08-01 12:18:44

Gabriel Capella 2 dias atrás
pai
commit
b4f6b32584
1 arquivos alterados com 28 adições e 5 exclusões
  1. 28 5
      bootstrap

+ 28 - 5
bootstrap

@@ -28,15 +28,32 @@ if [[ $EUID -eq 0 ]]; then
     exit 1
 fi
 
+# Acquire sudo up front. Without this the first privileged command fails on a
+# non-interactive shell and, because install_file runs as an `if` condition
+# (where bash disables set -e), the failure would be swallowed and the script
+# would report writes that never happened.
+if ! sudo -v; then
+    log_error "Could not obtain sudo credentials."
+    log_error "Run this from a real terminal -- sudo needs a tty to prompt."
+    exit 1
+fi
+
 # Write a file only if its content differs, so re-runs stay quiet and we avoid
-# needlessly restarting units.
+# needlessly restarting units. Aborts on failure rather than reporting a
+# phantom success -- see the set -e note above.
 install_file() {
     local dest="$1" content="$2"
     if [[ -f "$dest" ]] && [[ "$(cat "$dest")" == "$content" ]]; then
         return 1
     fi
-    sudo mkdir -p "$(dirname "$dest")"
-    printf '%s\n' "$content" | sudo tee "$dest" >/dev/null
+    if ! sudo mkdir -p "$(dirname "$dest")"; then
+        log_error "Failed to create $(dirname "$dest")"
+        exit 1
+    fi
+    if ! printf '%s\n' "$content" | sudo tee "$dest" >/dev/null; then
+        log_error "Failed to write $dest"
+        exit 1
+    fi
     log_info "Wrote $dest"
     return 0
 }
@@ -47,7 +64,10 @@ enable_unit() {
         return 0
     fi
     log_info "Enabling $unit"
-    sudo systemctl enable "$unit"
+    if ! sudo systemctl enable "$unit"; then
+        log_error "Failed to enable $unit"
+        exit 1
+    fi
 }
 
 enable_user_unit() {
@@ -56,7 +76,10 @@ enable_user_unit() {
         return 0
     fi
     log_info "Enabling (user) $unit"
-    systemctl --user enable "$unit"
+    if ! systemctl --user enable "$unit"; then
+        log_error "Failed to enable user unit $unit"
+        exit 1
+    fi
 }
 
 log_info "Bootstrapping system state for $hostname_id"