Skip to content

Instantly share code, notes, and snippets.

@mleko
Last active January 15, 2022 19:50
Show Gist options
  • Save mleko/78f9130bcbb24925b86f6ae42a436246 to your computer and use it in GitHub Desktop.
Save mleko/78f9130bcbb24925b86f6ae42a436246 to your computer and use it in GitHub Desktop.
Dry run and console color helper
#!/bin/bash
# Usage: `. color-run.sh`
# To perform dry run set `EXEC_DRY_RUN=1`
function colors {
# check if stdout is a terminal...
if test -t 1; then
# see if it supports colors...
ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then
ETEXT_BOLD="$(tput bold)"
ETEXT_DIM="$(tput dim)"
ETEXT_UNDERLINE="$(tput smul)"
ETEXT_STANDOUT="$(tput smso)"
ETEXT_NORMAL="$(tput sgr0)"
ETEXT_BLACK="$(tput setaf 0)"
ETEXT_RED="$(tput setaf 1)"
ETEXT_GREEN="$(tput setaf 2)"
ETEXT_YELLOW="$(tput setaf 3)"
ETEXT_BLUE="$(tput setaf 4)"
ETEXT_MAGENTA="$(tput setaf 5)"
ETEXT_CYAN="$(tput setaf 6)"
ETEXT_WHITE="$(tput setaf 7)"
else
ETEXT_BOLD=""
ETEXT_DIM=""
ETEXT_UNDERLINE=""
ETEXT_STANDOUT=""
ETEXT_NORMAL=""
ETEXT_BLACK=""
ETEXT_RED=""
ETEXT_GREEN=""
ETEXT_YELLOW=""
ETEXT_BLUE=""
ETEXT_MAGENTA=""
ETEXT_CYAN=""
ETEXT_WHITE=""
fi
fi
}
function info {
echo "${ETEXT_BOLD}${ETEXT_CYAN}"$@"${ETEXT_NORMAL}";
}
function success {
echo "${ETEXT_BOLD}${ETEXT_GREEN}"$@"${ETEXT_NORMAL}";
}
function error {
echo "${ETEXT_BOLD}${ETEXT_RED}"$@"${ETEXT_NORMAL}";
}
function warning {
echo "${ETEXT_BOLD}${ETEXT_YELLOW}"$@"${ETEXT_NORMAL}";
}
function exec {
echo "${ETEXT_BLUE}"$@"${ETEXT_NORMAL}"
if [ -z "${EXEC_DRY_RUN}" ] || [ "${EXEC_DRY_RUN}" = 0 ]; then
$@
if [ 0 != $? ]; then
error "Script failed; bail";
exit 1;
fi
fi
}
#RUN
colors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment