Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change of local installation directory to avoid bloating of home directory #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ Below is a short snippet that you can place at the start of your script or main
- If not, attempt to automatically install ShellCore if we failed to find an installation. The installation script is fully
unattended - errors are sent to **stderr**.
- If the installation fails, then output an error message to **stderr** and exit the script with a non-zero return code.
- Attempt to load ShellCore from `~/.pv-shcore` (local) first, then fallback to `/usr/local/share/pv-shcore` (global)
- Attempt to load ShellCore from `~/.local/share/pv-shcore` (local) first, then fallback to `/usr/local/share/pv-shcore` (global)

```bash
# Error handling function for ShellCore
_sc_fail() { >&2 echo "Failed to load or install Privex ShellCore..." && exit 1; }
# If `load.sh` isn't found in the user install / global install, then download and run the auto-installer
# from Privex's CDN.
[[ -f "${HOME}/.pv-shcore/load.sh" ]] || [[ -f "/usr/local/share/pv-shcore/load.sh" ]] || \
{ curl -fsS https://cdn.privex.io/github/shell-core/install.sh | bash >/dev/null; } || _sc_fail
[[ -f "${HOME}/.local/share/pv-shcore/load.sh" ]] || [[ -f "/usr/local/share/pv-shcore/load.sh" ]] || \
{ curl -fsS https://raw.githubusercontent.com/Privex/shell-core/master/lib/scripts/shellcore_install.sh | bash >/dev/null; } || _sc_fail

# Attempt to load the local install of ShellCore first, then fallback to global install if it's not found.
[[ -d "${HOME}/.pv-shcore" ]] && source "${HOME}/.pv-shcore/load.sh" || \
[[ -d "${HOME}/.local/share/pv-shcore" ]] && source "${HOME}/.local/share/pv-shcore/load.sh" || \
source "/usr/local/share/pv-shcore/load.sh" || _sc_fail

# Optionally, you may wish to run `autoupdate_shellcore` after loading it. This will quietly update ShellCore to
Expand Down
43 changes: 26 additions & 17 deletions base/trap.bash
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,12 @@ function exit_handler ()
local row="${BASH_REMATCH[1]}"
lineno="${BASH_REMATCH[2]}"

msgerr white "FILE:\t\t${error_file}"
msgerr white "${row^^}:\t\t${lineno}\n"
msgerr "FILE:\t\t${error_file}"
msgerr "${row^^}:\t\t${lineno}\n"

msgerr white "ERROR CODE:\t${error_code}"
msgerr "ERROR CODE:\t${error_code}"
msgerr yellow "ERROR MESSAGE:\n"
msgerr white "\t$error_message\n"
msgerr "\t$error_message\n"

else
regex="^${error_file}\$|^${error_file}\s+|\s+${error_file}\s+|\s+${error_file}\$"
Expand All @@ -346,12 +346,12 @@ function exit_handler ()
# (could not reproduce this case so far)
# ------------------------------------------------------

msgerr white "FILE:\t\t$error_file"
msgerr white "ROW:\t\tunknown\n"
msgerr "FILE:\t\t$error_file"
msgerr "ROW:\t\tunknown\n"

msgerr white "ERROR CODE:\t${error_code}"
msgerr "ERROR CODE:\t${error_code}"
msgerr yellow "ERROR MESSAGE:\n"
msgerr white "\t${stderr}\n"
msgerr "\t${stderr}\n"
else
_trap_debug "_backtrace did not match regex..."
# Neither the error line nor the error file was found on the log
Expand Down Expand Up @@ -389,21 +389,21 @@ function exit_handler ()
error_file="$( echo "$error_file" | sed -E 's/^[ \t]*//' | sed -E 's/[ \t]*$//' )"
fi

msgerr white "FILE:\t\t$error_file"
msgerr "FILE:\t\t$error_file"
if (($_TRAP_ERR_CALL==1)); then
msgerr white "ROW:\t\t$_TRAP_LAST_LINE\n"
msgerr white "Line which triggered the error:\n\n\t$_TRAP_LAST_CALLER\n\n"
msgerr "ROW:\t\t$_TRAP_LAST_LINE\n"
msgerr "Line which triggered the error:\n\n\t$_TRAP_LAST_CALLER\n\n"
else
msgerr white "ROW:\t\tunknown\n"
msgerr "ROW:\t\tunknown\n"
fi

msgerr white "ERROR CODE:\t${error_code}"
msgerr "ERROR CODE:\t${error_code}"
if [ -n "${stderr}" ]; then
msgerr yellow "ERROR MESSAGE:\n"
msgerr white "\t${stderr}\n"
msgerr "\t${stderr}\n"
else
msgerr yellow "ERROR MESSAGE:\n"
msgerr white "\t${error_message}\n"
msgerr "\t${error_message}\n"
fi
fi
fi
Expand Down Expand Up @@ -433,6 +433,9 @@ trap_err_handler() {
_TRAP_LAST_LINE="$1"
_TRAP_LAST_CALLER="$2"
_TRAP_ERR_CALL=1
if [ ! -z ${SG_PRETRAPEXIT+x} ]; then
eval $SG_PRETRAPEXIT
fi
exit $_ret
}

Expand Down Expand Up @@ -482,8 +485,14 @@ function trap_traceback
j=$(( i - 1 ))
local function="${FUNCNAME[$i]}"
local file="${BASH_SOURCE[$i]}"
local line="${BASH_LINENO[$j]}"
echo " ${function}() in ${file}:${line}"
local line=''
if [ $i -eq $start ]; then
line="$_TRAP_LAST_LINE"
else
line="${BASH_LINENO[$j]}"
fi
local cmd="$(awk "NR==$line" $file | sed 's/^ *//g')"
echo " ${cmd} ==> ${function}() in ${file}:${line}"
done
}

Expand Down
4 changes: 2 additions & 2 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ DIR="$( cd "$( dirname "${_SDIR}" )" && pwd )"
: ${SG_DEBUG=0} # If set to 1, will enable debugging output to stderr
: ${DEBUGLOG="${SG_DIR}/logs/debug.log"}

: ${SG_LOCALDIR="${HOME}/.pv-shcore"} # Folder to install Privex Shell Core for local installs
: ${SG_LOCALDIR="${HOME}/.local/share/pv-shcore"} # Folder to install Privex Shell Core for local installs
: ${SG_GLOBALDIR="/usr/local/share/pv-shcore"} # Folder to install Privex Shell Core for global installs
# How many seconds must've passed since the last update to trigger an auto-update
: ${SG_UPDATE_SECS=604800}
Expand All @@ -49,7 +49,7 @@ last_update_shellcore
update_shellcore() { bash "${SG_DIR}/run.sh" update; }
autoupdate_shellcore() {
last_update_shellcore
local _unix_now=$(date +'%s')
local _unix_now=$(date +'%s')
local unix_now=$(($_unix_now)) next_update=$((SG_LAST_UPDATE+SG_UPDATE_SECS))
local last_rel=$((unix_now-SG_LAST_UPDATE))
if (($next_update<$unix_now)); then
Expand Down
12 changes: 6 additions & 6 deletions lib/scripts/shellcore_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# #
# Released under the GNU GPLv3 #
# #
# Official Repo: github.com/Privex/shell-core #
# Official Repo: github.com/Privex/shell-core #
# #
#############################################################

Expand All @@ -25,8 +25,8 @@ export BOLD RED GREEN YELLOW BLUE MAGENTA CYAN WHITE RESET
len() { wc -c <<< "${@:1}"; }

cleanup() {
if ! [ -z ${clonedir+x} ] && [[ $(len "$clonedir") -gt 5 ]]; then
echo "Removing temporary clone folder '$clonedir'..." && rm -rf "$clonedir";
if ! [ -z ${clonedir+x} ] && [[ $(len "$clonedir") -gt 5 ]]; then
echo "Removing temporary clone folder '$clonedir'..." && rm -rf "$clonedir";
fi
}

Expand All @@ -40,7 +40,7 @@ sudo() {
fi
/usr/bin/env sudo "${@:1}"
else
/usr/bin/env "${@:1}" # The user is already root, so just drop the 'sudo' and run it raw.
/usr/bin/env "${@:1}" # The user is already root, so just drop the 'sudo' and run it raw.
fi
}

Expand Down Expand Up @@ -86,8 +86,8 @@ git clone -q https://github.com/Privex/shell-core.git "$clonedir"
echo "${GREEN} -> Using 'run.sh install' to install/update Privex ShellCore${RESET}"
bash "${clonedir}/run.sh" install

if [[ -d "${HOME}/.pv-shcore" ]]; then
echo "source ${HOME}/.pv-shcore/load.sh" > /tmp/pv-shellcore
if [[ -d "${HOME}/.local/share/pv-shcore" ]]; then
echo "source ${HOME}/.local/share/pv-shcore/load.sh" > /tmp/pv-shellcore
elif [[ -d "/usr/local/share/pv-shcore" ]]; then
echo "source /usr/local/share/pv-shcore/load.sh" > /tmp/pv-shellcore
else
Expand Down