Fixed dispatcher functions and parser and sanity checks

_cfg_drop_column now leaves no trailing tab
Updated documentation for _cfg insert
This commit is contained in:
ayakael 2018-01-11 11:05:36 -09:00
parent d20bc4e986
commit 351516d54c
No known key found for this signature in database
GPG key ID: 575626A4AE5F4026
2 changed files with 10 additions and 9 deletions

View file

@ -27,8 +27,8 @@
# drop <row|column} {no|name} # drop <row|column} {no|name}
# Drops <row-no> or <column-name>. # Drops <row-no> or <column-name>.
# #
# insert <value-for-col1> <value-for-col2> <...> # insert <row|column> <value(s)>
# Inserts new row with a value per column. Number of arguments must match number of columns. # Inserts new row or column. Number of row values must match number of columns.
# #
# query <conditional> # query <conditional>
# Queries which <row-no(s)> fits the <conditional>. # Queries which <row-no(s)> fits the <conditional>.
@ -118,7 +118,7 @@ _cfg_drop() {
# Argument parser and sanity check # Argument parser and sanity check
[[ $# != 2 ]] && { _msg ECHO "_cfg_drop(): Expected 2 arguments"; return 1; } [[ $# != 2 ]] && { _msg ECHO "_cfg_drop(): Expected 2 arguments"; return 1; }
local SUBCMD=${1}; shift local SUBCMD=${1}; shift
[[ ! $(_if_array_contains ${SUBCMD} row column) ]] && { _msg ECHO "_cfg_drop(): Expected 'row' or 'column' as subcommand"; return 1; } _if_array_contains ${SUBCMD} row column || { _msg ECHO "_cfg_drop(): Expected 'row' or 'column' as subcommand"; return 1; }
# Dispatcher # Dispatcher
eval _cfg_drop_${SUBCMD} ${1} eval _cfg_drop_${SUBCMD} ${1}
@ -139,20 +139,20 @@ _cfg_drop_column() {
local NF=$(_cfg_column_to_nf "${COLUMN}") local NF=$(_cfg_column_to_nf "${COLUMN}")
# Writes new version of file # Writes new version of file
awk 'BEGIN {OFS="\t"}; {$'${NF}'=""; print $0}' ${_CFG_PATH} > ${_CFG_TMP_PATH} && mv ${_CFG_TMP_PATH} ${_CFG_PATH} awk 'BEGIN {OFS="\t"}; $'${NF}'="";1' ${_CFG_PATH} > ${_CFG_TMP_PATH} && mv ${_CFG_TMP_PATH} ${_CFG_PATH}
} }
_cfg_insert() { _cfg_insert() {
# Argument parser and sanity check # Argument parser and sanity check
[[ $# != 2 ]] && { _msg ECHO "_cfg_insert(): Expected 2 arguments"; return 1; }
local SUBCMD=${1}; shift local SUBCMD=${1}; shift
[[ ! $(_if_array_contains ${SUBCMD} row column) ]] && { _msg ECHO "_cfg_insert(): Expected 'row' or 'column' as subcommand"; return 1; } _if_array_contains ${SUBCMD} row column || { _msg ECHO "_cfg_insert(): Expected 'row' or 'column' as subcommand"; return 1; }
# Dispatcher # Dispatcher
eval _cfg_insert_${SUBCMD} ${1} eval _cfg_insert_${SUBCMD} ${@}
} }
_cfg_insert_column() { _cfg_insert_column() {
[[ $# != 1 ]] && { _msg ECHO "_cfg_insert_column(): Expected 1 argument"; return 1; }
# Argument parser and sanity check # Argument parser and sanity check
local VALUE=${1} local VALUE=${1}
local NX_COLUMN=$(( $(awk 'BEGIN{OFS="\t"}; {if (NR == 1) {print NF}}' ${_CFG_PATH}) + 1 )) local NX_COLUMN=$(( $(awk 'BEGIN{OFS="\t"}; {if (NR == 1) {print NF}}' ${_CFG_PATH}) + 1 ))
@ -162,6 +162,7 @@ _cfg_insert_column() {
} }
_cfg_insert_row() { _cfg_insert_row() {
[[ -z ${1+x} ]] && { _msg ECHO "_cfg_insert_row(): Expected at least 1 argument"; return 1; }
# Argument parser and sanity check # Argument parser and sanity check
local valueList=(${@}) local valueList=(${@})
local COLUMN_NO=$(awk 'BEGIN{OFS="\t"}; {if (NR == 1) {print NF}}' ${_CFG_PATH}) local COLUMN_NO=$(awk 'BEGIN{OFS="\t"}; {if (NR == 1) {print NF}}' ${_CFG_PATH})
@ -183,7 +184,7 @@ _cfg() {
[[ -z ${1+x} ]] && { _msg ECHO "_cfg(): Expected command"; return 1; } [[ -z ${1+x} ]] && { _msg ECHO "_cfg(): Expected command"; return 1; }
local cmdList="(set-file create print change drop insert query)" local cmdList="(set-file create print change drop insert query)"
local CMD=${1}; shift local CMD=${1}; shift
[[ ! $(_if_array_contains ${CMD} ${cmdList[@]}) ]] && { _msg ECHO "_cfg(): Command specified not valid"; return 1; } _if_array_contains ${CMD} ${cmdList[@]} || { _msg ECHO "_cfg(): Command specified not valid"; return 1; }
# Dispatcher # Dispatcher
eval _cfg_$(sed 's|-|_|' <<<"${CMD}") ${@} eval _cfg_$(sed 's|-|_|' <<<"${CMD}") ${@}

View file

@ -130,7 +130,7 @@ _test_cfg_query() {
_msg EXEC "Testing _cfg_query" _msg EXEC "Testing _cfg_query"
_cfg_insert_row "EL1 EL2 EL3 EL4 EL5" _cfg_insert_row "EL1 EL2 EL3 EL4 EL5"
_cfg_insert_row "EL6 EL7 EL8 EL9 EL10" _cfg_insert_row "EL6 EL7 EL8 EL9 EL10"
local ARG="\$1==\"EL6\"" local ARG='$1=="EL6"'
local CMD="_cfg_query '${ARG}'" local CMD="_cfg_query '${ARG}'"
eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_query(): Error in function execution"; } eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_query(): Error in function execution"; }