#!/usr/bin/env bash # ------------------------------------------------------------------------------ # Copyright (c) 2013-2026 Geoffray Levasseur # Protected by the BSD3 license. Please read bellow for details. # # * Redistribution and use in source and binary forms, # * with or without modification, are permitted provided # * that the following conditions are met: # * # * Redistributions of source code must retain the above # * copyright notice, this list of conditions and the # * following disclaimer. # * # * Redistributions in binary form must reproduce the above # * copyright notice, this list of conditions and the following # * disclaimer in the documentation and/or other materials # * provided with the distribution. # * # * Neither the name of the copyright holder nor the names # * of any other contributors may be used to endorse or # * promote products derived from this software without # * specific prior written permission. # * # * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND # * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, # * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR # * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # * OF SUCH DAMAGE. # ------------------------------------------------------------------------------ # Private helpers of profile.d/packages.sh, loaded by the module itself. # Internal helper: run a package manager command, with root privileges when # needed (sudo, else doas), or only display it in dry-run mode. # Usage: _pkg_run [args...] _pkg_run() { local needs_root="$1" dry="$2" shift 2 local -a prefix=() if (( needs_root )) && [[ $EUID -ne 0 ]]; then if command -v sudo >/dev/null 2>&1; then prefix=(sudo) elif command -v doas >/dev/null 2>&1; then prefix=(doas) elif (( ! dry )); then disp E "Root privileges are required and neither sudo nor doas is available." return 1 fi fi if (( dry )); then # %q gives a copy-pasteable command line printf '%q ' "${prefix[@]}" "$@" printf '\n' return 0 fi if ! command -v "$1" >/dev/null 2>&1; then disp E "'$1' is not installed." return 1 fi "${prefix[@]}" "$@" } export -f _pkg_run