139 lines
3.5 KiB
Bash
Executable File
139 lines
3.5 KiB
Bash
Executable File
#!/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}
|