From 8cbdb67b734eb1a9d16649788f41f49dc2a064f7 Mon Sep 17 00:00:00 2001 From: Antonia Stevens Date: Wed, 27 May 2020 16:21:50 +0000 Subject: [PATCH 1/6] Fix load_config to allow it to read from stdin Adds the ability to read INI style conifigs from stdin and switches to a continious read operation rather than multiple reads --- shtdlib.sh | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/shtdlib.sh b/shtdlib.sh index c620754..b27d602 100755 --- a/shtdlib.sh +++ b/shtdlib.sh @@ -2526,26 +2526,34 @@ function associate_array { done } -# Safely loads config file +# Safely loads config file or config from stdin # First parameter is filename, all consequent parameters are assumed to be # valid configuration parameters function load_config { - config_file="${1}" - # Verify config file permissions are correct and warn if they aren't - # Dual stat commands to work with both linux and bsd - while read -r line; do + # If first argument is a filename read it and pass it to the function + if [ -f "${1:-}" ]; then + cat "${1}" | load_config "${@:2}" + return + elif [ -t 0 ] ; then + color_echo red "No config filename provided or data on stdin, exiting" + return 1 + fi + + # First command needs to be read, this way any piped input goes to it + readarray config_file + for line in "${config_file[@]}"; do if [[ "${line}" =~ ^[^#]*= ]]; then setting_name="$(echo "${line}" | awk -F '=' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" setting_value="$(echo "${line}" | cut -f 2 -d '=' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" - for requested_setting in "${@:2}" ; do + for requested_setting in "${@}" ; do if [ "${requested_setting}" == "${setting_name}" ] ; then export "${setting_name}"="${setting_value}" debug 10 "Loaded config parameter ${setting_name} with value of '${setting_value}'" fi done fi - done < "${config_file}"; + done } # Load settings from config file if they have not been set already From 0c1e885876bd4276ea56a4e5a4af33651f86ac0d Mon Sep 17 00:00:00 2001 From: Antonia Stevens Date: Thu, 28 May 2020 15:30:45 +0000 Subject: [PATCH 2/6] Fix archive creation --- shtdlib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shtdlib.sh b/shtdlib.sh index b27d602..a94c81a 100755 --- a/shtdlib.sh +++ b/shtdlib.sh @@ -2206,7 +2206,7 @@ function create_relative_archive { done # shellcheck disable=SC2068 - tar ${transformations[@]} "${verbose_flag}" "--${archive_operation}" --exclude-vcs --directory "${run_dir}" --file "${archive_path}" ${source_elements[@]} || exit_on_fail + tar ${transformations[@]} ${verbose_flag} --${archive_operation} --exclude-vcs --directory "${run_dir}" --file "${archive_path}" ${source_elements[@]} || exit_on_fail } # Given a filename it will sign the file with the default key From d63df3cfd12ca1697f2a7a91cdc6bef1d0b35017 Mon Sep 17 00:00:00 2001 From: Antonia Stevens Date: Thu, 28 May 2020 18:06:43 +0000 Subject: [PATCH 3/6] Fix load_config --- shtdlib.sh | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/shtdlib.sh b/shtdlib.sh index a94c81a..8e0e2e1 100755 --- a/shtdlib.sh +++ b/shtdlib.sh @@ -2206,7 +2206,7 @@ function create_relative_archive { done # shellcheck disable=SC2068 - tar ${transformations[@]} ${verbose_flag} --${archive_operation} --exclude-vcs --directory "${run_dir}" --file "${archive_path}" ${source_elements[@]} || exit_on_fail + tar ${transformations[@]} "${verbose_flag}" "--${archive_operation}" --exclude-vcs --directory "${run_dir}" --file "${archive_path}" ${source_elements[@]} || exit_on_fail } # Given a filename it will sign the file with the default key @@ -2529,14 +2529,20 @@ function associate_array { # Safely loads config file or config from stdin # First parameter is filename, all consequent parameters are assumed to be # valid configuration parameters +# By default settings are printed when config is piped since variables set +# would only live in a subshell, this can be changed using the "print_settings" +# variable function load_config { # If first argument is a filename read it and pass it to the function if [ -f "${1:-}" ]; then - cat "${1}" | load_config "${@:2}" + print_settings='false' + load_config "${@:2}" < "${1}" return elif [ -t 0 ] ; then color_echo red "No config filename provided or data on stdin, exiting" return 1 + else + print_settings="${print_settings:-true}" fi # First command needs to be read, this way any piped input goes to it @@ -2554,6 +2560,13 @@ function load_config { done fi done + + # Print settings if needed when piping + if ${print_settings}; then + for setting in "${@}"; do + echo "${setting}=${!setting}" + done + fi } # Load settings from config file if they have not been set already From 3cb47a20fe7cac0efe359afbe3ef1df7db05c726 Mon Sep 17 00:00:00 2001 From: Antonia Stevens Date: Tue, 2 Jun 2020 12:48:38 +0000 Subject: [PATCH 4/6] Fix inline/offline update --- shtdlib.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/shtdlib.sh b/shtdlib.sh index 8e0e2e1..2bac638 100755 --- a/shtdlib.sh +++ b/shtdlib.sh @@ -2206,7 +2206,7 @@ function create_relative_archive { done # shellcheck disable=SC2068 - tar ${transformations[@]} "${verbose_flag}" "--${archive_operation}" --exclude-vcs --directory "${run_dir}" --file "${archive_path}" ${source_elements[@]} || exit_on_fail + tar ${transformations[@]} ${verbose_flag} --${archive_operation} --exclude-vcs --directory "${run_dir}" --file "${archive_path}" ${source_elements[@]} || exit_on_fail } # Given a filename it will sign the file with the default key @@ -2249,6 +2249,7 @@ function get_git_status { # Reads bash files and inlines any "source" references to a new file # If second parameter is empty or "-" the new file is printed to stdout +# Explicit inlines can be defined using "#inline_source path_to_file" declare -a processed_inline_sources=() function inline_bash_source { local inline_source_file="${1}" @@ -2263,7 +2264,13 @@ function inline_bash_source { local i for (( i=1; i Date: Tue, 16 Jun 2020 13:37:25 +0000 Subject: [PATCH 5/6] Replace awk with cut --- shtdlib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shtdlib.sh b/shtdlib.sh index 2bac638..3ef40f0 100755 --- a/shtdlib.sh +++ b/shtdlib.sh @@ -2556,7 +2556,7 @@ function load_config { readarray config_file for line in "${config_file[@]}"; do if [[ "${line}" =~ ^[^#]*= ]]; then - setting_name="$(echo "${line}" | awk -F '=' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" + setting_name="$(echo "${line}" | cut -f 1 -d '=' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" setting_value="$(echo "${line}" | cut -f 2 -d '=' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" for requested_setting in "${@}" ; do From e7ca9d4d26c884c510c3edfe2604dcc7aa36fcaf Mon Sep 17 00:00:00 2001 From: Antonia Stevens Date: Thu, 18 Jun 2020 15:49:21 +0000 Subject: [PATCH 6/6] Add logging/debug info for required arguments --- shtdlib.sh | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/shtdlib.sh b/shtdlib.sh index 3ef40f0..138ab06 100755 --- a/shtdlib.sh +++ b/shtdlib.sh @@ -1381,10 +1381,15 @@ EOF # This is mostly irrelevant when running in strict mode function required_argument { print_usage_function="${3:-print_usage}" - if [ -z "${!1}" ]; then - ${print_usage_function} - color_echo red "${2}" - exit 255 + if [ -n "${1:-}" ]; then + debug 10 "Processing required argument: ${1}" + if [ -z "${!1}" ]; then + ${print_usage_function} + color_echo red "${2}" + exit 255 + else + debug 10 "Argument: ${1} set to: ${!1}" + fi fi }