Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix load_config to allow it to read from stdin #77

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions shtdlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand All @@ -2263,7 +2264,13 @@ function inline_bash_source {
local i
for (( i=1; i<lines+1; i++ )) ; do
local filename
filename="$(echo "${source_file_array[${i}-1]}" | grep '^source ' | awk '{print $2}')"
filename="$(echo "${source_file_array[${i}-1]}" | grep -e '^source ' -e '^import ' -e '^import_lib' -e '^#inline_source ' | awk '{print $2}')"
debug 10 "Processing source file: ${filename}"
interpolated_filename="$(echo ${filename//\"} | envsubst)"
if [ "${filename}" != "${interpolated_filename}" ] ; then
color_echo red "Warning, environment variable found in source/import statement '${filename}', note that variable(s) need to be set/populated when inlining the source, cautiosly proceeding"
filename="${interpolated_filename}"
fi
if [ "${filename}" != "" ] ; then
debug 10 "Found line with source instruction to file: ${filename}"
local relative_filename
Expand Down Expand Up @@ -2526,26 +2533,47 @@ 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
# 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 {
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
print_settings='false'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we allow flexibility here to allow printing?

E.g.

Suggested change
print_settings='false'
print_settings="${print_settings:-false}"

So we can do something like:

print_settings=true load_config  config foo
Loaded config parameter foo with value of 'bar'
foo=bar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do .. and we don't.

Print Setting will actually cause issues because we're allowing piping but it does not matter because that particular case will recurse so it will pick up the setting on the second run :)

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}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't figure out when we ever reach this case...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason my reply is missing, think about the function being called iteratively ...

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:]]*$//')"
antevens marked this conversation as resolved.
Show resolved Hide resolved

for requested_setting in "${@:2}" ; do
for requested_setting in "${@}" ; do
if [ "${requested_setting}" == "${setting_name}" ] ; then
export "${setting_name}"="${setting_value}"
antevens marked this conversation as resolved.
Show resolved Hide resolved
debug 10 "Loaded config parameter ${setting_name} with value of '${setting_value}'"
fi
done
fi
done < "${config_file}";
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
Expand Down