Add tests for ini_val and follow Library export best practices

This commit is contained in:
Kevin van Zonneveld 2016-02-17 12:48:57 +01:00
parent b404c87999
commit 180282d97d
6 changed files with 124 additions and 7 deletions

View File

@ -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)

View File

@ -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<NF; i++) printf \$i \" \"; print \$NF}" "${file}")
local current=$(awk -F "${delim}" "/^${key}${delim}/ {for (i=2; i<NF; i++) printf \$i \" \"; print \$NF}" "${file}")
if [ -z "${val}" ]; then
# get a value
echo "${current}"
@ -28,11 +59,22 @@ function ini_val() {
echo "${key}${delim}${val}" >> "${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

View File

@ -0,0 +1 @@
0

24
test/fixture/inival.stdio Normal file
View File

@ -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

View File

@ -0,0 +1,7 @@
orphan = exists
[connection]
host = 127.0.0.1
[software]
packages = nginx, nodejs

View File

@ -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