|
1 | | -cmd_mkpasswd() { |
2 | | - # Check if mkpasswd is available |
3 | | - if ! command -v mkpasswd &> /dev/null; then |
4 | | - error "mkpasswd not found. Install it with: sudo apt-get install whois" |
| 1 | +install_mkpasswd() { |
| 2 | + if command -v mkpasswd &> /dev/null; then |
| 3 | + return 0 |
| 4 | + fi |
| 5 | + |
| 6 | + info "mkpasswd not found. Installing password hash helper..." |
| 7 | + |
| 8 | + local os_info pkg_manager |
| 9 | + IFS='|' read -r os_info pkg_manager <<< "$(detect_os)" |
| 10 | + |
| 11 | + if [ -z "$pkg_manager" ]; then |
| 12 | + warn "Could not detect package manager." |
| 13 | + return 1 |
| 14 | + fi |
| 15 | + |
| 16 | + local sudo_cmd="sudo" |
| 17 | + if [ "$(id -u 2>/dev/null || echo 1)" -eq 0 ]; then |
| 18 | + sudo_cmd="" |
| 19 | + elif ! command -v sudo &> /dev/null && [ "$pkg_manager" != "brew" ]; then |
| 20 | + warn "sudo is required to install mkpasswd with $pkg_manager." |
| 21 | + return 1 |
| 22 | + fi |
| 23 | + |
| 24 | + local install_success=false |
| 25 | + local log_file="/tmp/shipnode_mkpasswd_install_$$.log" |
| 26 | + |
| 27 | + case "$pkg_manager" in |
| 28 | + apt) |
| 29 | + info "Using apt to install whois..." |
| 30 | + $sudo_cmd apt update &> "$log_file" && \ |
| 31 | + $sudo_cmd apt install -y whois >> "$log_file" 2>&1 && install_success=true |
| 32 | + ;; |
| 33 | + dnf|yum) |
| 34 | + info "Using $pkg_manager to install whois..." |
| 35 | + $sudo_cmd "$pkg_manager" install -y whois &> "$log_file" && install_success=true |
| 36 | + ;; |
| 37 | + apk) |
| 38 | + info "Using apk to install whois..." |
| 39 | + $sudo_cmd apk add --no-cache whois &> "$log_file" && install_success=true |
| 40 | + ;; |
| 41 | + pacman) |
| 42 | + info "Using pacman to install whois..." |
| 43 | + $sudo_cmd pacman -S --needed --noconfirm whois &> "$log_file" && install_success=true |
| 44 | + ;; |
| 45 | + brew) |
| 46 | + info "Using Homebrew to install whois..." |
| 47 | + brew install whois &> "$log_file" && install_success=true |
| 48 | + ;; |
| 49 | + *) |
| 50 | + warn "Unsupported package manager: $pkg_manager" |
| 51 | + return 1 |
| 52 | + ;; |
| 53 | + esac |
| 54 | + |
| 55 | + if [ "$install_success" = true ] && command -v mkpasswd &> /dev/null; then |
| 56 | + success "mkpasswd installed successfully" |
| 57 | + rm -f "$log_file" |
| 58 | + return 0 |
5 | 59 | fi |
6 | 60 |
|
| 61 | + warn "Failed to install mkpasswd." |
| 62 | + if [ -f "$log_file" ]; then |
| 63 | + warn "Installation log available at: $log_file" |
| 64 | + fi |
| 65 | + warn "Install it manually with your package manager (Debian/Ubuntu package: whois)." |
| 66 | + return 1 |
| 67 | +} |
| 68 | + |
| 69 | +ensure_mkpasswd() { |
| 70 | + if command -v mkpasswd &> /dev/null; then |
| 71 | + return 0 |
| 72 | + fi |
| 73 | + |
| 74 | + install_mkpasswd || error "mkpasswd not found. Install it manually with your package manager (Debian/Ubuntu: sudo apt-get install whois)" |
| 75 | +} |
| 76 | + |
| 77 | +cmd_mkpasswd() { |
| 78 | + ensure_mkpasswd |
| 79 | + |
7 | 80 | info "Generate password hash for users.yml" |
8 | 81 | echo "" |
9 | 82 |
|
@@ -33,4 +106,3 @@ cmd_mkpasswd() { |
33 | 106 | echo " password: \"$hash\"" |
34 | 107 | echo "" |
35 | 108 | } |
36 | | - |
|
0 commit comments