mirror of
https://github.com/fosrl/newt.git
synced 2026-03-26 12:36:45 +00:00
feat(installer): prefer /usr/local/bin and improve POSIX compatibility
- Always install to /usr/local/bin instead of ~/.local/bin
- Use sudo automatically when write access is needed
- Replace bash-specific syntax with POSIX equivalents:
- Change shebang from #!/bin/bash to #!/bin/sh
- Replace [[ == *pattern* ]] with case statements
- Replace echo -e with printf for colored output
- Script now works with dash, ash, busybox sh, and bash
This commit is contained in:
79
get-newt.sh
79
get-newt.sh
@@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
|
|
||||||
# Get Newt - Cross-platform installation script
|
# Get Newt - Cross-platform installation script
|
||||||
# Usage: curl -fsSL https://raw.githubusercontent.com/fosrl/newt/refs/heads/main/get-newt.sh | bash
|
# Usage: curl -fsSL https://raw.githubusercontent.com/fosrl/newt/refs/heads/main/get-newt.sh | sh
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
@@ -17,15 +17,15 @@ GITHUB_API_URL="https://api.github.com/repos/${REPO}/releases/latest"
|
|||||||
|
|
||||||
# Function to print colored output
|
# Function to print colored output
|
||||||
print_status() {
|
print_status() {
|
||||||
echo -e "${GREEN}[INFO]${NC} $1"
|
printf '%b[INFO]%b %s\n' "${GREEN}" "${NC}" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
print_warning() {
|
print_warning() {
|
||||||
echo -e "${YELLOW}[WARN]${NC} $1"
|
printf '%b[WARN]%b %s\n' "${YELLOW}" "${NC}" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
print_error() {
|
print_error() {
|
||||||
echo -e "${RED}[ERROR]${NC} $1"
|
printf '%b[ERROR]%b %s\n' "${RED}" "${NC}" "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to get latest version from GitHub API
|
# Function to get latest version from GitHub API
|
||||||
@@ -113,16 +113,34 @@ get_install_dir() {
|
|||||||
if [ "$OS" = "windows" ]; then
|
if [ "$OS" = "windows" ]; then
|
||||||
echo "$HOME/bin"
|
echo "$HOME/bin"
|
||||||
else
|
else
|
||||||
# Try to use a directory in PATH, fallback to ~/.local/bin
|
# Prefer /usr/local/bin for system-wide installation
|
||||||
if echo "$PATH" | grep -q "/usr/local/bin"; then
|
|
||||||
if [ -w "/usr/local/bin" ] 2>/dev/null; then
|
|
||||||
echo "/usr/local/bin"
|
echo "/usr/local/bin"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if we need sudo for installation
|
||||||
|
needs_sudo() {
|
||||||
|
local install_dir="$1"
|
||||||
|
if [ -w "$install_dir" ] 2>/dev/null; then
|
||||||
|
return 1 # No sudo needed
|
||||||
else
|
else
|
||||||
echo "$HOME/.local/bin"
|
return 0 # Sudo needed
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get the appropriate command prefix (sudo or empty)
|
||||||
|
get_sudo_cmd() {
|
||||||
|
local install_dir="$1"
|
||||||
|
if needs_sudo "$install_dir"; then
|
||||||
|
if command -v sudo >/dev/null 2>&1; then
|
||||||
|
echo "sudo"
|
||||||
|
else
|
||||||
|
print_error "Cannot write to ${install_dir} and sudo is not available."
|
||||||
|
print_error "Please run this script as root or install sudo."
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo "$HOME/.local/bin"
|
echo ""
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,14 +148,17 @@ get_install_dir() {
|
|||||||
install_newt() {
|
install_newt() {
|
||||||
local platform="$1"
|
local platform="$1"
|
||||||
local install_dir="$2"
|
local install_dir="$2"
|
||||||
|
local sudo_cmd="$3"
|
||||||
local binary_name="newt_${platform}"
|
local binary_name="newt_${platform}"
|
||||||
local exe_suffix=""
|
local exe_suffix=""
|
||||||
|
|
||||||
# Add .exe suffix for Windows
|
# Add .exe suffix for Windows
|
||||||
if [[ "$platform" == *"windows"* ]]; then
|
case "$platform" in
|
||||||
|
*windows*)
|
||||||
binary_name="${binary_name}.exe"
|
binary_name="${binary_name}.exe"
|
||||||
exe_suffix=".exe"
|
exe_suffix=".exe"
|
||||||
fi
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
local download_url="${BASE_URL}/${binary_name}"
|
local download_url="${BASE_URL}/${binary_name}"
|
||||||
local temp_file="/tmp/newt${exe_suffix}"
|
local temp_file="/tmp/newt${exe_suffix}"
|
||||||
@@ -155,14 +176,18 @@ install_newt() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Make executable before moving
|
||||||
|
chmod +x "$temp_file"
|
||||||
|
|
||||||
# Create install directory if it doesn't exist
|
# Create install directory if it doesn't exist
|
||||||
|
if [ -n "$sudo_cmd" ]; then
|
||||||
|
$sudo_cmd mkdir -p "$install_dir"
|
||||||
|
print_status "Using sudo to install to ${install_dir}"
|
||||||
|
$sudo_cmd mv "$temp_file" "$final_path"
|
||||||
|
else
|
||||||
mkdir -p "$install_dir"
|
mkdir -p "$install_dir"
|
||||||
|
|
||||||
# Move binary to install directory
|
|
||||||
mv "$temp_file" "$final_path"
|
mv "$temp_file" "$final_path"
|
||||||
|
fi
|
||||||
# Make executable (not needed on Windows, but doesn't hurt)
|
|
||||||
chmod +x "$final_path"
|
|
||||||
|
|
||||||
print_status "newt installed to ${final_path}"
|
print_status "newt installed to ${final_path}"
|
||||||
|
|
||||||
@@ -179,9 +204,9 @@ verify_installation() {
|
|||||||
local install_dir="$1"
|
local install_dir="$1"
|
||||||
local exe_suffix=""
|
local exe_suffix=""
|
||||||
|
|
||||||
if [[ "$PLATFORM" == *"windows"* ]]; then
|
case "$PLATFORM" in
|
||||||
exe_suffix=".exe"
|
*windows*) exe_suffix=".exe" ;;
|
||||||
fi
|
esac
|
||||||
|
|
||||||
local newt_path="${install_dir}/newt${exe_suffix}"
|
local newt_path="${install_dir}/newt${exe_suffix}"
|
||||||
|
|
||||||
@@ -215,17 +240,19 @@ main() {
|
|||||||
INSTALL_DIR=$(get_install_dir)
|
INSTALL_DIR=$(get_install_dir)
|
||||||
print_status "Install directory: ${INSTALL_DIR}"
|
print_status "Install directory: ${INSTALL_DIR}"
|
||||||
|
|
||||||
|
# Check if we need sudo
|
||||||
|
SUDO_CMD=$(get_sudo_cmd "$INSTALL_DIR")
|
||||||
|
if [ -n "$SUDO_CMD" ]; then
|
||||||
|
print_status "Root privileges required for installation to ${INSTALL_DIR}"
|
||||||
|
fi
|
||||||
|
|
||||||
# Install newt
|
# Install newt
|
||||||
install_newt "$PLATFORM" "$INSTALL_DIR"
|
install_newt "$PLATFORM" "$INSTALL_DIR" "$SUDO_CMD"
|
||||||
|
|
||||||
# Verify installation
|
# Verify installation
|
||||||
if verify_installation "$INSTALL_DIR"; then
|
if verify_installation "$INSTALL_DIR"; then
|
||||||
print_status "newt is ready to use!"
|
print_status "newt is ready to use!"
|
||||||
if [[ "$PLATFORM" == *"windows"* ]]; then
|
|
||||||
print_status "Run 'newt --help' to get started"
|
print_status "Run 'newt --help' to get started"
|
||||||
else
|
|
||||||
print_status "Run 'newt --help' to get started"
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user