This commit is contained in:
dig 2021-09-30 16:01:16 +02:00
parent de980bb045
commit ff50b3c280
10 changed files with 793 additions and 0 deletions

30
icons.sh Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env bash
_nesto_dir="$(dirname $(readlink -f $0))"
source "$_nesto_dir/types.sh"
Array clock_anim=(🕐 🕑 🕒 🕓 🕔 🕕 🕖 🕗 🕘 🕙 🕚 🕛 🕜 🕝 🕞 🕟 🕠 🕡 🕢 🕣 🕤 🕥 🕦 🕧)
Map status_icons=([running]=[stopped]=[template]="☢️ " [other]="✔️☑️🔘⚪⚫🔴🔵🔺🔻🔸🔹🔶🔷🔳🔲▪️▫️◾◽◼️⬛⬜☢️📻⏰⌛️⏳📡🔋🔌💡💰🔧🔑✉️📩📨📧💌📥📤📦📜📃📄📑📊📈📉📆📅📇📋📁📂📰📓📔📚📖🔖🔗📎📐📏📌📍📝🔍🔎🔏🔐🔒🔓🎭🎨🎬🎤🎧🎼🎹🎷🎺🎸🎻🎲♟🎯🎮🔈🔇🔉🔊🔔🔕📣📢💬💭♠️♣️♥️♦️" )
Map status_colors=([running]=$c_green [stopped]=$c_dark$c_red [template]=$c_yellow )
spinning=""
s=1
spinner()
{
s=$[$s+1]
local i=$[$s%100]
if [[ -n $spinning ]]
then
if [[ $i == 0 ]]
then
printf "${clock_anim[$1]} $s\r"
[[ $1 < ${#clock_anim[@]} ]] && spinner $[$1+1] || spinner 1
else
spinner $1
fi
fi
}
spinning=true
spinner

23
model.awk Executable file
View File

@ -0,0 +1,23 @@
#!/usr/bin/awk -f
NR==1 {}
NR==2 {
head=""
color=""
format=""
for(i=1; i<=NF; i++)
{
name=$i
col=$i
fmt=$i
gsub( /\|.*$/,"", name )
head=head name"="i";"
# gsub( /^name/, "", col )
# gsub( /%.*$/, "", col )
# color=color"c_"name"=\""col"\";"
gsub( /^[^|]*\|/, "", fmt )
format=format"f"name"=\""fmt"\";"
}
print "NR==1{} NR==2{"head" "format"}"
}

18
model2.awk Normal file
View File

@ -0,0 +1,18 @@
#!/usr/bin/awk -f
NR==1 {}
NR==2 {
names=""
colors=""
formats=""
for(i=1; i<=NF; i++)
{
name=$i
col=$i
fmt=$i
gsub(/%[\-0-9.*a-z]*\s/,"",name)
head=head$i"="i";"
gsub(/^[^%]*%/,"%",col)
format=format"f"$i"=\""col"\";"
}
print "NR==1{} NR==2{"head" "format"}"
}

276
nesto Executable file
View File

@ -0,0 +1,276 @@
#!/usr/bin/env bash
_nesto_dir="$(dirname $(readlink -f $0))"
#echo "$_nesto_dir/types.sh"
source "$_nesto_dir/types.sh"
_model="$(dirname $0)/model.awk"
echo args: $0 $@
#source easyoptions
_DB="$1"
# sawk '$name=/dig$/i'
# sawk dig
# sawk '$num>150'
search()
{
autoload
DB=~/dev/nesto/DB
# Find if 1st arg is a number
case $1 in
''|*[!0-9]*) re=$1 ;;
*) re="^$1 " ;;
esac
columns="$(for col in ${@:2}; do printf "\$$col," | sed 's/%.*,//'; done | sed 's/,$//')"
# columns=$(echo ${@:2} | awk 'BEGIN{RS=" ";ORS=",o"}{ print "$"$0 }')
echo "(${@:2})"
echo "[$columns]"
format="$(for col in ${@:2}; do printf "cpurple f$col\" \""; done)"
echo "{$format}"
echo $($_model $DB)" NR > 1 && /$re/i {printf $format, $columns}" $vmdb
awk "$($_model $DB) NR > 1 && tolower(\$0) ~ /$re/ {printf $format\"\\n\", $columns}" cpurple=$c_purple c=$c_ $vmdb
#'{printf "%-10s %-30s %-10s\n", cp $1, cs $2, c ct $4 c }' cs=$c_status ct=$c_type cp=$c_purple cg=$c_green gr=$c_white cd=$c_dark c=$c_
}
each()
{
Object arr=$(exp $1)
String tpl="$(printf "$2" | sed 's/"/\"/')"
# debug arr tpl
for i in "${!arr[@]}"; do eval "echo -ne \"$tpl\""; done
}
#tests=(aze qsd wxc)
#each tests '
# $i) $c_cyan${tests[$i]}$c_'
c0=$c_light$c_black
Map status_icons=([running]=⚫ [stopped]=⚪ [template]="☢️ " )
#[other]="✔️☑️🔘⚪⚫🔴🔵🔺🔻🔸🔹🔶🔷🔳🔲▪️▫️◾◽◼️⬛⬜☢️📻⏰⌛️⏳📡🔋🔌💡💰🔧🔑✉️📩📨📧💌📥📤📦📜📃📄📑📊📈📉📆📅📇📋📁📂📰📓📔📚📖🔖🔗📎📐📏📌📍📝🔍🔎🔏🔐🔒🔓🎭🎨🎬🎤🎧🎼🎹🎷🎺🎸🎻🎲♟🎯🎮🔈🔇🔉🔊🔔🔕📣📢💬💭♠️♣️♥️♦️" )
Map status_colors=([running]=$c_green [stopped]=$c_dark$c_red [template]=$c_yellow )
tester()
{
# eval "declare -A vm="${1#*=}
Map vm=$1
Array domains=${vm[domains]}
debug vm domains
echo -e "$c0
┌───┤ ${status_colors[${vm[status]}]}${status_icons[${vm[status]}]}$c0${c_light}VM n°$c_purple${vm[num]}$c0 ├────────────────────┐
│ │
│ VM n°$c_red${vm[num]}$c0
│ Name: $c_green${vm[name]}$c0
│ Status: ${status_colors[${vm[status]}]}${vm[status]}$c0
│ Type: $c_yellow${vm[type]}$c0
│ Domains: $(each domains '
│ $i) $c_cyan${domains[$i]}$c0'
)
│ │
└─────────────────────────────────────┘
"$c_
}
inter()
{
Map vm=$1
Array domains=${vm[domains]}
debug vm domains
whiptail --title "VM n°${vm[num]}" \
--msgbox " │
VM n°$c_red${vm[num]}
Name: $c_green${vm[name]}
Status: ${vm[status]}
Type: ${vm[type]}
Domains: $(each domains '
$i) ${domains[$i]}'
)
" 16 78
#--menu "Search for 'dig' \nMatched Vm(s)" 16 78 10 aze "" qsd "" wxc ""
}
compact()
{
Map vm=$1
Array domains=${vm[domains]}
# debug vm domains
local c0=${status_colors[${vm[status]}]}
echo -e "$c0$c_light${status_colors[${vm[status]}]}${status_icons[${vm[status]}]}$c0$c_purple${vm[num]}$c0 $(printf '%-30s' "${vm[name]}") $c_yellow${vm[type]}"$c_
}
_VM()
{
printf "([num]=$1 [name]=$2 [status]=$3 [type]=$4)"
}
array2map "(num* name status type)" "(111 dig-docker running KVM)"
model=(key* value)
array2map "`exp model`" "(active true)"
#alias VM='declare -A '
#VM vm_temp= "(111 dig-docker running KVM)"
sVM="111 dig-docker running KVM"
_VM 111 dig-docker running KVM
_VM $sVM
Map vm1=`_VM $sVM`
exp vm1
echo ho ho ho nesto
_db2array()
{
local sArr="($(awk '$0 !~ /^#!/ {printf "\"%s\" ",$0}' $1))"
# debug sArr
Array lines=$sArr
# debug lines
# debug lines[0]
# Map cols=${arr[0]}
# debug cols
# echo "${lines[0]}"
Array columns_line=(${lines[0]})
# debug columns_line
Array columns_order=()
for i in ${!columns_line[@]}
do
# echo "(${columns_line[$i]})"
Object field="(${columns_line[$i]})"
# exp field
# echo ${!field[@]}
columns_order[$i]=${!field[@]}
done
Map columns="(${lines[0]})"
columns[_order]="(${columns_order[@]})"
# eval "Map cols=( ${_columns_line[@]} )"
# debug columns columns_order
Array DB=("`exp columns`")
for line in "${lines[@]:1}"
do
line=$(echo "$line" | sed 's/(/"(/' | sed 's/)/)"/')
Object item=$(array2map "${columns[_order]}" "(${line})")
# debug item
DB+=("$(exp item)")
done
# debug DB
exp DB
}
render()
{
_db2array $_nesto_dir/new-test.db
Array DB1=$(_db2array $_nesto_dir/new-test.db)
debug DB1
for i in ${!DB1[@]}
do
[[ $i == 0 ]] && continue
# Map first=${DB1[$i]}
# debug first
$1 "${DB1[$i]}"
done
}
create()
{
fields=($@)
echo $0 laaaa
#eval ``
# echo "#!/home/dig/dev/nesto/nesto"
echo "#!/usr/bin/env nestor"
for i in ${!fields[@]}
do
case ${fields[$i]} in
\[*\]) printf %s "${fields[$i]}= " ;;
\[*) printf %s "${fields[$i]} " ;;
[^[]*) printf %s "[${fields[$i]}]= " ;;
[^[\]]=*) printf "[%s]=%s " "$(echo ${fields[$i]} | awk -F= '{print $1}')" "$(echo ${fields[$i]} | awk -F= '{print $1}')" ;;
*) printf %s "[errors]=\"(\\\"Field not well formated: ${fields[$i]}\\\")\" " ;;
esac
done
}
show()
{
if [ -n $DB ]
then
cat $DB
fi
}
add()
{
Array items=("$@")
exp items
for arg in "$@"
do
Object item=$arg #{items[$i]}
Array aItem=()
exp item
for col in "${columns[@]}"
do
local val="${item[$col]}"
[[ -z "$val" ]] && val="-"
aItem+="$val "
done
exp aItem
echo "${aItem[@]}" >> $DB
done
}
[[ -t 1 ]] && isPiped="" || isPiped=true
hasPipe=""
pipe=""
if [ -t 0 ]; then
echo running interactivelly
else
hasPipe=true
read -d \0 pipe
fi
debug isPiped hasPipe pipe
Array args=()
for arg in "$@"
do
args+=("$arg")
done
debug args
#Array args=($@)
DB=""
[[ -r $1 ]] && echo "in db" || echo direct
if [ -r $1 ]
then
DB=$args
args=("${args[@]:1}")
_columns_line=($(cat $DB | awk 'NR==2{print $0" "}'))
Array columns=()
for i in ${!_columns_line[@]}
do
Object field="(${_columns_line[$i]})"
# exp field
# echo ${!field[@]}
columns[$i]=${!field[@]}
done
eval "Map cols=( ${_columns_line[@]} )"
debug columns cols
fi
debug args
echo calling: ${args[0]} ${args[@]:1}
${args[0]} "${args[@]:1}"

138
opener Executable file
View File

@ -0,0 +1,138 @@
#!/bin/bash
## nesto v0.1.0
## Copyright (C) p2p.legal, Thomas Di gregorio
## Licensed under MIT
##
## This is the tool to facilitate working on several small text databases.
## Usage:
## nesto [options] <file.db> COMMAND <data>
## <data> | nesto [options] <file.db>
## <file.db> COMMAND <data>
## <data> | <file.db> COMMAND
##
## Options:
## ( Long version are mandatory, and can be before or after short version )
##
## -h, --help Show this help.
## -v, --verbose Show sub-task resolution and options parsed.
## -s, --silent Hide text output.
## -y, --yes Answer yes to questions (Dangerous).
## --all=<val> Display all VMs
##
##
source $(dirname $0)/types.sh
_model="$(dirname $0)/model.awk"
# Parse options and replace defaults
source easyoptions || exit
verbose(){ [[ "$verbose" == "yes" ]] && echo $@; }
_debug(){ [[ "$verbose" == "yes" ]] && for arg in $@;do declare -p $arg 2>/dev/null; done; }
_debug verbose silent yes all arguments
DB=${arguments[0]}
#if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi
#returns "terminal", because the output is sent to your terminal, whereas
#(if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi) | cat
[[ -t 1 ]] && is_piped="" || is_piped="yes"
#[[ -n $piped ]] && echo "will be piped"
#[[ -z $piped ]] && echo "will be printed directly"
add()
{
verbose "add row $@"
}
upd()
{
verbose "update $@"
}
del()
{
verbose "delete $@"
}
_qry()
{
case $1 in
*\&\&*|*\$*|*\|\|*) echo "$1" ;;
*=*) echo "\$$(echo $1 | sed 's/=/=="/')\"" ;;
*~*) echo "\$$(echo "$1" | sed 's|~|~/|')/" ;;
*[!0-9]*) echo "tolower(\$0) ~ /$1/" ;;
*[0-9]*) echo "\$1==$1" ;;
*) echo "$1" ;;
esac
}
qry()
{
verbose "search for $@"
# Find if 1st arg is a number
# case $1 in
# *\&\&*|*\$*|*\|\|*) q="$1" ;;
# *=*) q="\$$(echo $1 | sed 's/=/=="/')\"" ;;
# *~*) q="\$$(echo "$1" | sed 's|~|~/|')/" ;;
# *[!0-9]*) q="tolower(\$0) ~ /$1/" ;;
# *[0-9]*) q="\$1==$1" ;;
# *) q="$1" ;;
# esac
q=$(_qry "$1")
_debug q
verbose "query: $q"
verbose "model: $($_model $DB)"
[[ -n $verbose ]] && declare -p arguments
verbose "(${@:2})"
columns="$(for col in ${@:2}; do printf "\$$col," | sed 's/|.*,//'; done | sed 's/,$//')"
# columns=$(echo ${@:2} | awk 'BEGIN{RS=" ";ORS=",o"}{ print "$"$0 }')
verbose "[$columns]"
format="$(for col in ${@:2}; do name=$(echo $col | sed 's/|.*//'); printf "cdefault f$name c\" \""; done)"
verbose "{$format}"
verbose $($_model $DB)" NR > 2 && $q {printf $format\"\\n\", $columns}" cdefault=$c_light c=$c_ $DB
awk "$($_model $DB) NR > 2 && $q {printf $format\"\\n\", $columns}" cdefault=$c_light$c_black c=$c_ $DB
#'{printf "%-10s %-30s %-10s\n", cp $1, cs $2, c ct $4 c }' cs=$c_status ct=$c_type cp=$c_purple cg=$c_green gr=$c_white cd=$c_dark c=$c_
}
_render()
{
eval `Object cols= $1`
echo '_render'
exp cols
columns="$(for col in ${@:2}; do printf "\$$col," | sed 's/|.*,//'; done | sed 's/,$//')"
# columns=$(echo ${@:2} | awk 'BEGIN{RS=" ";ORS=",o"}{ print "$"$0 }')
verbose "[$columns]"
format="$(for col in ${@:2}; do name=$(echo $col | sed 's/|.*//'); printf "cdefault f$name c\" \""; done)"
verbose "{$format}"
echo "{printf $format\"\\n\", $columns}"
}
exp arguments
eval `Array rest= \( ${arguments[@]:2} \)`
exp rest
[[ $is_piped == "" ]] && qry ${arguments[1]} | _render "`exp rest`"
[[ $is_piped == "yes" ]] && qry ${arguments[1]}
#qry ${arguments[@]:1}

4
tests/file Executable file
View File

@ -0,0 +1,4 @@
#!/home/dig/dev/nesto/opener
id name status
1 root open
2 dig pété

6
tests/new-test.db Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env nestor
[num]= [name]= [status]= [type]= [domains]=
111 dig-docker running KVM (devingfx.com ipfs.io exemple.com *.devingfx.com git.p2p.legal)
147 odoo template LXC (odoo12-test.p2P.legal)
106 docker running KVM (accounts.p2P.legal)
888 aze stopped - ()

5
tests/test.db Executable file
View File

@ -0,0 +1,5 @@
#!/home/dig/dev/nesto/opener
id|%3s name|\033[32m%-30s type|\033[33m%3s status|%-10s branch|%-10s domains|\033[36m%-100s
111 svp-docker-dig LXC running staff www.devingfx.com:ipfs.devingfx.com
106 p2p-docker KVM running docker dalp.p2p.legal:mail.samourai.coop
123 svp-odoo-fred-copylaradio LXC stopped

159
tests/types.test.sh Executable file
View File

@ -0,0 +1,159 @@
#!/bin/bash
verbose=yes
source ./types.sh
echo '--Assoc--'
sAss="([ert]= [dd]=\"toto\" [blo]=true)"
Map obj=(
[ert]=
[dd]="toto"
[blo]=true
)
Map ass=$sAss
debug obj ass
echo '--Array--'
sArr="(a b c d e f)"
Array arr1=(a b c d e f)
Array arr2=$sArr
arrA=(a b c d e f)
arrB=(
a
b c
d
e
f
)
debug arr1 arr2 arrA arrB
sss=sss
debug sss
echo '--String--'
sss=sss
debug sss
ss1=ss1
ss2="ss2"
sStr="\"str\""
String ss3="ss3"
String ss3b=ss3b
String ss4b=$sStr
String ss4="$sStr"
debug ss1 ss2 ss3 ss3b ss4 ss4b
echo '--Objects--'
Map toto=( [id]= [velo]= [9]=9 )
Map vm=( [id]=111 [name]=dig-docker [status]=running )
aze=(toto titi tata)
declare -p aze
debug aze
echo 'laaa'
Object hyb ${aze[@]}
Array list=(aze qsd wxc)
debug list toto vm
echo '--Function arguments--'
my-func()
{
echo "brut($#): $@"
echo "brut: $1"
echo "brut: $2"
Object arg1=$1
declare -p arg1
arg1+=([rrr]=R [ttt]=T [yyy]=Y)
declare -p arg1
arg1+=([name]=NAME)
declare -p arg1
for a in "${!arg1[@]}"; do echo "elem: ${arg1[$a]}"; done
Array arg2=$2
declare -p arg2
arg2+=(rrr ttt yyy)
declare -p arg2
for a in "${!arg2[@]}"; do echo "elem: ${arg2[$a]}"; done
}
my-func "`exp vm`" "`exp aze`"
#if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi
#returns "terminal", because the output is sent to your terminal, whereas
#(if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi) | cat
[[ -t 1 ]] && is_piped="" || is_piped="yes"
#[[ -n $piped ]] && echo "will be piped"
#[[ -z $piped ]] && echo "will be printed directly"
array2map "(num* name status type)" "(111 dig-docker running KVM)"
model=(key* value)
array2map "`exp model`" "(active true)"
#alias VM='declare -A '
#VM vm_temp= "(111 dig-docker running KVM)"
sVM="111 dig-docker running KVM"
_VM 111 dig-docker running KVM
_VM $sVM
Map vm1=`_VM $sVM`
debug vm1
Object test=([key]=aze [value]=fooo)
#declare -A test=([key]=aze [value]=fooo)
debug test
echo looollo
sTest=`declare -p test`
list=(aze qsd "${sTest#*=}" wxc)
exp list
exp list[0]
exp list[2]
exp list[2].value
echo ho ho ho
toString()
{
printf "1) %s\n2) %s\n" $1 $2
local aze=(); local foo=(coucou monde)
echo "${foo[@]} !"
}
Map obj=(
[num]=1
[name]=bob
[status]=chill
[type]=KVN
[toString]='obj_toString()
{
Object this=$1
'"`declare -f toString | awk 'NR>1{print}'`;
obj_toString "
)
debug obj
#obj[arrmap]='{ Object this=$1; '"${obj[arrmap]#{};"'obj_arrmap '
echo "${obj[toString]}"
${obj[toString]}
echo "${obj[toString]} '([name]=toto)'"
eval "${obj[toString]} '([name]=toto)'"

134
types.sh Executable file
View File

@ -0,0 +1,134 @@
shopt -s expand_aliases
alias Object='declare -A'
alias Map='declare -A'
alias Array='declare -a'
alias Int='declare -i'
alias String='declare'
debug()
{
for var in "$@"
do echo -e $c_purple$var$c_="`exp $var`"
done
}
#exp(){ src="$(declare -p $1)"; echo "${src#*=}"; }
exp()
{
# echo ""
# echo ""
echo "Expose $@"
local path=($(echo "$@" | awk -F '[[\].]' '{for(i=1;i<=NF;i++){printf "%s ", $i}; printf "\n"}'))
# declare -p path
# local cur="$(declare -p ${path[0]})"
local src="$(declare -p $path)"
src="${src#*=}"
Object obj=$src
# local cur=($path)
path=(${path[@]:1})
# declare -p obj
while [[ -n $path ]]
do
# echo "while) " $path "${obj[$path]}"
# cur+=$path
src=${obj[$path]}
Object obj=$src
# declare -p obj
# src="$(declare -p obj)"
# src="${src#*=}"
# declare -p src
# obj=$src
path=(${path[@]:1})
done
# src="$(declare -p cur)"
echo "$src"
}
function exp()
{
declare -p $1 | sed 's/^declare -. [^=]*=//' # | sed 's/)$//'
}
function Object()
{
# echo o
case "${@:2}" in
\"*) String $1 ${@:2} ;;
\(\[[0-9]*) Array $1 ${@:2} ;;
\(\[*) Assoc $1 ${@:2} ;;
*) echo "declare $1${@:2}"
esac
}
function tg()
{
src="${@:2}"
is_str=`echo "${@:2}" | grep -v '^".*"$'`
is_arr=`echo "${@:2}" | grep -v '^\(\['`
is_arr=$is_arr`echo "${@:2}" | grep '^\[[0-9]*\]='`
[[ -z $is_arr ]] && echo "declare -A $1=(${@:2})" || echo "declare -a $1=(${@:2})"
}
#Assoc()
#{
# echo "declare -A $1${@:2}"
#}
#function Array()
#{
# echo "declare -a $1${@:2}"
#}
#function String()
#{
# echo "declare $1${@:2}"
#}
map2keys()
{
Map obj=$1
for key in ${obj[@]}
do echo $key
done
}
array2map()
{
Array fields=$1
Array values=$2
Map result=()
for i in ${!fields[@]}
do
result[${fields[$i]}]=${values[$i]}
done
exp result
}
map2json()
{
# eval $1
Map data=$1
printf "{"
for key in ${!data[@]}
do
printf '"%s":' $key
case "${data[$key]}" in
true|false|yes|no)printf '%s' "${data[$key]}" ;;
[0-9.,]) printf '%s' "${data[$key]}" ;;
*) printf '"%s"' "${data[$key]}" ;;
esac
printf ","
# printf '"%s",' "${data[$key]}"
done | sed 's/,$//'
printf "}"
}