Implemented logging functions

This commit is contained in:
Tristan Ancelet 2023-11-18 19:32:42 -06:00
parent 02dbe5e0eb
commit a734bbf83b

View File

@ -2,6 +2,7 @@
# BEGIN: Variables
DEBUG=1
ACTION=""
DOWNLOAD_URL='https://discord.com/api/download/stable?platform=linux&format=tar.gz'
VERSION_REGEX='[0-9]+\.[0-9]+\.[0-9]+'
@ -19,6 +20,12 @@ DESKTOP_FILE_INSTALLED=0
# BEGIN: Helper Functions
function log () {
if [[ $DEBUG -eq 1 ]]; then local DATE=`date` local MESSAGE="${1:?"log: Message not provided"}"
echo "$DATE : $MESSAGE"
fi
}
function get_remote_version(){
local REMOTE_VERSION=`curl $DOWNLOAD_URL | grep -Eo $VERSION_REGEX | head -n 1`
@ -26,7 +33,7 @@ function get_remote_version(){
echo "$REMOTE_VERSION"
return 0
else
echo "error: remote version not found"
log "error: remote version not found"
return 1
fi
}
@ -36,17 +43,19 @@ function do_download(){
## If the provided version doesn't match the format #.#.#
if [[ ! "$VERSION" =~ ^$VERSION_REGEX$ ]]; then
echo "do_download : the version ($VERSION) provided, does not match format $VERSION_REGEX."
log "do_download : the version ($VERSION) provided, does not match format $VERSION_REGEX."
exit 2
fi
## Making the URL to download the package
### replacing all {VERSION} in PACKAGE_DOWNLOAD_URL_BASE with $REMOTE_VERSION using string substitution
PACKAGE_DOWNLOAD_URL="${PACKAGE_DOWNLOAD_URL_BASE//\{VERSION\}/$REMOTE_VERSION}"
log "Download url created ($PACKAGE_DOWNLOAD_URL)"
## Getting filename from url
### Removing all characters from the last / back to leave only the filename
FILENAME="${PACKAGE_DOWNLOAD_URL/*\/}"
log "File was downloaded as $FILENAME"
## Downloading the discord package (tar.gz)
curl "$PACKAGE_DOWNLOAD_URL" -o "$FILENAME" >/dev/null 2>&1
@ -54,6 +63,8 @@ function do_download(){
function update_desktop_file() {
log "Updating desktop file"
## Desktop file in the current/new install
DESKTOP_FILE=$EXISTING_INSTALL/discord.desktop
@ -75,6 +86,7 @@ function update_desktop_file() {
## Pushing replacement values to desktop file
echo -e "$DESKTOP_FILE_CONTENTS" > $DESKTOP_FILE
log "Desktop file has been updated"
}
@ -89,8 +101,8 @@ function do_install() {
if [[ -f $FILENAME ]]; then
tar xf "$FILENAME"
else
echo "$FILENAME failed to download. Exiting now"
exit 1
log "$FILENAME failed to download. Exiting now"
exit 3
fi
update_desktop_file
@ -137,11 +149,13 @@ function do_upgrade(){
if [[ -d $EXISTING_INSTALL ]] && [[ -f $BUILD_FILE ]]; then
LOCAL_VERSION=`grep -Eo $VERSION_REGEX $BUILD_FILE`
log "Local version was found to be ($LOCAL_VERSION)"
else
ACTION=INSTALL
fi
REMOTE_VERSION=`get_remote_version`
log "Retrieved remote version ($REMOTE_VERSION)"
# END: Pre-Work Check