Tidied up colour code.

This commit is contained in:
Rob Pearce 2022-01-03 16:01:21 +11:00
parent 953d79e3e3
commit d6402bc17e
1 changed files with 78 additions and 34 deletions

112
gnscli.sh
View File

@ -1,14 +1,14 @@
#!/bin/bash
# start/stop/del with multiple args broken
# rename node issue when matching multiple
# rename project
# node stop givesjq error
# connect xxx & [opens iterm (or whatever)]
VER=0.1
VER=1.0
# helper function to turn a directory of scripts into inline
## defaults at the end of this script
@ -87,7 +87,7 @@ function initauth() {
if [[ $doscripts -eq 1 ]]; then
mkscripts "$SCRIPTDIR"
fi
echo -e "$BOLD${CYAN}Initialisation of $BOLD$CONFDIR$PLAIN complete.$PLAIN"
cecho -s "$CYAN" "Initialisation of ^b$CONFDIR^p complete."
return 0
}
@ -217,7 +217,7 @@ function getstr() { # getstr last_prompt_char default_answer "prompt string"
shift
prompt="$*"
retstr=""
echo -n -e "${PURPLE}$prompt [$BOLD$def$PLAIN$PURPLE]${lastchar}${PLAIN} "
echo -n -e "${NOTIFYNDCOL}$prompt [${NOTIFYNDCOLB}$def$NOTIFYNDCOL]${lastchar}${PLAIN} "
read retstr
echo -e "$PLAIN"
[[ -z $retstr ]] && retstr="$def"
@ -276,6 +276,7 @@ function getcmd() {
pstr="gnscli> "
[[ ! -z $CACHING ]] && info "$CACHING"
set -f
builtin read -p "$(echo -en $BOLD)[$(echo -en $PLAIN$pcol)${prefix}$(echo -e "$PLAIN$BOLD")] $pstr$(echo -e "${PLAIN}")" -e -r input
set +f
@ -306,7 +307,7 @@ function profile() {
local fname
fname=${FUNCNAME[2]}
[[ -z $fname ]] && fname=${FUNCNAME[1]}
[[ $PROFILING -eq 1 ]] && echo -e "${YELLOW}${BOLD}${FUNCNAME[1]}(): ${PLAIN}${YELLOW}$*${PLAIN}" 1>&2
[[ $PROFILING -eq 1 ]] && cecho -s "$YELLOW" "^b${FUNCNAME[1]}(): ^p$*" 1>&2
}
function harddebug() {
@ -321,29 +322,64 @@ function debug() {
fi
if [[ $VERBOSE -eq 1 ]]; then
if [[ $force -eq 1 ]]; then
echo -e "${PURPLE}${BOLD}${FUNCNAME[1]}(): ${PLAIN}${PURPLE}$*${PLAIN}" >/dev/stderr
echo -e "${NOTIFYCOLB}${FUNCNAME[1]}(): ${NOTIFYCOL}$*${PLAIN}" >/dev/stderr
else
echo -e "${PURPLE}${BOLD}${FUNCNAME[1]}(): ${PLAIN}${PURPLE}$*${PLAIN}" 1>&2
echo -e "${NOTIFYCOLB}${FUNCNAME[1]}(): ${NOTIFYCOL}$*${PLAIN}" 1>&2
fi
fi
}
# 'str' can contain:
# ^b bold
# ^i italic
# ^p plain
#
function cecho() { # [-n] [-s] col boldcol "str"
local col bcol str minusn="" autobold=0
while [[ ${1:0:1} == "-" ]]; do
if [[ $1 == "-n" ]]; then
minusn="-n"
fi
if [[ $1 == "-s" ]]; then
autobold=1
fi
shift
done
col="$1"
shift
if [[ $autobold -eq 1 ]]; then
bcol="$col$BOLD"
else
bcol="$1"
shift
fi
str="$*"
str=${str//\^b/${bcol}}
str=${str//\^p/${PLAIN}${col}}
str=${str//\^i/${col}${ITALIC}}
echo $minusn -e "${col}${str}${PLAIN}" 1>&2
}
function info() {
echo -e "${CYAN}${BOLD}${ITALIC}[$*]${PLAIN}" 1>&2
cecho -s "$CYAN" "^i[$*]" 1>&2
}
function error() {
[[ $innotify -eq 1 ]] && fail
echo -e "${RED}${BOLD}ERROR: ${PLAIN}${RED}$*${PLAIN}" 1>&2
cecho -s "$RED" "^bERROR: ^p$*" 1>&2
}
function notify() {
echo -en "${PURPLE}${BOLD}* ${PLAIN}${PURPLE}$*...${PLAIN} " 1>&2
#echo -en "${NOTIFYCOLB}* ${NOTIFYCOL}$*...${PLAIN} " 1>&2
cecho -n "$NOTIFYCOL" "$NOTIFYCOLB" "^b* ^p$*... " 1>&2
innotify=1
}
function notify_nodots() {
echo -e "${PURPLE}${BOLD}* ${PLAIN}${PURPLE}$*${PLAIN} " 1>&2
#echo -e "${NOTIFYNDCOLB}* ${NOTIFYNDCOL}$*${PLAIN} " 1>&2
cecho "$NOTIFYNDCOL" "$NOTIFYNDCOLB" "^b* ^p$* " 1>&2
}
function ok() {
@ -361,7 +397,7 @@ function fail() {
}
function warn() {
echo -e "${YELLOW}${BOLD}Warning: ${PLAIN}${YELLOW}$*${PLAIN}" 1>&2
cecho -s "$YELLOW" "^bWarning: ^p$*" 1>&2
}
function usage() {
@ -1516,8 +1552,8 @@ function runaction() { # runaction <nodes|vms|etc> <actionname> targetlist optio
local actionname line all
local force=0 foreground=0 f rv files n thisfile jqres goterror
local good allgoodresults errs allbadresults
# oooo change this
local jqf
# oooo change this
local jqf_bad='[ "_DC_", "_OB_", .fault.detail, .fault.reason, .status ] | @csv'
local objecttype extrainfo
@ -1596,7 +1632,6 @@ debug -f "--> extrainfo = $extrainfo"
objecttype=$(echo ${what:0:1} | tr '[a-z]' '[A-Z]')${what:1}
[[ $objecttype == "Model" ]] && objecttype="Node"
# oooo
# figure out jq based on action
if [[ $actionname == "add" && $endpoint == "model" ]]; then
allgoodresults="Server,${objecttype},Ports"
@ -1663,7 +1698,7 @@ debug -f "--> extrainfo = $extrainfo"
echo
if [[ $errs -gt 0 ]]; then
local fullres_bad
echo -e "${RED}$errs x '${BOLD}$actionname${PLAIN}${RED}' actions failed:${PLAIN}"
cecho -s "${RED}" "$errs x '^b$actionname^p' actions failed:"
echo -e "${RED}"
echo "$allbadresults" | sed -e 's/^/ /'
echo -e "${PLAIN}"
@ -1672,7 +1707,7 @@ debug -f "--> extrainfo = $extrainfo"
if [[ $good -ge 1 ]]; then
local fullres
echo -e "${GREEN}$good x '${BOLD}$actionname${PLAIN}${GREEN}' actions submitted successfully.${PLAIN}"
cecho -s "${GREEN}" "$good x '^b$actionname^p' actions submitted successfully."
# More than just a heading?
if [[ $(echo -e "$allgoodresults" | wc -l | bc) -gt 1 ]]; then
@ -1875,7 +1910,7 @@ debug all pids finished
rescount=$(printf %d $(echo -n "$all" | wc -c))
if [[ $rescount -eq 0 ]]; then
echo -e " ${BOLD}${YELLOW}(no results)${PLAIN}"
cecho -s "$YELLOW" " ^b(no results)"
echo
rm -f $TMPDIR/get.*
rm -f $TMPDIR/err.*
@ -1958,7 +1993,7 @@ debug "dotscript:\n${dotscript}"
greenwords="true up enabled"
redwords="false down paused uninitialized disabled"
yellowwords="migrating"
uuidcol="$PURPLE"
uuidcol="$NOTIFYCOL"
while read -r line ; do
key=${line%%:*}
@ -2193,7 +2228,7 @@ debug "alluuids is $alluuids"
if [[ $nobs -gt 1 && $multiallowed -eq 0 ]]; then
debug "actionfilter=$actionfilter"
notify_nodots "'${BOLD}${actionfilter}${PLAIN}${PURPLE}' matched multiple ${whattolist}s"
notify_nodots "'${NOTIFYNDCOLB}${actionfilter}${NOTIFYNDCOL}' matched multiple ${whattolist}s"
narrowdown "$whattolist" "$allobs" "$alluuids" selobname selobuuid || rv=1
else
selobname="$allobs"
@ -2502,11 +2537,11 @@ debug "post replacedargs is [$replacedargs]"
echo -e "$PLAIN"
res=$(uuid_to_name ${arg})
if [[ $? -eq 0 ]]; then
echo -e "${CYAN} UUID ${BOLD}${arg}${PLAIN}${CYAN} -> ${res}"
cecho -s "${CYAN}" " UUID ^b${arg}^p -> ${res}"
else
res=$(name_to_uuid ${arg})
if [[ $? -eq 0 ]]; then
echo -e "${CYAN} UUID ${res} -> ${BOLD}${arg}${PLAIN}"
cecho -s "${CYAN}" " UUID ${res} -> ^b${arg}^p"
else
error "No cache results found for ${arg}"
fi
@ -2578,7 +2613,7 @@ debug "post replacedargs is [$replacedargs]"
else
debug "newnodetype is $newnodetype"
debug "newnodeuuid is $newnodetype_uuid"
notify_nodots "Adding a new ${BOLD}$newnodetype${PLAIN}${PURPLE} named $BOLD$newname${PLAIN}"
notify_nodots "Adding a new ${NOTIFYNDCOLB}$newnodetype${NOTIFYNDCOL} named ${NOTIFYNDCOLB}$newname${PLAIN}"
confirm=1
fi
elif [[ $endpoint == "link" ]]; then
@ -2619,7 +2654,7 @@ debug "adapnumlist is $adapnumlist"
debug "${letter}-node ${dev[$x]} port ${port[$x]} is OK (${dev[$x]})"
fi
done
notify_nodots "Adding a link from ${BOLD}${dev[0]} ${port[0]}${PLAIN}${PURPLE} to ${BOLD}${dev[1]} ${port[1]}${PLAIN}"
notify_nodots "Adding a link from ${NOTIFYNDCOLB}${dev[0]} ${port[0]}${NOTIFYNDCOL} to ${NOTIFYNDCOLB}${dev[1]} ${port[1]}${PLAIN}"
confirm=1
fi
else # action is not add/connect
@ -2644,7 +2679,7 @@ debug "adapnumlist is $adapnumlist"
else
textdata="$data"
fi
echo -e "${PURPLE}About to run '${BOLD}$actionname${PLAIN}${PURPLE}' on ${BOLD}${nobs}${PLAIN}${PURPLE} ${whattolist}${ob_ess} on ${BOLD}${ndcs}${PLAIN}${PURPLE} server${dc_ess}:${PLAIN}"
cecho "$NOTIFYNDCOL" "$NOTIFYNDCOLB" "About to run '^b$actionname^p' on ^b${nobs}^p ${whattolist}${ob_ess} on ^b${ndcs}^p server${dc_ess}:"
echo -e "$textdata" | awk -F, "BEGIN {lastdc=\"\"} { if (\$1 != lastdc) { print \" ${YELLOW}- ${BOLD}\" \$1 \" -> project=${curproj}${PLAIN}\"; lastdc=\$1; } print \" ${YELLOW}- \" \$2 \"${PLAIN}\"}"
echo
@ -2654,7 +2689,7 @@ debug "adapnumlist is $adapnumlist"
else
local confirmcode entered_string
confirmcode=$(generate_random_string)
notify_nodots "Confirmation code is: ${BOLD}${confirmcode}${PLAIN}"
notify_nodots "Confirmation code is: ${NOTIFYDCOLBOLD}${confirmcode}${NOTIFYDCOL}"
getstr ":" "n" "Enter the above code to proceed, anything else will abort"
entered_string="$retstr"
[[ $entered_string == $confirmcode ]] && confirm=1 || confirm=0
@ -2991,9 +3026,18 @@ GREEN="\033[32m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
ORANGE="\033[38;2;255;165;0m"
PURPLE="\033[38;2;200;0;200m"
#PURPLE="\033[35m"
ORANGE="${PLAIN}\033[38;2;255;165;0m"
ORANGEBOLD="${BOLD}\033[38;2;255;220;0m"
MAGENTARGB="${PLAIN}\033[38;2;208;65;126m"
MAGENTARGBBOLD="${BOLD}\033[38;2;255;135;196m"
NOTIFYNDCOL="$ORANGE"
NOTIFYNDCOLB="$ORANGEBOLD"
NOTIFYCOL="$MAGENTARGB"
NOTIFYCOLB="$MAGENTARGBBOLD"
GREY="\033[38;2;110;110;110m"
LINK="$BLUE$UNDERLINE"
@ -3013,7 +3057,7 @@ CURLERRORSTRINGS="(^40.:|error|status.*40.)"
UUIDCHAR="[0-9a-f]"
UUID_REGEXP=${UUIDCHAR}\{8\}-${UUIDCHAR}\{4\}-${UUIDCHAR}\{4\}-${UUIDCHAR}\{4\}-${UUIDCHAR}\{12\}
WORDCOLOURS=( "${GREEN}" "${RED}" "${YELLOW}" )
UUIDCOL="$PURPLE"
UUIDCOL="$NOTIFYCOL"
COLOURED_WORDS=( "true\|up\|enabled\|complete\|finished\|started\|opened" "false\|down\|paused\|uninitialized\|disabled\|failed\|stopped\|closed" "migrating\|powering_up\|project_closed\|unknown" )
ADD_COLOURS=""
for colidx in ${!WORDCOLOURS[@]}; do
@ -3210,7 +3254,7 @@ cat "$errfile"
rv=$?
if [[ $rv -ne 0 || -z $jqoutput ]]; then
error "$rv Got bad data from initial project list query to API"
echo -e "${RED}${BOLD}Errors:${PLAIN}"
cecho -s "${RED}" "^bErrors:^p"
echo -en "${RED}"
cat "${errfile}" | sed -e 's/^/ /'
echo -en "${PLAIN}"
@ -3227,11 +3271,11 @@ cat "$errfile"
else
fail
error "Could not obtain initial project list from API"
echo -e "${RED}${BOLD}Output:${PLAIN}"
cecho -s "${RED}" "^bOutput:^p"
echo -en "${RED}"
echo "${output}" | sed -e 's/^/ /'
echo -en "${PLAIN}"
echo -e "${RED}${BOLD}Errors:${PLAIN}"
cecho -s "${RED}" "^bErrors:^p"
echo -en "${RED}"
cat "${errfile}" | sed -e 's/^/ /'
echo -en "${PLAIN}"
@ -3253,7 +3297,7 @@ dumpmsgq
trap cleanup EXIT
echo -e "${UNDERLINE}Unofficial GNS3 cli v${VER}${PLAIN}"
echo -e "${ITALIC}${YELLOW}Note: this script is still in development and may have bugs!${PLAIN}"
#echo -e "${ITALIC}${YELLOW}Note: this script is still in development and may have bugs!${PLAIN}"
if [[ $# -gt 0 ]]; then
processcmd $*