-
Notifications
You must be signed in to change notification settings - Fork 3
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
antevens
wants to merge
6
commits into
master
Choose a base branch
from
fix/load_config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8cbdb67
Fix load_config to allow it to read from stdin
antevens 0c1e885
Fix archive creation
antevens d63df3c
Fix load_config
antevens 3cb47a2
Fix inline/offline update
antevens 1ab0d77
Replace awk with cut
antevens e7ca9d4
Add logging/debug info for required arguments
antevens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
||
|
@@ -2206,7 +2211,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 +2254,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 +2269,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 | ||
|
@@ -2526,26 +2538,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' | ||
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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't figure out when we ever reach this case... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_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:]]*$//')" | ||
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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
So we can do something like:
There was a problem hiding this comment.
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 :)