Adding colored output support (outColor)
Extended seq template
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
## Version information
|
||||
|
||||
VERSION_REV=12
|
||||
VERSION_MAJOR=0
|
||||
VERSION_MAJOR=1
|
||||
VERSION_MINOR=0
|
||||
|
||||
## Start of generic script part
|
||||
@@ -32,6 +32,25 @@ TEMPLATE_NAME=seqTemplateExample.sh
|
||||
MISSING_CONF=missingConf.log
|
||||
VERSION_STRING="${VERSION_REV}.${VERSION_MAJOR}.${VERSION_MINOR}"
|
||||
|
||||
BBLACK= ; [ -t 1 ] && BBLACK='\033[40m'
|
||||
BLACK= ; [ -t 1 ] && BLACK='\033[0;30m'
|
||||
DARKGRAY= ; [ -t 1 ] && DARKGRAY='\033[1;30m'
|
||||
RED= ; [ -t 1 ] && RED='\033[0;31m'
|
||||
LIGHTRED= ; [ -t 1 ] && LIGHTRED='\033[1;31m'
|
||||
GREEN= ; [ -t 1 ] && GREEN='\033[0;32m'
|
||||
LIGHTGREEN= ; [ -t 1 ] && LIGHTGREEN='\033[1;32m'
|
||||
ORANGE= ; [ -t 1 ] && ORANGE='\033[0;33m'
|
||||
YELLOW= ; [ -t 1 ] && YELLOW='\033[1;33m'
|
||||
BLUE= ; [ -t 1 ] && BLUE='\033[0;34m'
|
||||
LIGHTBLUE= ; [ -t 1 ] && LIGHTBLUE='\033[1;34m'
|
||||
PURPLE= ; [ -t 1 ] && PURPLE='\033[0;35m'
|
||||
LIGHTPURPLE= ; [ -t 1 ] && LIGHTPURPLE='\033[1;35m'
|
||||
CYAN= ; [ -t 1 ] && CYAN='\033[0;36m'
|
||||
LIGHTCYAN= ; [ -t 1 ] && LIGHTCYAN='\033[1;36m'
|
||||
LIGHTGRAY= ; [ -t 1 ] && LIGHTGRAY='\033[0;37m'
|
||||
WHITE= ; [ -t 1 ] && WHITE='\033[1;37m'
|
||||
NC= ;[ -t 1 ] && NC='\033[0m' # No Color
|
||||
|
||||
helpSequencer() {
|
||||
cat <<USAGE_EOF
|
||||
Usage: ${0##*/} [OPTIONS] [STEP NUMBER(s) or ALIAS] [STEP ARGUMENTS]
|
||||
@@ -103,19 +122,24 @@ sequencer.sh global variables:
|
||||
Profile string selected with -p argument
|
||||
|
||||
sequencer.sh build-in functions:
|
||||
|
||||
exe [COMMANDLINE]
|
||||
USAGE_API
|
||||
echo -e "${GREEN} exe [COMMANDLINE]${NC}"
|
||||
cat <<USAGE_API
|
||||
Execute command line without pipes or redirects (>,<,|).
|
||||
Supporting: dry-run (-d): only print command without execution
|
||||
verbose (-v): print command before execution
|
||||
|
||||
exep "[COMMANDLINE]"
|
||||
USAGE_API
|
||||
echo -e "${GREEN} exep \"[COMMANDLINE]\"${NC}"
|
||||
cat <<USAGE_API
|
||||
See exe, but support for pipes or redirects.
|
||||
Important:
|
||||
- Shell commands cd, read, ... won't work because COMMANDLINE is started in a new shell.
|
||||
- All apostrophes need to be esacped since the command line is given as string.
|
||||
|
||||
initSeqConfig [OPTION] <NAME> [TEMPLATE]
|
||||
USAGE_API
|
||||
echo -e "${GREEN} initSeqConfig [OPTION] <NAME> [TEMPLATE]${NC}"
|
||||
cat <<USAGE_API
|
||||
Create a configuration file in $SEQ_CONFIG_HOME/ and source it if already existent.
|
||||
[OPTION]
|
||||
-p : Use profiles
|
||||
@@ -128,7 +152,9 @@ sequencer.sh build-in functions:
|
||||
2 : created empty configuration
|
||||
3 : No configuration created
|
||||
|
||||
addConf <OPTIONS> [SOURCE TYPE] <SOURCE> <DESTINATION FILE>
|
||||
USAGE_API
|
||||
echo -e "${GREEN} addConf <OPTIONS> [SOURCE TYPE] <SOURCE> <DESTINATION FILE>${NC}"
|
||||
cat <<USAGE_API
|
||||
Trying to write or append text or a file (<SOURCE>) to a destination file.
|
||||
If the CONFIGFILE exists, a backup (name_%Y%m%d-%H%M%S.bck) is saved at the same location.
|
||||
If -s fails or -m, "$(realpath "$MISSING_CONF")" is created with the conflicts
|
||||
@@ -145,30 +171,58 @@ sequencer.sh build-in functions:
|
||||
<DESTINATION FILE>
|
||||
Target file to be created or modified.
|
||||
|
||||
step <STEP NUMBER OR ALIAS>
|
||||
USAGE_API
|
||||
echo -e "${GREEN} step <STEP NUMBER OR ALIAS>${NC}"
|
||||
cat <<USAGE_API
|
||||
Executes a single step also by alias. Useful if step numbers get reorganized.
|
||||
dry-run is not applied in this function! The executed step is responsible.
|
||||
|
||||
echoerr [...]
|
||||
USAGE_API
|
||||
echo -e "${GREEN} outColor [FOREGROUND COLOR] [BACKGROUND COLOR]${NC}"
|
||||
cat <<USAGE_API
|
||||
Set output color permanently until reset.
|
||||
No argument or unknown foreground color restores shell default (reset).
|
||||
Color reset happens after every step and step_info function call.
|
||||
[COLOR]: black, red, green, yellow, blue, magenta, cyan, white
|
||||
|
||||
USAGE_API
|
||||
echo -e "${GREEN} echoerr [...]${NC}"
|
||||
cat <<USAGE_API
|
||||
echo to stderr
|
||||
[...] : all parameter are forwarded to echo
|
||||
|
||||
echoinfo [...]
|
||||
USAGE_API
|
||||
echo -e "${GREEN} echoseq [...]${NC}"
|
||||
cat <<USAGE_API
|
||||
echo to stdout if sequencer output is not suppressed
|
||||
[...] : all parameter are forwarded to echo
|
||||
|
||||
USAGE_API
|
||||
echo -e "${GREEN} echoinfo [...]${NC}"
|
||||
cat <<USAGE_API
|
||||
echo additional correctly indented line to step info
|
||||
[...] : all parrameter are forwared to echo
|
||||
|
||||
endCheckEmpty <VARIABLENAME> [DESCRIPTION]
|
||||
USAGE_API
|
||||
echo -e "${GREEN} endCheckEmpty <VARIABLENAME> [DESCRIPTION]${NC}"
|
||||
cat <<USAGE_API
|
||||
exit 666 if variable is empty
|
||||
<VARIABLENAME> : Name used within eval
|
||||
[DESCRIPTION] : Additional text for error output
|
||||
|
||||
saveReturn [ERRORCODE]
|
||||
USAGE_API
|
||||
echo -e "${GREEN} saveReturn [ERRORCODE]${NC}"
|
||||
cat <<USAGE_API
|
||||
Save ERRORCODE if it is != 0 for later use with endReturn
|
||||
|
||||
getReturn
|
||||
USAGE_API
|
||||
echo -e "${GREEN} getReturn${NC}"
|
||||
cat <<USAGE_API
|
||||
Return last saved error code
|
||||
|
||||
endReturn [OPTIONS] [MESSAGE]
|
||||
USAGE_API
|
||||
echo -e "${GREEN} endReturn [OPTIONS] [MESSAGE]${NC}"
|
||||
cat <<USAGE_API
|
||||
Notifys user that there was an error (previously saved by saveReturn,
|
||||
or -o [ERRORCODE]) and asks to continue or end the sequence.
|
||||
Always exits with evaluated error code.
|
||||
@@ -181,9 +235,57 @@ sequencer.sh build-in functions:
|
||||
USAGE_API
|
||||
}
|
||||
|
||||
# Echo only if not -qq ($QUIET -ne 2)
|
||||
echoseq() { [ $QUIET -ne 2 ] && echo "$@"; }
|
||||
|
||||
# Echo to stderr
|
||||
echoerr() { >&2 echo "$@"; }
|
||||
|
||||
# outColor <FOREGROUND COLOR> [BACKGROUND COLOR]
|
||||
outColor() {
|
||||
[ ! -t 1 ] && return 0
|
||||
case "$1" in
|
||||
black)
|
||||
tput setaf 0;;
|
||||
red)
|
||||
tput setaf 1;;
|
||||
green)
|
||||
tput setaf 2;;
|
||||
yellow)
|
||||
tput setaf 3;;
|
||||
blue)
|
||||
tput setaf 4;;
|
||||
magenta)
|
||||
tput setaf 5;;
|
||||
cyan)
|
||||
tput setaf 6;;
|
||||
white)
|
||||
tput setaf 7;;
|
||||
*)
|
||||
tput sgr0
|
||||
return;;
|
||||
esac
|
||||
|
||||
case "$2" in
|
||||
black)
|
||||
tput setab 0;;
|
||||
red)
|
||||
tput setab 1;;
|
||||
green)
|
||||
tput setab 2;;
|
||||
yellow)
|
||||
tput setab 3;;
|
||||
blue)
|
||||
tput setab 4;;
|
||||
magenta)
|
||||
tput setab 5;;
|
||||
cyan)
|
||||
tput setab 6;;
|
||||
white)
|
||||
tput setab 7;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Echo additional line to info correctly indented
|
||||
INDENT_HELP=' : '
|
||||
INDENTAPPEND_HELP=' '
|
||||
@@ -355,7 +457,7 @@ initSeqConfig() {
|
||||
SEQ_CONFIG_HOME="$configDir"
|
||||
|
||||
if [ -s "$configLoc" ] ; then
|
||||
if [ $QUIET -ne 2 ] ; then echo " [I] Using configuration file: $configLoc" ; fi
|
||||
[ $QUIET -ne 2 ] && echo " [I] Using configuration file: $configLoc"
|
||||
SEQ_CONFIG_FILE="$configLoc"
|
||||
. "$configLoc"
|
||||
return 0
|
||||
@@ -546,6 +648,7 @@ execute() {
|
||||
existsFunction step_${1}_info
|
||||
if [ $? -eq 0 ] ; then
|
||||
step_${1}_info $1 "${STEP_ARGS[@]}"
|
||||
outColor
|
||||
else
|
||||
# Add newline if no info is given
|
||||
echo
|
||||
@@ -579,6 +682,7 @@ execute() {
|
||||
step_$1 $1 "${STEP_ARGS[@]}"
|
||||
STEP_RETURN=$?
|
||||
fi
|
||||
outColor
|
||||
}
|
||||
|
||||
# checkStep <Step Number or Alias>
|
||||
@@ -629,6 +733,7 @@ step() {
|
||||
else
|
||||
step_$stepNo "${stepArgs[@]}"
|
||||
fi
|
||||
outColor
|
||||
}
|
||||
|
||||
# continous <Starting Step Number>
|
||||
@@ -714,6 +819,9 @@ step_config() {
|
||||
#initSeqConfig -p "\$SCRIPT_NAME" "\$CONFIG_FILE_TEMPLATE"
|
||||
#if [ \$? -eq 0 ] ; then
|
||||
# CONFIG=1
|
||||
#else
|
||||
# # End if no configuration file exists
|
||||
# [ \$DRY -eq 0 ] && exit 1
|
||||
#fi
|
||||
}
|
||||
|
||||
@@ -828,7 +936,7 @@ displayHelp() {
|
||||
existsFunction step_${i}_alias
|
||||
if [ $? -eq 0 ] ; then
|
||||
step_${i}_alias
|
||||
echo " = $ALIAS"
|
||||
echo -e " = ${ORANGE}$ALIAS${NC}"
|
||||
printf '%s' "$INDENT_HELP"
|
||||
else
|
||||
echo -n " : "
|
||||
@@ -841,6 +949,7 @@ displayHelp() {
|
||||
else
|
||||
echo " - step_${i}_info() missing"
|
||||
fi
|
||||
outColor
|
||||
done
|
||||
echo
|
||||
fi
|
||||
|
Reference in New Issue
Block a user