utils.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash
  2. # utils.sh - Common utility functions for USM installer
  3. # Colors for terminal output
  4. readonly RED='\033[0;31m'
  5. readonly GREEN='\033[0;32m'
  6. readonly YELLOW='\033[1;33m'
  7. readonly BLUE='\033[0;34m'
  8. readonly CYAN='\033[0;36m'
  9. readonly BOLD='\033[1m'
  10. readonly NC='\033[0m' # No Color
  11. # Logging functions
  12. log_info() {
  13. echo -e "${GREEN}[INFO]${NC} $1"
  14. }
  15. log_warn() {
  16. echo -e "${YELLOW}[WARN]${NC} $1"
  17. }
  18. log_error() {
  19. echo -e "${RED}[ERROR]${NC} $1"
  20. }
  21. log_step() {
  22. echo -e "${CYAN}==>${NC} ${BOLD}$1${NC}"
  23. }
  24. # Check if running as root
  25. is_root() {
  26. [[ $EUID -eq 0 ]]
  27. }
  28. # Check if a command exists
  29. command_exists() {
  30. command -v "$1" &>/dev/null
  31. }
  32. # Get the system's package manager
  33. detect_package_manager() {
  34. if command_exists apt-get; then
  35. echo "apt"
  36. elif command_exists dnf; then
  37. echo "dnf"
  38. elif command_exists yum; then
  39. echo "dnf" # Fallback to dnf implementation
  40. elif command_exists pacman; then
  41. echo "pacman"
  42. else
  43. echo "unknown"
  44. fi
  45. }
  46. # Get the system's sudo command
  47. get_sudo() {
  48. if is_root; then
  49. echo ""
  50. elif command_exists sudo; then
  51. echo "sudo"
  52. elif command_exists doas; then
  53. echo "doas"
  54. else
  55. echo ""
  56. fi
  57. }
  58. # Confirm action with user
  59. confirm() {
  60. local prompt="$1"
  61. local default="${2:-n}"
  62. local response
  63. if [[ "$ASSUME_YES" == "true" ]]; then
  64. return 0
  65. fi
  66. if [[ "$default" == "y" ]]; then
  67. prompt="$prompt [Y/n]: "
  68. else
  69. prompt="$prompt [y/N]: "
  70. fi
  71. read -r -p "$prompt" response
  72. response="${response:-$default}"
  73. [[ "$response" =~ ^[Yy]$ ]]
  74. }
  75. # Create directory if it doesn't exist
  76. ensure_dir() {
  77. local dir="$1"
  78. if [[ ! -d "$dir" ]]; then
  79. mkdir -p "$dir"
  80. fi
  81. }
  82. # Extract XZ compressed data from this script
  83. extract_payload() {
  84. local output_dir="$1"
  85. local script_path="$0"
  86. local payload_start
  87. # Find the payload marker
  88. payload_start=$(grep -an "^__PAYLOAD_START__$" "$script_path" | cut -d: -f1)
  89. if [[ -z "$payload_start" ]]; then
  90. log_error "Could not find payload in installer"
  91. return 1
  92. fi
  93. # Extract and decompress the payload
  94. tail -n +$((payload_start + 1)) "$script_path" | xz -d | tar -xf - -C "$output_dir"
  95. }
  96. # Cleanup function for traps
  97. cleanup() {
  98. if [[ -n "$TEMP_DIR" && -d "$TEMP_DIR" ]]; then
  99. rm -rf "$TEMP_DIR"
  100. fi
  101. }
  102. # Set up temporary directory
  103. setup_temp_dir() {
  104. TEMP_DIR=$(mktemp -d)
  105. trap cleanup EXIT
  106. }
  107. # Download a file
  108. download_file() {
  109. local url="$1"
  110. local output="$2"
  111. if command_exists curl; then
  112. curl -fsSL "$url" -o "$output"
  113. elif command_exists wget; then
  114. wget -q "$url" -O "$output"
  115. else
  116. log_error "Neither curl nor wget is available"
  117. return 1
  118. fi
  119. }
  120. # Clone a git repository
  121. clone_repo() {
  122. local url="$1"
  123. local dest="$2"
  124. local branch="${3:-main}"
  125. if ! command_exists git; then
  126. log_error "git is not installed"
  127. return 1
  128. fi
  129. git clone --depth 1 -b "$branch" "$url" "$dest" 2>/dev/null
  130. }
  131. # Get total size of a directory
  132. get_dir_size() {
  133. local dir="$1"
  134. du -sb "$dir" 2>/dev/null | cut -f1
  135. }
  136. # Format bytes to human readable
  137. format_size() {
  138. local bytes=$1
  139. local units=('B' 'KB' 'MB' 'GB' 'TB')
  140. local unit=0
  141. local size=$bytes
  142. while (( size > 1024 && unit < 4 )); do
  143. size=$((size / 1024))
  144. ((unit++))
  145. done
  146. echo "$size${units[$unit]}"
  147. }