Added several functions to support JoeyBuild.

This commit is contained in:
Scott Duensing 2023-01-12 16:51:02 -06:00
parent b0149d7417
commit 71dd635f47

146
towel.sh
View file

@ -39,6 +39,23 @@ tCYAN=6
# Global variables used by Towel. # Global variables used by Towel.
# #
__tG__PARENTPATH="${BASH_SOURCE[0]}" __tG__PARENTPATH="${BASH_SOURCE[0]}"
__tG__SUDOPASS=
#
# Argument helper to process things intended for Towel.
#
# Parameters:
# ARGS - All command line arguments passed to script.
#
function tArgsHandler() {
local __FIRST=$1
# Is SUDO after us?
if [[ "${__FIRST}" == "TOWEL_ASKPASS" ]]; then
tSudoAskPass # Does not return.
fi
}
# #
@ -93,6 +110,32 @@ function tCheckPackages() {
} }
#
# Downloads file.
#
# Parameters:
#
# URL - URL of file to download.
# FILE - Optional alternate name to save downloaded file under.
#
function tDownload() {
local __URL=$1
local __FILE=
tTrim __FILE "$2"
if [[ -z ${__FILE} ]]; then
__FILE=${__URL##*/}
fi
if [[ -e "${__FILE}" ]]; then
rm -f "${__FILE}"
fi
wget -O "${__FILE}" ${__URL}
}
# #
# Escapes a given string so it's safe to write into an XML document. # Escapes a given string so it's safe to write into an XML document.
# #
@ -111,6 +154,32 @@ function tEscapeXml() {
} }
#
# Downloads the latest release of a project from GitHub.
#
# Parameters:
#
# RESULT - Variable in which to return the name of the downloaded file.
# DEVELOPER - GitHub developer name that owns the project.
# PROJECT - Which project from that developer to download.
# EXTENSION - Which file extension to download from the release list.
#
function tFetchGitHubRelease() {
local __RESULT=$1
local __DEVELOPER=$2
local __PROJECT=$3
local __EXTENSION=$4
local __URL=$(curl -s https://api.github.com/repos/${__DEVELOPER}/${__PROJECT}/releases \
| grep "browser_download_url.*${__EXTENSION}" \
| cut -d : -f 2,3 \
| tr -d \" \
| head -n 1)
local __FILE=${__URL##*/}
tDownload ${__URL}
eval $__RESULT=\${__FILE}
}
# #
# Displays a whiptail-based file browser for selecting a particular type of file. # Displays a whiptail-based file browser for selecting a particular type of file.
# #
@ -266,6 +335,83 @@ function tGetTowelPath() {
} }
#
# Runs program with sudo asking for password using a nice dialog box.
#
# Parameters:
#
# ARGS - Command to execute with sudo.
#
function tSudo() {
local __ARGS="$@"
local __ASK="$(mktemp --suffix=.sh -q)"
if [[ "${__tG__SUDOPASS}x" == "x" ]]; then
cat <<- ASKPASS > "${__ASK}"
#!/bin/bash
"${BASH_SOURCE[1]}" TOWEL_ASKPASS
ASKPASS
else
cat <<- ASKPASS > "${__ASK}"
#!/bin/bash
echo "${__tG__SUDOPASS}"
ASKPASS
fi
chmod +x "${__ASK}"
if [[ "${__ARGS}x" == "x" ]]; then
SUDO_ASKPASS="${__ASK}" sudo -A -v
else
SUDO_ASKPASS="${__ASK}" sudo -A ${__ARGS}
fi
rm "${__ASK}"
}
#
# Helper function for asking for password using a nice dialog box.
#
# Parameters:
#
# None.
#
function tSudoAskPass() {
local __RET=
local __PASSWORD=
set +e
__PASSWORD=$(
whiptail --passwordbox "\n$(basename "${BASH_SOURCE[1]}") needs superuser access.\n\nPlease enter your password:" 12 60 --title "Need Root" 3>&1 1>&2 2>&3
)
__RET=$?
set -e
if [[ ${__RET} -ne 0 ]]; then
echo "Cannot continue without superuser access." 1>&2
exit 1
fi
printf "${__PASSWORD}\n"
exit 0
}
#
# Allows specifying the sudo password for automated installs.
#
# Parameters:
#
# PASSWORD - The sudo password to use.
#
function tSudoSetPassword() {
local __PASSWORD=$1
__tG__SUDOPASS=${__PASSWORD}
}
# #
# Removes whitespace from the beginning and end of a string. # Removes whitespace from the beginning and end of a string.
# #