Add --local option to force installation to user bin directory, preventing automatic fallback when sudo is unavailable

This commit is contained in:
kdusek
2025-11-13 20:16:01 +01:00
parent 85fec83f2c
commit c727d8e1f2
3 changed files with 111 additions and 81 deletions

View File

@@ -278,28 +278,53 @@ check_version_symlink() {
# Function to determine installation strategy
determine_install_strategy() {
local local_install="$1"
local current_info=$(get_current_version)
local current_version=$(echo "$current_info" | cut -d: -f1)
local install_type=$(echo "$current_info" | cut -d: -f2)
# Strategy: Prefer system installation for all users
if [[ $EUID -eq 0 ]]; then
print_status "Running as root, installing system-wide"
INSTALL_STRATEGY="system_install"
TARGET_DIR="$SYSTEM_INSTALL_DIR"
elif sudo -n true 2>/dev/null; then
print_status "Passwordless sudo available, installing system-wide"
INSTALL_STRATEGY="system_install"
TARGET_DIR="$SYSTEM_INSTALL_DIR"
elif command_exists sudo; then
print_status "Testing sudo access (you may be prompted for password)..."
if sudo -v 2>/dev/null; then
print_success "Sudo access confirmed, installing system-wide"
# Strategy: Check installation type preference
if [[ "$local_install" == "true" ]]; then
print_status "Local installation requested"
if check_user_bin_setup; then
print_status "User bin directory is available and in PATH"
INSTALL_STRATEGY="user_install"
TARGET_DIR="$USER_BIN_DIR"
else
print_error "User bin directory not available for local installation"
exit 1
fi
else
# Prefer system installation for all users
if [[ $EUID -eq 0 ]]; then
print_status "Running as root, installing system-wide"
INSTALL_STRATEGY="system_install"
TARGET_DIR="$SYSTEM_INSTALL_DIR"
elif sudo -n true 2>/dev/null; then
print_status "Passwordless sudo available, installing system-wide"
INSTALL_STRATEGY="system_install"
TARGET_DIR="$SYSTEM_INSTALL_DIR"
elif command_exists sudo; then
print_status "Testing sudo access (you may be prompted for password)..."
if sudo -v 2>/dev/null; then
print_success "Sudo access confirmed, installing system-wide"
INSTALL_STRATEGY="system_install"
TARGET_DIR="$SYSTEM_INSTALL_DIR"
else
print_error "Sudo access failed. Use --local to install to user bin directory"
exit 1
fi
else
print_warning "Sudo access failed, falling back to user installation"
print_error "Sudo not available. Use --local to install to user bin directory"
exit 1
fi
# Warn if user bin has borg (only for system installs)
if [[ "$INSTALL_STRATEGY" == "system_install" ]] && [[ -f "$USER_BIN_DIR/borg" ]]; then
print_warning "Borg CLI found in user bin directory ($USER_BIN_DIR)"
print_status "This might take precedence if $USER_BIN_DIR is in PATH before system paths"
fi
fi
else
print_warning "Sudo not available, using user installation"
fi
@@ -463,6 +488,7 @@ show_usage() {
echo " -v, --version Install specific version (default: latest)"
echo " -f, --force Force reinstall even if up-to-date"
echo " -r, --reinstall Reinstall the current installed version"
echo " -l, --local Force installation to user bin directory (~/.bin)"
echo " -p, --package Force installation from package manager"
echo " -b, --binary Force installation from binary"
echo
@@ -470,6 +496,7 @@ show_usage() {
echo " $0 # Install latest version using best method"
echo " $0 -v v1.2.7 # Install specific version"
echo " $0 --reinstall # Reinstall current version"
echo " $0 --local # Force installation to user bin"
echo " $0 --package # Force installation from package manager"
echo " $0 --binary # Force installation from binary"
echo
@@ -480,6 +507,7 @@ main() {
local target_version=""
local force_reinstall=false
local reinstall_current=false
local local_install=false
local install_method="auto"
# Parse command line arguments
@@ -501,6 +529,10 @@ main() {
reinstall_current=true
shift
;;
-l|--local)
local_install=true
shift
;;
-p|--package)
install_method="package"
shift
@@ -553,7 +585,7 @@ main() {
fi
# Determine installation strategy
determine_install_strategy
determine_install_strategy "$local_install"
# Check if update is needed
if ! check_update_needed "$target_version"; then