From a624cae51ba8a0cbdb3b3bd657ed8674135a85ad Mon Sep 17 00:00:00 2001 From: ayakael Date: Mon, 26 Mar 2018 22:39:58 -0400 Subject: [PATCH] File is not defined explicitely rather than globally --- src/cfg.sh | 82 ++++++++++++++++++++++++++---------------------- test/test-cfg.sh | 54 ++++++++++++++----------------- 2 files changed, 69 insertions(+), 67 deletions(-) diff --git a/src/cfg.sh b/src/cfg.sh index 61ede8c..d9de4d8 100644 --- a/src/cfg.sh +++ b/src/cfg.sh @@ -9,11 +9,9 @@ # Reads and writes config files delimited by tabs, like fstab, using awk. # # USAGE -# _cfg +# _cfg # # COMMANDS -# set-file -# Sets path to file # # create # Creates config file and assigns . @@ -50,11 +48,12 @@ _cfg_column_to_nf() { # Argument parser and sanity check - [[ -z ${1+x} ]] && { _msg ECHO "_cfg_column_to_nf(): Expected column name"; return 1; } - local COLUMN=${1} + [[ $# != 2 ]] && { _msg ECHO "_cfg_column_to_nf(): Expected 2 arguments"; return 1; } + local FILE="${1}" + local COLUMN=${2} # Queries list of columns - local columnList=(all $(awk 'BEGIN {FS="\t"; OFS="\t"}; {if (NR == 1 ) {print $0}}' ${_CFG_PATH})) + local columnList=(all $(awk 'BEGIN {FS="\t"; OFS="\t"}; {if (NR == 1 ) {print $0}}' ${FILE})) # Checks if queried ${COLUMN} exists if ! _if_array_contains ${COLUMN} ${columnList[@]}; then @@ -71,75 +70,80 @@ _cfg_column_to_nf() { fi } -_cfg_set_file() { - # Argument parser and sanity check - [[ $# != 1 ]] && { _msg ECHO "_cfg_set_file(): Expected 1 argument"; return 1; } +_cfg_tmp_file() { + local FILE="${1}" - # Sets _CFG_PATH globally - _CFG_PATH="${1}" - _CFG_TMP_PATH="$(sed 's|\(.*\)\/|\1/.|' <<<"${_CFG_PATH}")" + echo -n "$(dirname ${FILE})/.$(basename ${FILE})" } _cfg_create() { # Argument parser and sanity check - [[ -z ${1+x} ]] && { _msg ECHO "_cfg_create(): Expected argument"; return 1; } + [[ $# -lt 2 ]] && { _msg ECHO "_cfg_create(): Expected at least 2 arguments"; return 1; } + local FILE="${1}"; shift local colList=(${@}) # Creates config file - printf "%s\t" ${colList[@]} | awk 'BEGIN{OFS="\t";} {printf "%s\n",$0}' >> ${_CFG_PATH} + printf "%s\t" ${colList[@]} | awk 'BEGIN{OFS="\t";} {printf "%s\n",$0}' >> ${FILE} } _cfg_print() { # Argument parser and sanity check - [[ $# != 2 ]] && { _msg ECHO "_cfg_print(): Expected 2 arguments"; return 1; } - local COLUMN=${1} - local ROW=${2} + [[ $# != 3 ]] && { _msg ECHO "_cfg_print(): Expected 3 arguments"; return 1; } + local FILE="${1}" + local COLUMN=${2} + local ROW=${3} # Prints row of ${_CFG_PATH} - awk 'BEGIN {OFS="\t"}; {if (NR == '${ROW}') {print $'$(_cfg_column_to_nf ${COLUMN})'}}' "${_CFG_PATH}" + awk 'BEGIN {OFS="\t"}; {if (NR == '${ROW}') {print $'$(_cfg_column_to_nf ${COLUMN})'}}' "${FILE}" } _cfg_change() { # Argument parser and sanity check - [[ $# != 3 ]] && { _msg ECHO "_cfg_change(): Expected 3 arguments"; return 1; } - local COLUMN=${1} - local ROW=${2} - local VALUE=${3} + [[ $# != 4 ]] && { _msg ECHO "_cfg_change(): Expected 4 arguments"; return 1; } + local FILE="${1}" + local COLUMN=${2} + local ROW=${3} + local VALUE=${4} + local TMP_FILE=$(_cfg_tmp_file ${FILE}) local COLUMN_NO=$(_cfg_column_to_nf ${COLUMN}) _if_is_integer ${ROW} || { _msg ECHO "_cfg_change(): Expected integer"; return 1; } [[ ${ROW} > $(awk 'BEGIN{FS="\t"; OFS="\t"}; {print NR}' ${_CFG_PATH} | wc -l) ]] && { _msg ECHO "_cfg_change(): Row ${ROW} does not exist"; return 1; } # Writes new version of file - awk 'BEGIN {OFS="\t"}; {if (NR == '${ROW}') {$'${COLUMN_NO}'="'${VALUE}'"} {print $0}}' "${_CFG_PATH}" > ${_CFG_TMP_PATH} && mv ${_CFG_TMP_PATH} ${_CFG_PATH} + awk 'BEGIN {OFS="\t"}; {if (NR == '${ROW}') {$'${COLUMN_NO}'="'${VALUE}'"} {print $0}}' "${FILE}" > ${TMP_FILE} && mv ${TMP_FILE} ${FILE} } _cfg_drop() { # Argument parser and sanity check - [[ $# != 2 ]] && { _msg ECHO "_cfg_drop(): Expected 2 arguments"; return 1; } + [[ $# != 3 ]] && { _msg ECHO "_cfg_drop(): Expected 3 arguments"; return 1; } local SUBCMD=${1}; shift _if_array_contains ${SUBCMD} row column || { _msg ECHO "_cfg_drop(): Expected 'row' or 'column' as subcommand"; return 1; } # Dispatcher - eval _cfg_drop_${SUBCMD} ${1} + eval _cfg_drop_${SUBCMD} ${@} } _cfg_drop_row() { # Argument parser and sanity check - local ROW=${1} - [[ ${ROW} > $(awk 'BEGIN {FS="\t"; OFS="\t"}; {print NR}' ${_CFG_PATH} | wc -l) ]] && { _msg ECHO "_cfg_drop_row(): Row ${ROW} does not exist"; return 1; } + local FILE="${1}" + local TMP_FILE="$(_cfg_tmp_file ${FILE})" + local ROW=${2} + [[ ${ROW} > $(awk 'BEGIN {FS="\t"; OFS="\t"}; {print NR}' ${FILE} | wc -l) ]] && { _msg ECHO "_cfg_drop_row(): Row ${ROW} does not exist"; return 1; } # Writes new version of file - awk 'BEGIN {OFS="\t"}; {if (NR != '${ROW}') {print $0}}' ${_CFG_PATH} > ${_CFG_TMP_PATH} && mv ${_CFG_TMP_PATH} ${_CFG_PATH} + awk 'BEGIN {OFS="\t"}; {if (NR != '${ROW}') {print $0}}' ${FILE} > ${TMP_FILE} && mv ${TMP_FILE} ${FILE} } _cfg_drop_column() { - local COLUMN=${1} + local FILE="${1}" + local TMP_FILE="$(_cfg_tmp_file ${FILE})" + local COLUMN=${2} local NF=$(_cfg_column_to_nf "${COLUMN}") # Writes new version of file - awk 'BEGIN {OFS="\t"}; $'${NF}'="";1' ${_CFG_PATH} > ${_CFG_TMP_PATH} && mv ${_CFG_TMP_PATH} ${_CFG_PATH} + awk 'BEGIN {OFS="\t"}; $'${NF}'="";1' ${FILE} > ${TMP_FILE} && mv ${TMP_FILE} ${FILE} } _cfg_insert() { @@ -154,29 +158,33 @@ _cfg_insert() { _cfg_insert_column() { [[ $# != 1 ]] && { _msg ECHO "_cfg_insert_column(): Expected 1 argument"; return 1; } # Argument parser and sanity check - local VALUE=${1} - local NX_COLUMN=$(( $(awk 'BEGIN{OFS="\t"}; {if (NR == 1) {print NF}}' ${_CFG_PATH}) + 1 )) + local FILE="${1}" + local TMP_FILE="$(_cfg_tmp_file ${FILE})" + local VALUE=${2} + local NX_COLUMN=$(( $(awk 'BEGIN{OFS="\t"}; {if (NR == 1) {print NF}}' ${FILE}) + 1 )) # Write new version of header into file - awk 'BEGIN {OFS="\t"}; {if (NR == 1) {$'${NX_COLUMN}'="'${VALUE}'"} {print $0}}' "${_CFG_PATH}" > ${_CFG_TMP_PATH} && mv ${_CFG_TMP_PATH} ${_CFG_PATH} + awk 'BEGIN {OFS="\t"}; {if (NR == 1) {$'${NX_COLUMN}'="'${VALUE}'"} {print $0}}' "${FILE}" > ${TMP_FILE} && mv ${TMP_FILE} ${FILE} } _cfg_insert_row() { [[ -z ${1+x} ]] && { _msg ECHO "_cfg_insert_row(): Expected at least 1 argument"; return 1; } # Argument parser and sanity check + local FILE="${1}"; shift 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}}' ${FILE}) [[ "${#valueList[@]}" != "${COLUMN_NO}" ]] && { _msg ECHO "_cfg_insert_row(): Number of arguments must be equal to ${COLUMN_NO}"; return 1; } # Write row into file - printf "%s\t" ${valueList[@]} | awk 'BEGIN{OFS="\t";} {printf "%s\n",$0}' >> ${_CFG_PATH} + printf "%s\t" ${valueList[@]} | awk 'BEGIN{OFS="\t";} {printf "%s\n",$0}' >> ${FILE} } _cfg_query() { # Argument parser and sanity check - local CONDITION="${1}" + local FILE="${1}" + local CONDITION="${2}" - awk 'BEGIN {FS="\t"; OFS="\t"}; {if ('${CONDITION}') {print NR}}' "${_CFG_PATH}" + awk 'BEGIN {FS="\t"; OFS="\t"}; {if ('${CONDITION}') {print NR}}' "${FILE}" } _cfg() { diff --git a/test/test-cfg.sh b/test/test-cfg.sh index 525c115..172b55a 100644 --- a/test/test-cfg.sh +++ b/test/test-cfg.sh @@ -1,10 +1,12 @@ #!/bin/bash -source ../lib/if.sh -source ../lib/ansi.sh -source ../lib/msg.sh -source ../lib/cfg.sh +source ../srv/if.sh +source ../src/ansi.sh +source ../src/msg.sh +source ../src/cfg.sh TMP=/tmp +FILE=${TMP}/cfg.db +TMP_FILE=${TMP}/.cfg.db testcolList=(COL1 COL2 COL3 COL4 COL5) # cfg test @@ -14,17 +16,9 @@ bail() { exit 1 } -_test_cfg_set_file() { - _msg EXEC "Testing _cfg_set_file()" - _cfg_set_file ${TMP}/test.cfg - [[ "${_CFG_PATH}" != "${TMP}/test.cfg" ]] && { _msg FAIL "_test_cfg_set_file(): \${_CFG_PATH} is '${_CFG_PATH}' rather than the expected '${TMP}/test.cfg'"; } - [[ "${_CFG_TMP_PATH}" != "${TMP}/.test.cfg" ]] && { _msg FAIL "_test_cfg_set_file(): \${_CFG_TMP_PATH} is '${_CFG_TMP_PATH}' rather than the expected '${TMP}/.test.cfg"; } - _msg OK -} - _test_cfg_create() { _msg EXEC "Testing _cfg_create()" - local CMD="_cfg_create ${testcolList[@]}" + local CMD="_cfg_create ${FILE} ${testcolList[@]}" eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_create(): Error in function execution"; } local FC_OUTPUT="$(cat ${TMP}/test.cfg)" @@ -35,7 +29,7 @@ _test_cfg_create() { _test_cfg_column_to_nf() { _msg EXEC "Testing _cfg_column_to_nf()" - local argList=(all ${testcolList[@]}) + local argList=(${FILE} all ${testcolList[@]}) _cfg_column_to_nf ${argList[1]} >${STDERR} 2>&1 || { _msg FAIL "test_cfg_column_to_nf(): Error in function execution"; } for no in {0..5}; do @@ -48,7 +42,7 @@ _test_cfg_column_to_nf() { _test_cfg_print() { _msg EXEC "Testing _cfg_print()" - local argList=(all 1) + local argList=(${FILE} all 1) local CMD="_cfg_print ${argList[@]}" eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_print(): Error in function execution"; } @@ -60,7 +54,7 @@ _test_cfg_print() { _test_cfg_insert_row() { _msg EXEC "Testing _cfg_insert_row()" - local argList=(EL1 EL2 EL3 EL4 EL5) + local argList=(${FILE} EL1 EL2 EL3 EL4 EL5) local CMD="_cfg_insert_row ${argList[@]}" eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_insert_row(): Error in function execution"; } @@ -68,13 +62,13 @@ _test_cfg_insert_row() { local EX_OUTPUT="$(printf '%s\t' ${argList[@]})" [[ "${FC_OUTPUT}" != "${EX_OUTPUT}" ]] && { _msg FAIL "_test_cfg_insert_row(): Output '${FC_OUTPUT}' expected to be '${EX_OUTPUT}'"; } - awk 'BEGIN {OFS="\t"}; {if (NR != 2) {print $0}}' ${_CFG_PATH} > ${_CFG_TMP_PATH} && mv ${_CFG_TMP_PATH} ${_CFG_PATH} + awk 'BEGIN {OFS="\t"}; {if (NR != 2) {print $0}}' ${FILE} > ${TMP_FILE} && mv ${TMP_FILE} ${FILE} _msg OK } _test_cfg_insert_column() { _msg EXEC "Testing _cfg_insert_column()" - local argList=(COL6) + local argList=(${FILE} COL6) local CMD="_cfg_insert_column ${argList[@]}" eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_insert_column(): Error in function execution"; } @@ -83,27 +77,27 @@ _test_cfg_insert_column() { [[ "${fcoutputList[@]}" != "${exoutputList[@]}" ]] && { _msg FAIL "_test_cfg_insert_column(): Output '${fcoutputList[@]}' expected to be '${exoutputList[@]}'"; } - awk 'BEGIN {OFS="\t"}; {$6=""; print $0}' ${_CFG_PATH} > ${_CFG_TMP_PATH} && mv ${_CFG_TMP_PATH} ${_CFG_PATH} + awk 'BEGIN {OFS="\t"}; {$6=""; print $0}' ${FILE} > ${TMP_FILE} && mv ${TMP_FILE} ${FILE} _msg OK } _test_cfg_change() { _msg EXEC "Testing _cfg_change()" - _cfg_insert_row ${testcolList[@]} - local argList=(COL1 2 EL1_CH) + _cfg_insert_row ${FILE} ${testcolList[@]} + local argList=(${FILE} COL1 2 EL1_CH) local CMD="_cfg_change ${argList[@]}" eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_change(): Error in function execution"; } local FC_OUTPUT="$(_cfg_print COL1 2)" local EX_OUTPUT="EL1_CH" [[ "${FC_OUTPUT}" != "${EX_OUTPUT}" ]] && { _msg FAIL "_test_cfg_change(): Output '${FC_OUTPUT}' expected to be '${EX_OUTPUT}'"; } - _cfg_drop_row 2 + _cfg_drop_row ${FILE} 2 _msg OK } _test_cfg_drop_row() { _msg EXEC "Testing _cfg_drop_row()" - _cfg_insert_row ${testcolList[@]} - local argList=(2) + _cfg_insert_row ${FILE} ${testcolList[@]} + local argList=(${FILE} 2) local CMD="_cfg_drop_row ${argList[@]}" eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_drop_row(): Error in function execution"; } @@ -115,12 +109,12 @@ _test_cfg_drop_row() { _test_cfg_drop_column() { _msg EXEC "Testing _cfg_drop_column()" - _cfg_insert_column "COL6" + _cfg_insert_column ${FILE} "COL6" local argList="COL6" local CMD="_cfg_drop_column ${argList[@]}" eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_drop_column(): Error in function execution"; } - FC_OUTPUT="$(printf '%s ' $(awk 'BEGIN{OFS="\t"}; {if (NR == 1) {print $0}}' ${_CFG_PATH}))" + FC_OUTPUT="$(printf '%s ' $(awk 'BEGIN{OFS="\t"}; {if (NR == 1) {print $0}}' ${FILE}))" EX_OUTPUT="$(printf '%s ' ${testcolList[@]})" [[ "${FC_OUTPUT}" != "${EX_OUTPUT}" ]] && { _msg FAIL "_test_cfg_drop_column(): Output '${FC_OUTPUT}' expected to be '${EX_OUTPUT}'"; } _msg OK @@ -128,17 +122,17 @@ _test_cfg_drop_column() { _test_cfg_query() { _msg EXEC "Testing _cfg_query" - _cfg_insert_row "EL1 EL2 EL3 EL4 EL5" - _cfg_insert_row "EL6 EL7 EL8 EL9 EL10" + _cfg_insert_row ${FILE} "EL1 EL2 EL3 EL4 EL5" + _cfg_insert_row ${FILE} "EL6 EL7 EL8 EL9 EL10" local ARG='$1=="EL6"' - local CMD="_cfg_query '${ARG}'" + local CMD="_cfg_query ${FILE} '${ARG}'" eval ${CMD} >${STDERR} 2>&1 || { _msg FAIL "_test_cfg_query(): Error in function execution"; } FC_OUTPUT=$(eval ${CMD}) EX_OUTPUT=3 [[ "${FC_OUTPUT}" != "${EX_OUTPUT}" ]] && { _msg FAIL "_test_cfg_query(): Output '${FC_OUTPUT}' expected to be '${EX_OUTPUT}'"; } -awk 'BEGIN {OFS="\t"}; {if (NR == 1) {print $0}}' ${_CFG_PATH} > ${_CFG_TMP_PATH} && mv ${_CFG_TMP_PATH} ${_CFG_PATH} +awk 'BEGIN {OFS="\t"}; {if (NR == 1) {print $0}}' ${FILE} > ${TMP_FILE} && mv ${TMP_FILE} ${FILE} _msg OK }