Something went wrong. Try again.
Dotfiles for my macos setup
Something went wrong. Try again.
10.0 kB ยท 311 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312#!/bin/bash# USB & SD Card Formatting Tool for macOS# Requires: gum (brew install gum)set -e# Check if gum is installedif ! command -v gum &>/dev/null; then echo "Error: gum is not installed. Install it with: brew install gum" exit 1fi# Headergum style \ --foreground 212 --border-foreground 212 --border double \ --align center --width 50 --margin "1 2" --padding "1 2" \ '๐พ USB & SD Card Formatter' 'for macOS'# Get list of USB drives and SD cards (filter by protocol)get_usb_drives() { diskutil list | grep -E "^/dev/disk" | awk '{print $1}' | while read -r disk; do protocol=$(diskutil info "$disk" 2>/dev/null | grep "Protocol:" | sed 's/.*: *//') if [ "$protocol" = "USB" ] || [ "$protocol" = "Secure Digital" ]; then echo "$disk" fi done}# Get drive infoget_drive_info() { local disk=$1 diskutil info "$disk" | grep -E "Device / Media Name:|Volume Name:|Disk Size:|Volume Free Space:|File System Personality:" | sed 's/^[[:space:]]*//'}# Check if drive/partition is healthycheck_drive_health() { local disk=$1 local disk_base=$(basename "$disk") local issues=() # Check each partition for partition in $(diskutil list "$disk" | grep -oE "${disk_base}s[0-9]+"); do local mount_status=$(diskutil info "/dev/$partition" 2>/dev/null | grep "Mounted:" | sed 's/.*: *//') local fs_type=$(diskutil info "/dev/$partition" 2>/dev/null | grep "File System Personality:" | sed 's/.*: *//') if [ "$mount_status" = "No" ] && [ -n "$fs_type" ]; then # Try to verify the filesystem verify_output=$(diskutil verifyVolume "/dev/$partition" 2>&1) if echo "$verify_output" | grep -q "exit code"; then error_msg=$(echo "$verify_output" | grep -E "Invalid|Error|failed" | head -1) issues+=("$partition: $error_msg") fi fi done if [ ${#issues[@]} -gt 0 ]; then echo "${issues[@]}" fi}# List USB drivesUSB_DRIVES=$(get_usb_drives)if [ -z "$USB_DRIVES" ]; then gum style --foreground 196 "โ No external USB drives or SD cards found!" exit 1fi# Create array of drives with info for selectionDRIVE_OPTIONS=()while IFS= read -r drive; do name=$(diskutil info "$drive" | grep "Device / Media Name:" | sed 's/.*: *//') size=$(diskutil info "$drive" | grep "Disk Size:" | sed 's/.*: *//' | cut -d'(' -f1) protocol=$(diskutil info "$drive" | grep "Protocol:" | sed 's/.*: *//') if [ "$protocol" = "Secure Digital" ]; then type_icon="๐ณ SD" else type_icon="๐ USB" fi DRIVE_OPTIONS+=("$drive - $type_icon - $name ($size)")done <<<"$USB_DRIVES"# Select a USB drive or SD cardgum style --foreground 86 "๐ Select a USB drive or SD card:"SELECTED=$(gum choose "${DRIVE_OPTIONS[@]}")if [ -z "$SELECTED" ]; then gum style --foreground 196 "โ No drive selected!" exit 1fi# Extract disk identifierDISK=$(echo "$SELECTED" | cut -d' ' -f1)# Show disk informationgum style --foreground 220 --bold "๐ Drive Information:"echo ""get_drive_info "$DISK" | while read -r line; do gum style --foreground 255 " $line"done# Check for filesystem issuesecho ""gum style --foreground 245 "๐ Checking drive health..."HEALTH_ISSUES=$(check_drive_health "$DISK" 2>/dev/null)if [ -n "$HEALTH_ISSUES" ]; then echo "" gum style --foreground 196 --bold "โ ๏ธ FILESYSTEM ISSUES DETECTED:" gum style --foreground 208 " This drive has a corrupted filesystem!" gum style --foreground 208 " This is why macOS shows 'disk not readable' error." gum style --foreground 255 " $HEALTH_ISSUES" gum style --foreground 86 " ๐ก Formatting will fix this issue." DRIVE_CORRUPTED=trueelse DRIVE_CORRUPTED=falsefi# Get disk base name (e.g., disk2 from /dev/disk2)DISK_BASE=$(basename "$DISK")# List mounted volumes for this disk using mount commandMOUNT_POINTS=$(mount | grep "/dev/${DISK_BASE}" | awk -F' on ' '{print $2}' | awk -F' \\(' '{print $1}')# Show contents if mountedif [ -n "$MOUNT_POINTS" ]; then echo "" gum style --foreground 86 "๐ Contents of the drive:" echo "" while IFS= read -r mount_point; do if [ -d "$mount_point" ]; then gum style --foreground 220 " Mount: $mount_point" # Show free space FREE_SPACE=$(df -h "$mount_point" | tail -1 | awk '{print $4}') USED_SPACE=$(df -h "$mount_point" | tail -1 | awk '{print $3}') CAPACITY=$(df -h "$mount_point" | tail -1 | awk '{print $5}') gum style --foreground 255 " Used: $USED_SPACE | Free: $FREE_SPACE | Capacity: $CAPACITY" echo "" # List contents (limited to first 15 items) gum style --foreground 141 " Files/Folders:" ls -lah "$mount_point" 2>/dev/null | head -20 | while read -r line; do echo " $line" done echo "" fi done <<<"$MOUNT_POINTS"else if [ "$DRIVE_CORRUPTED" = true ]; then gum style --foreground 208 "โ ๏ธ Drive cannot be mounted due to filesystem corruption" else gum style --foreground 208 "โ ๏ธ Drive is not mounted or has no readable volumes" fifi# Ask if user wants to formatecho ""if gum confirm "๐๏ธ Do you want to FORMAT this drive? (ALL DATA WILL BE LOST)"; then # Choose filesystem gum style --foreground 86 "๐ Select filesystem format:" FORMAT=$(gum choose \ "ExFAT (Best for cross-platform)" \ "APFS (Apple - macOS only)" \ "APFS Encrypted (Apple - macOS only, secure)" \ "Mac OS Extended (HFS+ Journaled)" \ "Mac OS Extended Encrypted (HFS+ Journaled, secure)" \ "MS-DOS FAT32 (Legacy, <32GB)") # Star Trek character names TREK_NAMES=( "KIRK" "SPOCK" "PICARD" "DATA" "WORF" "RIKER" "JANEWAY" "SEVEN" "TUVOK" "BONES" "SCOTTY" "UHURA" "SULU" "CHEKOV" "CRUSHER" "TROI" "LAFORGE" "SISKO" "DAX" "ODO" "QUARK" "BASHIR" "ARCHER" "TPOL" "BURNHAM" "PIKE" ) # Pick a random Star Trek name RANDOM_NAME=${TREK_NAMES[$RANDOM % ${#TREK_NAMES[@]}]} # Get new volume name gum style --foreground 86 "๐ Enter new volume name:" VOLUME_NAME=$(gum input --placeholder "$RANDOM_NAME" --value "$RANDOM_NAME") if [ -z "$VOLUME_NAME" ]; then VOLUME_NAME="$RANDOM_NAME" fi # Map format selection to diskutil format ENCRYPTED=false case "$FORMAT" in "ExFAT"*) FS_FORMAT="ExFAT" ;; "APFS Encrypted"*) FS_FORMAT="APFS" ENCRYPTED=true ;; "APFS"*) FS_FORMAT="APFS" ;; "Mac OS Extended Encrypted"*) FS_FORMAT="JHFS+" ENCRYPTED=true ;; "Mac OS Extended"*) FS_FORMAT="JHFS+" ;; "MS-DOS"*) FS_FORMAT="MS-DOS FAT32" ;; *) FS_FORMAT="ExFAT" ;; esac # Get password if encrypted format selected if [ "$ENCRYPTED" = true ]; then echo "" gum style --foreground 220 "๐ Encryption requires a password" PASSPHRASE=$(gum input --password --placeholder "Enter encryption password") if [ -z "$PASSPHRASE" ]; then gum style --foreground 196 "โ Password cannot be empty for encryption!" exit 1 fi PASSPHRASE_CONFIRM=$(gum input --password --placeholder "Confirm encryption password") if [ "$PASSPHRASE" != "$PASSPHRASE_CONFIRM" ]; then gum style --foreground 196 "โ Passwords do not match!" exit 1 fi fi # Final confirmation echo "" gum style --foreground 196 --bold "โ ๏ธ WARNING: This will ERASE ALL DATA on $DISK" gum style --foreground 255 " Format: $FS_FORMAT" gum style --foreground 255 " Name: $VOLUME_NAME" if [ "$ENCRYPTED" = true ]; then gum style --foreground 220 " ๐ Encryption: YES" fi echo "" if gum confirm --default=false "Are you ABSOLUTELY sure?"; then echo "" gum spin --spinner dot --title "Formatting $DISK as $FS_FORMAT..." -- \ diskutil eraseDisk "$FS_FORMAT" "$VOLUME_NAME" GPT "$DISK" # Handle encryption if selected if [ "$ENCRYPTED" = true ]; then echo "" # Find the volume to encrypt VOLUME_DISK=$(diskutil list "$DISK" | grep "$VOLUME_NAME" | awk '{print $NF}') if [ -n "$VOLUME_DISK" ]; then gum spin --spinner dot --title "๐ Encrypting volume..." -- \ diskutil apfs encryptVolume "/dev/$VOLUME_DISK" -user disk -passphrase "$PASSPHRASE" gum style --foreground 46 "๐ Volume encrypted successfully!" else gum style --foreground 208 "โ ๏ธ Could not find volume to encrypt. You may need to encrypt manually." fi fi echo "" gum style --foreground 46 "โ
Drive formatted successfully!" echo "" # Show new drive info gum style --foreground 86 "๐ New drive information:" diskutil info "$DISK" | grep -E "Volume Name:|Disk Size:|Volume Free Space:|File System Personality:" | sed 's/^/ /' echo "" if gum confirm "โ๏ธ Do you want to eject the drive?"; then gum spin --spinner dot --title "Ejecting $DISK..." -- \ diskutil eject "$DISK" echo "" gum style --foreground 46 "โ
Drive ejected safely! You can remove it now." fi else gum style --foreground 208 "โ Format cancelled." fielse gum style --foreground 86 "๐ Goodbye! No changes were made."fi