From aabe344aa60cc1663cb45a0c2d84fb7f30466cfa Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 24 Jul 2021 13:21:27 +0900 Subject: [PATCH] include/conf: Add support for multiple configuration files The current conf implementation does not permit processes to use more than one configuration file. This commit modifies the conf module so that each process may use an arbitrary number of configuration files by appending the name of a config domain to the commandline of commands such as conf_get() and conf_set(), falling back to "default" if the config domain was omitted. This commit further adds the conf_get_domains() method that may be used to query the names of known configuration domains. --- include/conf.sh | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/include/conf.sh b/include/conf.sh index 84afbf6..a5de6d0 100644 --- a/include/conf.sh +++ b/include/conf.sh @@ -31,8 +31,8 @@ __init() { return 1 fi - declare -xgr __conf_root="$TOOLBOX_HOME/conf" - declare -xgr __conf_file="$__conf_root/$script_name.conf" + declare -xgr __conf_root="$TOOLBOX_HOME/conf/$script_name" + declare -xgr __conf_default="$__conf_root/default.conf" if ! mkdir -p "$__conf_root"; then log_error "Could not create config dir" @@ -44,14 +44,14 @@ __init() { conf_get() { local name="$1" - local default="$2" + local config="$2" - if ! grep -m 1 -oP "^$name=\K.*" "$__conf_file" 2>/dev/null; then - if (( $# <= 1 )); then - return 1 - fi + if [[ -z "$config" ]]; then + config="default" + fi - echo "$default" + if ! grep -m 1 -oP "^$name=\\K.*" "$__conf_root/$config.conf" 2>/dev/null; then + return 1 fi return 0 @@ -59,8 +59,13 @@ conf_get() { conf_unset() { local name="$1" + local config="$2" - if ! sed -i -e "/^$name=.*/d" "$__conf_file" &> /dev/null; then + if [[ -z "$config" ]]; then + config="default" + fi + + if ! sed -i -e "/^$name=.*/d" "$__conf_root/$config.conf" &> /dev/null; then return 1 fi @@ -71,16 +76,32 @@ conf_unset() { conf_set() { local name="$1" local value="$2" + local config="$3" - if conf_get "$name" &> /dev/null; then - if ! conf_unset "$name"; then + if [[ -z "$config" ]]; then + config="default" + fi + + if conf_get "$name" "$config" &> /dev/null; then + if ! conf_unset "$name" "$config"; then return 1 fi fi - if ! echo "$name=$value" >> "$__conf_file"; then + if ! echo "$name=$value" >> "$__conf_root/$config.conf"; then return 1 fi return 0 } + +conf_get_domains() { + local config + + while read -r config; do + config="${config##*/}" + echo "${config%.conf}" + done < <(find "$__conf_root" -type f -iname "*.conf") + + return 0 +} -- 2.47.3