diff options
author | davidovski <david@davidovski.xyz> | 2022-02-13 18:10:19 +0000 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2022-02-13 18:10:19 +0000 |
commit | 9e49a626064c86ca43575301206714d2ae7d77b5 (patch) | |
tree | c9a6939c9679144326806e86ff198bdfe4b7ec06 | |
parent | 3364af2dbdea4c1d816b8499cf58ce0fa1790a62 (diff) |
will not match : in glob matching
-rwxr-xr-x | src/parseconf | 106 | ||||
-rwxr-xr-x | src/parseconf.sh | 2 | ||||
-rwxr-xr-x | test/parseconf.sh | 2 |
3 files changed, 108 insertions, 2 deletions
diff --git a/src/parseconf b/src/parseconf new file mode 100755 index 0000000..44089b7 --- /dev/null +++ b/src/parseconf @@ -0,0 +1,106 @@ +#!/bin/bash + +usage () { + printf "Usage $0 " + echo << "EOF" +OPTIONS... [FILTER] + +Print the parsed config file filtering by the keys +Arguments: + -f file read configuration from a file, uses /dev/sdtin otherwise + -v only print values + -c n print the last [n] +EOF +} + +# print the parsed values from the config file in key:value format +# +parse () { + local file="$1" + local level="" + local list="" + while IFS= read -r line; do + line=$(sed "s/\s\+/ /g" <<< "$line" | sed "s/^\s\|\s$\|;*$//g") + + grep -q "^#" <<< "$line" && continue + grep -q "." <<< "$line" || continue + + local key=$(echo $line | cut -d" " -f1) + local value=$(echo $line | cut -d" " -f2-) + + [ "$key" = "include" ] && parse $value && continue + + case ${value: -1} in + "{") + level="$level$key." + ;; + "[") + list="$list$key." + printf "$level$key:" + ;; + "}") + level=$(sed "s/[^\.]\w*\.$//g" <<< "$level") + ;; + "]") + printf "\n" + list=$(sed "s/[^\.]\w*\.$//g" <<< "$list") + ;; + *) + + grep -q "." <<< "$list" && + printf "$line " || + printf "$level$key:$value\n" + ;; + esac + done < "$file" +} + +# Filter the parsed file for specific keys +# +filter () { + local pattern= + + [ $# = 0 ] && + pattern=".*" || + pattern=$(sed "s/\*/[^:]*/g"<<< "$@") + + $print_keys && + pattern="s/^($pattern:.+)/\1/p" || + pattern="s/^$pattern:(.+)/\1/p" + + + parse $CONF_FILE | sed -rn $pattern +} + +# Use the env variable if exists +[ -z ${CONF_FILE} ] && CONF_FILE="/dev/stdin" + +# initialise options +print_keys=true +count= + +while getopts ":f:c:v" opt; do + case "${opt}" in + f) + [ "${OPTARG}" = "-" ] && + CONF_FILE="/dev/stdin" || + CONF_FILE="${OPTARG}" + ;; + + v) + print_keys=false + ;; + c) + count="${OPTARG}" + ;; + *) + esac +done + +shift $((OPTIND-1)) + +[ -z ${count} ] && + filter "$@" || + filter "$@" | tail -n $count + + diff --git a/src/parseconf.sh b/src/parseconf.sh index c5ed801..44089b7 100755 --- a/src/parseconf.sh +++ b/src/parseconf.sh @@ -62,7 +62,7 @@ filter () { [ $# = 0 ] && pattern=".*" || - pattern=$(sed "s/\*/.*/g"<<< "$@") + pattern=$(sed "s/\*/[^:]*/g"<<< "$@") $print_keys && pattern="s/^($pattern:.+)/\1/p" || diff --git a/test/parseconf.sh b/test/parseconf.sh index 3095e18..3af55e1 100755 --- a/test/parseconf.sh +++ b/test/parseconf.sh @@ -1,6 +1,6 @@ #!/bin/sh -PARSECONF="./src/parseconf" +PARSECONF="./src/parseconf.sh" SIMPLECONF="./test/simple.conf" |