major refactor
This commit is contained in:
90
lib/platform.sh
Normal file
90
lib/platform.sh
Normal file
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env bash
|
||||
# Platform and package manager detection for bootstrap CLI
|
||||
|
||||
if [ -n "${_LIB_PLATFORM_SOURCED:-}" ]; then
|
||||
return 0
|
||||
fi
|
||||
_LIB_PLATFORM_SOURCED=1
|
||||
|
||||
# Source common utilities if not already loaded
|
||||
if [ -z "${_LIB_COMMON_SOURCED:-}" ]; then
|
||||
# Assumes common.sh is in the same directory as platform.sh
|
||||
# We resolve the directory of the current script
|
||||
_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
. "$_LIB_DIR/common.sh"
|
||||
fi
|
||||
|
||||
detect_distro() {
|
||||
if has_command pacman; then
|
||||
echo "arch"
|
||||
elif has_command apt; then
|
||||
echo "debian"
|
||||
elif has_command dnf; then
|
||||
echo "fedora"
|
||||
else
|
||||
echo "unknown"
|
||||
fi
|
||||
}
|
||||
|
||||
detect_arch() {
|
||||
local arch
|
||||
arch="$(uname -m)"
|
||||
case "$arch" in
|
||||
x86_64) echo "x86_64" ;;
|
||||
aarch64|arm64) echo "arm64" ;;
|
||||
*) echo "$arch" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Install packages depending on detected distro
|
||||
# Usage: pkg_install <package_name_arch> <package_name_debian> <package_name_fedora>
|
||||
# Or simpler: map common packages to their distro equivalents
|
||||
pkg_install() {
|
||||
local distro
|
||||
distro=$(detect_distro)
|
||||
|
||||
if [ "$distro" = "unknown" ]; then
|
||||
log_error "Unsupported distribution. Cannot install packages automatically."
|
||||
return 1
|
||||
fi
|
||||
|
||||
local pkgs=()
|
||||
for arg in "$@"; do
|
||||
# Format can be "pkg" or "arch:pkg_a|debian:pkg_d|fedora:pkg_f"
|
||||
if [[ "$arg" =~ : ]]; then
|
||||
IFS='|' read -ra PARTS <<< "$arg"
|
||||
local mapped=""
|
||||
for part in "${PARTS[@]}"; do
|
||||
local d_prefix="${part%%:*}"
|
||||
local d_pkg="${part#*:}"
|
||||
if [ "$d_prefix" = "$distro" ]; then
|
||||
mapped="$d_pkg"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ -n "$mapped" ]; then
|
||||
pkgs+=("$mapped")
|
||||
fi
|
||||
else
|
||||
pkgs+=("$arg")
|
||||
fi
|
||||
done
|
||||
|
||||
if [ ${#pkgs[@]} -eq 0 ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_info "Installing packages via $distro package manager: ${pkgs[*]}"
|
||||
case "$distro" in
|
||||
arch)
|
||||
sudo pacman -Sy --needed --noconfirm "${pkgs[@]}"
|
||||
;;
|
||||
debian)
|
||||
sudo apt update
|
||||
sudo apt install -y "${pkgs[@]}"
|
||||
;;
|
||||
fedora)
|
||||
sudo dnf install -y "${pkgs[@]}"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
Reference in New Issue
Block a user