diff --git a/README.md b/README.md index 58ad63d..bb628d8 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Although *3* introduces a node.js dependency, this does allow for easy version p ### master (Unreleased) -- tba +- Add tests for `ini_val` and follow Library export best practices ### v1.2.1 (2016-02-17) diff --git a/src/ini_val.sh b/src/ini_val.sh index 0f7b2ed..a07b022 100755 --- a/src/ini_val.sh +++ b/src/ini_val.sh @@ -1,8 +1,39 @@ #!/usr/bin/env bash +# BASH3 Boilerplate: ini_val +# +# This file: +# - Can read and write .ini files using pure bash +# +# Limitations: +# - All keys inside the .ini file must be unique, regardless of the use of sections +# +# More info: +# - https://github.com/kvz/bash3boilerplate +# - http://kvz.io/blog/2013/02/26/introducing-bash3boilerplate/ +# +# Version: 1.2.1 +# +# Authors: +# - Kevin van Zonneveld (http://kvz.io) +# - Izaak Beekman (https://izaakbeekman.com/) +# - Alexander Rathai (Alexander.Rathai@gmail.com) +# +# Usage as a function: +# +# source ini_val.sh +# ini_val data.ini connection.host 127.0.0.1 +# +# Usage as a command: +# +# ini_val.sh data.ini connection.host 127.0.0.1 +# +# Licensed under MIT +# Copyright (c) 2016 Kevin van Zonneveld (http://kvz.io) + function ini_val() { - local file="${1}" - local sectionkey="${2}" - local val="${3}" + local file="${1:-}" + local sectionkey="${2:-}" + local val="${3:-}" local delim=" = " local section="" local key="" @@ -14,7 +45,7 @@ function ini_val() { section="" fi - local current=$(awk -F"${delim}" "/^${key}${delim}/ {for (i=2; i> "${file}" else # add to section - sed "/\[${section}\]/a ${key}${delim}${val}" -i "${file}" + sed -i.bak -e "/\[${section}\]/a ${key}${delim}${val}" "${file}" + # this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666 + rm -f "${file}.bak" fi else # replace existing - sed "/^${key}${delim}/s/${delim}.*/${delim}${val}/" -i "${file}" + sed -i.bak -e "/^${key}${delim}/s/${delim}.*/${delim}${val}/" "${file}" + # this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666 + rm -f "${file}.bak" fi fi } + +if [ "${BASH_SOURCE[0]}" != ${0} ]; then + export -f ini_val +else + ini_val "${@}" + exit ${?} +fi diff --git a/test/fixture/inival.exitcode b/test/fixture/inival.exitcode new file mode 100644 index 0000000..573541a --- /dev/null +++ b/test/fixture/inival.exitcode @@ -0,0 +1 @@ +0 diff --git a/test/fixture/inival.stdio b/test/fixture/inival.stdio new file mode 100644 index 0000000..5034a09 --- /dev/null +++ b/test/fixture/inival.stdio @@ -0,0 +1,24 @@ +--> command: Read 3 values +exists +127.0.0.1 +nginx, nodejs +--> command: Replace three values in-place and show result +orphan = no more + +[connection] +host = 192.168.0.1 + +[software] +packages = vim +--> function: Read 3 values +exists +127.0.0.1 +nginx, nodejs +--> function: Replace three values in-place and show result +orphan = no more + +[connection] +host = 192.168.0.1 + +[software] +packages = vim diff --git a/test/scenario/inival/data.ini b/test/scenario/inival/data.ini new file mode 100644 index 0000000..d55cef4 --- /dev/null +++ b/test/scenario/inival/data.ini @@ -0,0 +1,7 @@ +orphan = exists + +[connection] +host = 127.0.0.1 + +[software] +packages = nginx, nodejs diff --git a/test/scenario/inival/run.sh b/test/scenario/inival/run.sh new file mode 100644 index 0000000..bb2e112 --- /dev/null +++ b/test/scenario/inival/run.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +set -o pipefail +set -o errexit +set -o nounset +# set -o xtrace + +# Set magic variables for current FILE & DIR +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" +__base="$(basename ${__file} .sh)" +__root="$(cd "$(dirname $(dirname $(dirname "${__dir}")))" && pwd)" + +# echo "B3BP:STDIO_REPLACE_DATETIMES" + + +# Use as standalone: +cp -f data.ini dummy.ini +echo "--> command: Read 3 values" +bash "${__root}/src/ini_val.sh" ./dummy.ini orphan +bash "${__root}/src/ini_val.sh" ./dummy.ini connection.host +bash "${__root}/src/ini_val.sh" ./dummy.ini software.packages + +echo "--> command: Replace three values in-place and show result" +bash "${__root}/src/ini_val.sh" ./dummy.ini orphan "no more" +bash "${__root}/src/ini_val.sh" ./dummy.ini connection.host "192.168.0.1" +bash "${__root}/src/ini_val.sh" ./dummy.ini software.packages "vim" +cat dummy.ini +rm -f dummy.ini + +# Use as include: +cp -f data.ini dummy.ini +source ${__root}/src/ini_val.sh +echo "--> function: Read 3 values" +ini_val ./dummy.ini orphan +ini_val ./dummy.ini connection.host +ini_val ./dummy.ini software.packages + +echo "--> function: Replace three values in-place and show result" +ini_val ./dummy.ini orphan "no more" +ini_val ./dummy.ini connection.host "192.168.0.1" +ini_val ./dummy.ini software.packages "vim" +cat dummy.ini +rm -f dummy.ini