Updated to use bashtools

This commit is contained in:
Rob Pearce 2023-05-10 12:28:56 +10:00
parent 9e14f38735
commit a195a48803
1 changed files with 18 additions and 125 deletions

143
gnscli.sh
View File

@ -12,36 +12,11 @@
VER=1.0
SPINNERFRAMES='|/-\'
function start_spinner() {
local idx=0 len spid
(
len=${#SPINNERFRAMES}
echo -n " "
while [ 1 ] ; do
echo -en "\b${SPINNERFRAMES:$idx:1}"
idx=$((idx + 1))
[[ $idx -ge $len ]] && idx=0
sleep 0.1
done
) &
spid=$!
echo $spid >>"$SPINNERFILE"
}
function stop_spinner() {
local f pid
[[ ! -e "$SPINNERFILE" ]] && return 1
while read -r pid ; do
if [[ $pid = *[[:digit:]]* ]]; then
{ kill $pid && wait $pid; } 2>/dev/null
echo -en "\b " >/dev/stderr
fi
done < "$SPINNERFILE"
rm -f "$SPINNERFILE"
}
. ${HOME}/code/bashtools/bashtools.sh
if [[ -z $HAVE_BASHTOOLS ]]; then
echo "ERROR: bashtools not installed download from https://git.nethack.net/rob/bashtools" >/dev/stderr
exit 1
fi
# helper function to turn a directory of scripts into inline
# defaults at the end of this script
@ -120,7 +95,7 @@ function initauth() {
if [[ $doscripts -eq 1 ]]; then
mkscripts "$SCRIPTDIR"
fi
cecho -s "$CYAN" "Initialisation of ^b$CONFDIR^p complete."
csecho "$CYAN" "Initialisation of ^b$CONFDIR^p complete."
return 0
}
@ -343,7 +318,7 @@ function profile() {
local fname
fname=${FUNCNAME[2]}
[[ -z $fname ]] && fname=${FUNCNAME[1]}
[[ $PROFILING -eq 1 ]] && cecho -s "$YELLOW" "^b${FUNCNAME[1]}(): ^p$*" 1>&2
[[ $PROFILING -eq 1 ]] && csecho "$YELLOW" "^b${FUNCNAME[1]}(): ^p$*" 1>&2
}
function harddebug() {
@ -377,80 +352,6 @@ function debug() {
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() {
cecho -s "$CYAN" "^i[$*]" 1>&2
}
function error() {
[[ $innotify -eq 1 ]] && fail
cecho -s "$RED" "^bERROR: ^p$*" 1>&2
}
function notify() {
#echo -en "${NOTIFYCOLB}* ${NOTIFYCOL}$*...${PLAIN} " 1>&2
cecho -n "$NOTIFYCOL" "$NOTIFYCOLB" "^b* ^p$*... " 1>&2
start_spinner &
innotify=1
}
function inform() {
#echo -e "${INFORMCOLB}* ${INFORMCOL}$*${PLAIN} " 1>&2
cecho "$INFORMCOL" "$INFORMCOLB" "^b* ^p$* " 1>&2
}
function ok() {
local msg=${*:-ok}
stop_spinner
[[ $innotify -eq 0 ]] && return 1
innotify=0
echo -e "$GREEN$msg$PLAIN" 1>&2
}
function fail() {
local msg=${*:-failed}
stop_spinner
[[ $innotify -eq 0 ]] && return 1
innotify=0
echo -e "$RED$msg$PLAIN" 1>&2
}
function warn() {
cecho -s "$YELLOW" "^bWarning: ^p$*" 1>&2
}
function usage() {
echo "usage: $0 OPTIONS command"
echo
@ -638,7 +539,7 @@ function loadprojectlist() {
rv=$?
if [[ $rv -ne 0 || -z $jqoutput ]]; then
error "$rv Got bad data from initial project list query to API"
cecho -s "${RED}" "^bErrors:^p"
csecho "${RED}" "^bErrors:^p"
echo -en "${RED}"
cat "${errfile}" | sed -e 's/^/ /'
echo -en "${PLAIN}"
@ -671,11 +572,11 @@ function loadprojectlist() {
else
fail
error "Could not obtain initial project list from API"
cecho -s "${RED}" "^bOutput:^p"
csecho "${RED}" "^bOutput:^p"
echo -en "${RED}"
echo "${output}" | sed -e 's/^/ /'
echo -en "${PLAIN}"
cecho -s "${RED}" "^bErrors:^p"
csecho "${RED}" "^bErrors:^p"
echo -en "${RED}"
cat "${errfile}" | sed -e 's/^/ /'
echo -en "${PLAIN}"
@ -860,8 +761,6 @@ function addcmdoption() {
done
}
function showcmdhelp() {
local real_cname cname idx text ln x format optname_arr optdesc_arr len
local otheraliases
@ -931,7 +830,6 @@ function showcmdhelp() {
echo
}
function getprojidx() { # getprojidx project_name_or_uuid
local x
for x in ${!proj_name[@]}; do
@ -969,7 +867,6 @@ function getalllocs() {
echo ${loc_name[@]}
}
function getfield() { # endpoint jqfilter fieldname
getdata "$1" list -q -e -R"$3" -r -F -f"$2"
}
@ -1175,7 +1072,6 @@ function makejson() { # makejson key1:val1^key2:val2^...
printf "%s\n" '}'
}
function action_needs_curldata() { # actionname
local a
a="$1"
@ -1538,7 +1434,6 @@ function getbasefilter() { # epidx [regexp_filter]
echo "${bf}"
}
function profile_start() {
profile "start profiling"
prof_start=$(($($GDATE +%s%N)/1000))
@ -1862,7 +1757,7 @@ debug -f "--> extrainfo = $extrainfo"
if [[ $errs -gt 0 ]]; then
local fullres_bad
fail
cecho -s "${RED}" "$errs x '^b$actionname^p' actions failed:"
csecho "${RED}" "$errs x '^b$actionname^p' actions failed:"
echo -e "${RED}"
echo "$allbadresults" | sed -e 's/^/ /'
echo -e "${PLAIN}"
@ -1872,7 +1767,7 @@ debug -f "--> extrainfo = $extrainfo"
if [[ $good -ge 1 ]]; then
local fullres
ok
cecho -s "${GREEN}" "$good x '^b$actionname^p' actions submitted successfully."
csecho "${GREEN}" "$good x '^b$actionname^p' actions submitted successfully."
# More than just a heading?
if [[ $(echo -e "$allgoodresults" | wc -l | bc) -gt 1 ]]; then
@ -2075,7 +1970,7 @@ debug all pids finished
rescount=$(printf %d $(echo -n "$all" | wc -c))
if [[ $rescount -eq 0 ]]; then
cecho -s "$YELLOW" " ^b(no results)"
csecho "$YELLOW" " ^b(no results)"
echo
rm -f $TMPDIR/get.*
rm -f $TMPDIR/err.*
@ -2527,8 +2422,6 @@ function convert_link_ids() { # endpoint str
return 0
}
function addline() { # varname "string_to_append"
local newstr varname extra
varname="$1"
@ -2540,8 +2433,6 @@ function addline() { # varname "string_to_append"
eval "$varname=\"${newstr}\""
}
function processcmd() {
local cmd arg newarg rv newlocs x err admin idx opts pipe gotargs
local whattolist actionname="" actionfilter=""
@ -2760,11 +2651,11 @@ debug "post replacedargs is [$replacedargs]"
echo -e "$PLAIN"
res=$(uuid_to_name ${arg})
if [[ $? -eq 0 ]]; then
cecho -s "${CYAN}" " UUID ^b${arg}^p -> ${res}"
csecho "${CYAN}" " UUID ^b${arg}^p -> ${res}"
else
res=$(name_to_uuid ${arg})
if [[ $? -eq 0 ]]; then
cecho -s "${CYAN}" " UUID ${res} -> ^b${arg}^p"
csecho "${CYAN}" " UUID ${res} -> ^b${arg}^p"
else
error "No cache results found for ${arg}"
fi
@ -3366,12 +3257,13 @@ addendpoint projects projects project project_id name
addepalias projects p
addeptitles projects "Project " "UUID" "Status "
addepfields projects ".name" ".project_id" ".status"
addepactions projects open close add del dupe
addepactions projects open close add del dupe mv
addepactionusage projects "add" "new_projectname"
addepactionusage projects "del" "existing_projectname"
addepactionusage projects "open" "[existing_projectname]"
addepactionusage projects "close" "[existing_projectname]"
addepactionusage projects "dupe" "existing_projectname new_projectname"
addepactionusage projects "mv" "old_project_name new_project_name"
addendpoint nodes projects/_CURPROJECT_/nodes node node_id name
addepalias nodes n
@ -3451,6 +3343,7 @@ addcmdalias "delink" "action link del" ""
addcmdalias "padd" "action project add" ""
addcmdalias "pdel" "action project del" ""
addcmdalias "pdupe" "action project dupe" "_CURPROJECT_"
addcmdalias "pmv" "action project mv" ""
addcmd help "List regular commands" 0 "?" "h"
addcmd exit "Exit from gnscli" 0 quit