1
0
mirror of https://github.com/Mayccoll/Gogh.git synced 2023-08-10 21:12:46 +03:00

Make sure exported variables get unset to avoid global variable pollution

This commit is contained in:
phenonymous 2018-11-30 23:03:39 +01:00
parent 5bdddddd62
commit 01f998e9ee
2 changed files with 46 additions and 5 deletions

View File

@ -10,6 +10,35 @@ GS="${GS:-$(command -v gsettings | xargs echo)}"
# Note: xargs echo is to make the command sucessful even if it was not
# otherwise the script will exit if the command does not exist (elementary os)
# |
# | Make sure all exported variables get unset no matter what
# | Defining this in this script because it gets called even if
# | gogh.sh was not called. Exported variables in gogh.sh gets
# | handled there in case there was en error before this script was called
# | ============================================
GLOBAL_VAR_CLEANUP() {
unset PROFILE_NAME
unset PROFILE_SLUG
unset scratchdir
unset TILIX_RES
unset TERMINAL
unset LOOP
unset OPTLENGTH
for c in $(seq -s " " -w 16); do
unset DEMO_COLOR_${c}
unset COLOR_${c}
done
unset BACKGROUND_COLOR
unset FOREGROUND_COLOR
unset CURSOR_COLOR
unset PROFILE_NAME
}
# Note: Since all scripts gets invoked in a subshell the traps from the parent shell
# will not get inherited. Hence traps defined in gogh.sh and print-themes.sh will still trigger
trap 'GLOBAL_VAR_CLEANUP; trap - EXIT' EXIT HUP INT QUIT PIPE TERM
# |
# | Second test for TERMINAL in case user ran
@ -252,6 +281,7 @@ if [[ "${COLORTERM:-}" == "truecolor" ]] || [[ "${COLORTERM:-}" == "24bit" ]]; t
[[ "$c" == "08" ]] && color_str+="\n" # new line
done
printf '\n%b\n\n\n' "${color_str}"
unset color_str
}
else
function gogh_colors () {
@ -262,6 +292,7 @@ else
[[ $c == 7 ]] && color_str+="\n" # new line
done
printf '\n%b\n\n' "${color_str}"
unset color_str
}
fi

16
gogh.sh
View File

@ -1,5 +1,16 @@
#!/usr/bin/env bash
# Define traps and trapfunctions early in case any errors before script exits
GLOBAL_VAR_CLEANUP(){
[[ -n "$(command -v TILIX_TMP_CLEANUP)" ]] && TILIX_TMP_CLEANUP
unset PROFILE_NAME
unset PROFILE_SLUG
unset TILIX_RES
unset TERMINAL
}
trap 'GLOBAL_VAR_CLEANUP; trap - EXIT' EXIT HUP INT QUIT PIPE TERM
declare -a THEMES=(
'3024-day.sh'
'3024-night.sh'
@ -341,18 +352,17 @@ if [[ "$TERMINAL" = "tilix" ]] && [[ ${#OPTION[@]} -gt 0 ]]; then
# | desides to abort before all themes has been processed this section will cleanup the tmpdir
# | =======================================
if [[ ${TILIX_RES::1} =~ ^(y|Y)$ ]]; then
cleanup() {
TILIX_TMP_CLEANUP() {
echo
echo "Cleaning up"
rm -rf "$scratchdir"
unset LOOP OPTLENGTH
unset LOOP OPTLENGTH scratchdir
echo "Done..."
exit 0
}
scratchdir=$(mktemp -d -t tmp.XXXXXXXX)
export scratchdir
trap 'cleanup; trap - EXIT' EXIT HUP INT QUIT PIPE TERM
fi
fi