Refactor `RunBrewCmd()` into a generalised `RunCmdAsUser()`

This commit is contained in:
Neved4 2024-10-25 22:12:15 +02:00
parent 9a86c00373
commit ae64484955
2 changed files with 18 additions and 8 deletions

View File

@ -91,7 +91,7 @@
# ReportManual Log manual actions to report file
# ReportSuggestion Add a suggestion to report file
# ReportWarning Add a warning and priority to report file
# RunBrewCmd Run Homebrew commands as a normal user instead of root
# RunCmdAsUser Run commands as a normal user instead of root
# SafeFile Security tests to perform on a file before using it
# SafePerms Check if a file has safe permissions
# SafeInput Test provided string to see if it contains unwanted characters
@ -3038,17 +3038,27 @@
################################################################################
# Name : RunBrewCmd()
# Description : Run Homebrew commands as a normal user instead of root
# Name : RunCmdAsUser()
# Description : Run commands as a normal user instead of root
#
# Parameters : $@ = Homebrew command arguments
# Parameters : $@ = command arguments
# Returns : None (executes the command as the appropriate user)
################################################################################
RunBrewCmd() {
RunCmdAsUser() {
case "$(id -u)" in
0) sudo -u "$SUDO_USER" brew "$@" ;;
*) brew "$@"
0)
if command -v sudo >/dev/null
then
sudo -u "$SUDO_USER" "$@"
elif command -v su >/dev/null
then
su "$(id -un)" -c "$@"
else
"$@"
fi
;;
*) "$@"
esac
}

View File

@ -127,7 +127,7 @@
LogText "Test: Querying brew to get package list"
Display --indent 4 --text "- Querying brew for installed packages"
LogText "Output:"; LogText "-----"
GPACKAGES=$(RunBrewCmd list --versions)
GPACKAGES=$(RunCmdAsUser brew list --versions)
while IFS= read -r PKG; do
PACKAGE_NAME=$(echo ${PKG} | ${CUTBINARY} -d ' ' -f1)
PACKAGE_VERSION=$(echo ${PKG} | ${CUTBINARY} -d ' ' -f2)