shell-function
Instructions how to write: write as functions using 'throw'
Write the request as a function using 'throw' function that is already defined in the environment.
Context: Functions already defined:
interrupt ()
{
echo.red "Interrupting: ${*}" 1>&2;
echo.yellow "FunctionChain: $(function_chain)" 1>&2;
echo.yellow "PWD: $PWD" 1>&2;
echo.yellow "PID : $$" 1>&2;
echo.yellow "BASHPID: $BASHPID" 1>&2;
kill -INT -$$;
exit 1
}
throw ()
{
interrupt "${*}"
}
Instructions how to write: use install_if_missing
Use
install_if_missing
when need to use some CLI tool.
its definition is already in environment:
install_if_missing ()
{
if [[ -z "${1}" ]]; then
gt_print_func_md_doc "${FUNCNAME[0]}";
return 1;
fi;
local WhatToInstall="$1";
local WhatToCheck="$2";
if [[ -z "${WhatToCheck}" ]]; then
WhatToCheck="${WhatToInstall}";
fi;
if ! command -v "${WhatToCheck}" > /dev/null; then
echo "${WhatToCheck} is missing. Installing ${WhatToInstall}...";
if shell.is_command_defined brew; then
echo "Found brew. Using brew to install...";
brew install "${WhatToInstall}" || return 1;
return 0;
fi;
if shell.is_command_defined dnf; then
echo "Found dnf. Using dnf to install...";
sudo dnf install "${WhatToInstall}" || return 1;
return 0;
fi;
if shell.is_command_defined apt-get; then
echo "Found apt-get. Using apt-get to install...";
sudo apt-get install "${WhatToInstall}" || return 1;
return 0;
fi;
echo "Did not find any known installers.";
return 1;
fi
}