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,11 +278,24 @@ check_version_symlink() {
# Function to determine installation strategy # Function to determine installation strategy
determine_install_strategy() { determine_install_strategy() {
local local_install="$1"
local current_info=$(get_current_version) local current_info=$(get_current_version)
local current_version=$(echo "$current_info" | cut -d: -f1) local current_version=$(echo "$current_info" | cut -d: -f1)
local install_type=$(echo "$current_info" | cut -d: -f2) local install_type=$(echo "$current_info" | cut -d: -f2)
# Strategy: Prefer system installation for all users # 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 if [[ $EUID -eq 0 ]]; then
print_status "Running as root, installing system-wide" print_status "Running as root, installing system-wide"
INSTALL_STRATEGY="system_install" INSTALL_STRATEGY="system_install"
@@ -298,7 +311,19 @@ determine_install_strategy() {
INSTALL_STRATEGY="system_install" INSTALL_STRATEGY="system_install"
TARGET_DIR="$SYSTEM_INSTALL_DIR" TARGET_DIR="$SYSTEM_INSTALL_DIR"
else else
print_warning "Sudo access failed, falling back to user installation" print_error "Sudo access failed. Use --local to install to user bin directory"
exit 1
fi
else
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 fi
else else
print_warning "Sudo not available, using user installation" print_warning "Sudo not available, using user installation"
@@ -463,6 +488,7 @@ show_usage() {
echo " -v, --version Install specific version (default: latest)" echo " -v, --version Install specific version (default: latest)"
echo " -f, --force Force reinstall even if up-to-date" echo " -f, --force Force reinstall even if up-to-date"
echo " -r, --reinstall Reinstall the current installed version" 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 " -p, --package Force installation from package manager"
echo " -b, --binary Force installation from binary" echo " -b, --binary Force installation from binary"
echo echo
@@ -470,6 +496,7 @@ show_usage() {
echo " $0 # Install latest version using best method" echo " $0 # Install latest version using best method"
echo " $0 -v v1.2.7 # Install specific version" echo " $0 -v v1.2.7 # Install specific version"
echo " $0 --reinstall # Reinstall current 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 --package # Force installation from package manager"
echo " $0 --binary # Force installation from binary" echo " $0 --binary # Force installation from binary"
echo echo
@@ -480,6 +507,7 @@ main() {
local target_version="" local target_version=""
local force_reinstall=false local force_reinstall=false
local reinstall_current=false local reinstall_current=false
local local_install=false
local install_method="auto" local install_method="auto"
# Parse command line arguments # Parse command line arguments
@@ -501,6 +529,10 @@ main() {
reinstall_current=true reinstall_current=true
shift shift
;; ;;
-l|--local)
local_install=true
shift
;;
-p|--package) -p|--package)
install_method="package" install_method="package"
shift shift
@@ -553,7 +585,7 @@ main() {
fi fi
# Determine installation strategy # Determine installation strategy
determine_install_strategy determine_install_strategy "$local_install"
# Check if update is needed # Check if update is needed
if ! check_update_needed "$target_version"; then if ! check_update_needed "$target_version"; then

View File

@@ -511,6 +511,7 @@ show_usage() {
echo " -v, --version Install specific version (default: latest)" echo " -v, --version Install specific version (default: latest)"
echo " -f, --force Force reinstall even if already installed" echo " -f, --force Force reinstall even if already installed"
echo " -r, --reinstall Reinstall the current installed version" 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 " -p, --package Force installation from package manager"
echo " -d, --deb Force installation from DEB package" echo " -d, --deb Force installation from DEB package"
echo " -t, --tarball Force installation from tarball" echo " -t, --tarball Force installation from tarball"
@@ -520,6 +521,7 @@ show_usage() {
echo " $0 # Install latest version using best method" echo " $0 # Install latest version using best method"
echo " $0 -v v2.6.0 # Install specific version" echo " $0 -v v2.6.0 # Install specific version"
echo " $0 --reinstall # Reinstall current 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 --package # Force installation from package manager"
echo " $0 --deb # Force installation from DEB package" echo " $0 --deb # Force installation from DEB package"
echo " $0 --checks # Check configuration without installing" echo " $0 --checks # Check configuration without installing"
@@ -556,6 +558,7 @@ main() {
local target_version="" local target_version=""
local force_reinstall=false local force_reinstall=false
local reinstall_current=false local reinstall_current=false
local local_install=false
local install_method="auto" local install_method="auto"
local checks_only=false local checks_only=false
@@ -578,6 +581,10 @@ main() {
reinstall_current=true reinstall_current=true
shift shift
;; ;;
-l|--local)
local_install=true
shift
;;
-p|--package) -p|--package)
install_method="package" install_method="package"
shift shift
@@ -644,19 +651,18 @@ main() {
detect_os detect_os
ARCH=$(get_arch) ARCH=$(get_arch)
# Create temporary directory # Check for local installation
mkdir -p "$TEMP_DIR" if [[ "$local_install" == "true" ]]; then
print_error "Git Credential Manager requires system installation for proper integration"
exit 1
fi
# Trap cleanup on exit # Get latest version if not specified
trap cleanup EXIT if [[ -z "$target_version" ]]; then
get_latest_release
# Installation logic based on method target_version="$LATEST_VERSION"
case $install_method in else
auto) print_status "Using specified version: $target_version"
# Try package manager first
if install_from_package_manager; then
verify_installation
exit 0
fi fi
# Fall back to distribution-specific methods # Fall back to distribution-specific methods

View File

@@ -154,11 +154,24 @@ check_version_symlink() {
# Function to determine installation strategy # Function to determine installation strategy
determine_install_strategy() { determine_install_strategy() {
local local_install="$1"
local current_info=$(get_current_version) local current_info=$(get_current_version)
local current_version=$(echo "$current_info" | cut -d: -f1) local current_version=$(echo "$current_info" | cut -d: -f1)
local install_type=$(echo "$current_info" | cut -d: -f2) local install_type=$(echo "$current_info" | cut -d: -f2)
# Strategy: Prefer system installation for all users # 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 if [[ $EUID -eq 0 ]]; then
print_status "Running as root, installing system-wide" print_status "Running as root, installing system-wide"
INSTALL_STRATEGY="system_install" INSTALL_STRATEGY="system_install"
@@ -174,10 +187,12 @@ determine_install_strategy() {
INSTALL_STRATEGY="system_install" INSTALL_STRATEGY="system_install"
TARGET_DIR="$SYSTEM_INSTALL_DIR" TARGET_DIR="$SYSTEM_INSTALL_DIR"
else else
print_warning "Sudo access failed, falling back to user installation" print_error "Sudo access failed. Use --local to install to user bin directory"
exit 1
fi fi
else else
print_warning "Sudo not available, using user installation" print_error "Sudo not available. Use --local to install to user bin directory"
exit 1
fi fi
# Warn if user bin has tea (only for system installs) # Warn if user bin has tea (only for system installs)
@@ -185,36 +200,6 @@ determine_install_strategy() {
print_warning "Tea CLI found in user bin directory ($USER_BIN_DIR)" print_warning "Tea 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" print_status "This might take precedence if $USER_BIN_DIR is in PATH before system paths"
fi fi
# If not system install, check user bin
if [[ "$INSTALL_STRATEGY" != "system_install" ]]; then
print_status "No sudo access, checking user bin directory"
if check_user_bin_setup; then
print_status "User bin directory is available and in PATH"
# Check if tea exists in user bin
if [[ -f "$USER_BIN_DIR/tea" ]]; then
print_status "Tea CLI found in user bin directory"
# Check for version symlink
if check_version_symlink; then
print_status "Version symlink found: $CURRENT_SYMLINK_VERSION"
INSTALL_STRATEGY="user_update"
TARGET_DIR="$USER_BIN_DIR"
else
print_status "No version symlink found, will create one"
INSTALL_STRATEGY="user_upgrade"
TARGET_DIR="$USER_BIN_DIR"
fi
else
print_status "Tea CLI not found in user bin, will install there"
INSTALL_STRATEGY="user_install"
TARGET_DIR="$USER_BIN_DIR"
fi
else
print_error "Cannot install system-wide (no sudo) and user bin not available"
exit 1
fi
fi fi
print_status "Installation strategy: $INSTALL_STRATEGY" print_status "Installation strategy: $INSTALL_STRATEGY"
@@ -453,6 +438,7 @@ show_usage() {
echo " -v, --version Install specific version (default: latest)" echo " -v, --version Install specific version (default: latest)"
echo " -f, --force Force reinstall even if up-to-date" echo " -f, --force Force reinstall even if up-to-date"
echo " -r, --reinstall Reinstall the current installed version" 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 " -p, --package Force installation from package manager"
echo " -b, --binary Force installation from binary" echo " -b, --binary Force installation from binary"
echo echo
@@ -460,6 +446,7 @@ show_usage() {
echo " $0 # Install latest version using best method" echo " $0 # Install latest version using best method"
echo " $0 -v v0.10.0 # Install specific version" echo " $0 -v v0.10.0 # Install specific version"
echo " $0 --reinstall # Reinstall current 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 --package # Force installation from package manager"
echo " $0 --binary # Force installation from binary" echo " $0 --binary # Force installation from binary"
echo echo
@@ -470,6 +457,7 @@ main() {
local target_version="" local target_version=""
local force_reinstall=false local force_reinstall=false
local reinstall_current=false local reinstall_current=false
local local_install=false
local install_method="auto" local install_method="auto"
# Parse command line arguments # Parse command line arguments
@@ -491,6 +479,10 @@ main() {
reinstall_current=true reinstall_current=true
shift shift
;; ;;
-l|--local)
local_install=true
shift
;;
-p|--package) -p|--package)
install_method="package" install_method="package"
shift shift
@@ -543,7 +535,7 @@ main() {
fi fi
# Determine installation strategy # Determine installation strategy
determine_install_strategy determine_install_strategy "$local_install"
# Check if update is needed # Check if update is needed
if ! check_update_needed "$target_version"; then if ! check_update_needed "$target_version"; then