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

feat: add redirect_stderr_to_stdout propery #291

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

## DEVELOPMENT VERSION

### New Artifacts Properties

- Added the new 'redirect_stderr_to_stdout' property, an optional feature available exclusively for the command collector. When set to true, this property redirects all error messages (stderr) to standard output (stdout), ensuring they are written to the output file.
10 changes: 6 additions & 4 deletions lib/command_collector.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# string output_directory: ful path to the output directory
# string output_file: output file name (optional)
# boolean compress_output_file: compress output file (optional) (default: false)
# boolean redirect_stderr_to_stdout: redirect stderr to stdout (optional) (default: false)
# Returns:
# none
_command_collector()
Expand All @@ -18,6 +19,7 @@ _command_collector()
__cc_output_directory="${3:-}"
__cc_output_file="${4:-}"
__cc_compress_output_file="${5:-false}"
__cc_redirect_stderr_to_stdout="${6:-false}"

if [ -z "${__cc_command}" ]; then
_log_msg ERR "_command_collector: empty command parameter"
Expand Down Expand Up @@ -59,7 +61,7 @@ _command_collector()
fi

_verbose_msg "${__UAC_VERBOSE_CMD_PREFIX}${__cc_new_command}"
_run_command "${__cc_new_command}" \
_run_command "${__cc_new_command}" "${__cc_redirect_stderr_to_stdout}" \
>>"${__cc_new_output_directory}/${__cc_new_output_file}"

# remove output file if it is empty
Expand All @@ -77,7 +79,7 @@ _command_collector()
_verbose_msg "${__UAC_VERBOSE_CMD_PREFIX}${__cc_new_command}"
(
cd "${__cc_new_output_directory}" \
&& _run_command "${__cc_new_command}"
&& _run_command "${__cc_new_command}" "${__cc_redirect_stderr_to_stdout}"
)
fi
done
Expand All @@ -96,7 +98,7 @@ _command_collector()
__cc_command="${__cc_command} | gzip - | cat -"
fi
_verbose_msg "${__UAC_VERBOSE_CMD_PREFIX}${__cc_command}"
_run_command "${__cc_command}" \
_run_command "${__cc_command}" "${__cc_redirect_stderr_to_stdout}" \
>>"${__cc_output_directory}/${__cc_output_file}"

# remove output file if it is empty
Expand All @@ -114,7 +116,7 @@ _command_collector()
_verbose_msg "${__UAC_VERBOSE_CMD_PREFIX}${__cc_command}"
(
cd "${__cc_output_directory}" \
&& _run_command "${__cc_command}"
&& _run_command "${__cc_command}" "${__cc_redirect_stderr_to_stdout}"
)
fi

Expand Down
18 changes: 12 additions & 6 deletions lib/parse_artifact.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ _parse_artifact()
__pa_path_pattern=""
__pa_path=""
__pa_permissions=""
__pa_redirect_stderr_to_stdout=false
__pa_supported_os=""
}
_cleanup_local_vars
Expand Down Expand Up @@ -93,14 +94,14 @@ _parse_artifact()
# run global condition command and skip collection if exit code is greater than 0
if echo "${__pa_condition}" | grep -q -E "^!"; then
__pa_condition=`echo "${__pa_condition}" | sed -e 's|^! *||' 2>/dev/null`
if _run_command "${__pa_condition}" >/dev/null; then
if _run_command "${__pa_condition}" true >/dev/null; then
_log_msg DBG "Global condition '${__pa_condition}' not satisfied. Skipping..."
return 1
else
_log_msg DBG "Global condition '${__pa_condition}' satisfied"
fi
else
if _run_command "${__pa_condition}" >/dev/null; then
if _run_command "${__pa_condition}" true >/dev/null; then
_log_msg DBG "Global condition '${__pa_condition}' satisfied"
else
_log_msg DBG "Global condition '${__pa_condition}' not satisfied. Skipping..."
Expand Down Expand Up @@ -201,6 +202,9 @@ _parse_artifact()
"permissions:")
__pa_permissions=`echo "${__pa_value}" | _array_to_psv 2>/dev/null`
;;
"redirect_stderr_to_stdout:")
__pa_redirect_stderr_to_stdout="${__pa_value}"
;;
"supported_os:")
__pa_supported_os=`echo "${__pa_value}" | _array_to_psv 2>/dev/null`
;;
Expand Down Expand Up @@ -232,15 +236,15 @@ _parse_artifact()
# run local condition command and skip collection if exit code greater than 0
if echo "${__pa_condition}" | grep -q -E "^!"; then
__pa_condition=`echo "${__pa_condition}" | sed -e 's|^! *||' 2>/dev/null`
if _run_command "${__pa_condition}" false >/dev/null; then
if _run_command "${__pa_condition}" true >/dev/null; then
_log_msg DBG "Condition '${__pa_condition}' not satisfied. Skipping..."
_cleanup_local_vars
continue
else
_log_msg DBG "Condition '${__pa_condition}' satisfied"
fi
else
if _run_command "${__pa_condition}" false >/dev/null; then
if _run_command "${__pa_condition}" true >/dev/null; then
_log_msg DBG "Condition '${__pa_condition}' satisfied"
else
_log_msg DBG "Condition '${__pa_condition}' not satisfied. Skipping..."
Expand Down Expand Up @@ -326,7 +330,8 @@ _parse_artifact()
"${__pa_new_command}" \
"${__pa_new_output_directory}" \
"${__pa_new_output_file}" \
"${__pa_compress_output_file}"
"${__pa_compress_output_file}" \
"${__pa_redirect_stderr_to_stdout}"
elif [ "${__pa_collector}" = "file" ]; then
_find_based_collector \
"file" \
Expand Down Expand Up @@ -408,7 +413,8 @@ _parse_artifact()
"${__pa_command}" \
"${__pa_output_directory}" \
"${__pa_output_file}" \
"${__pa_compress_output_file}"
"${__pa_compress_output_file}" \
"${__pa_redirect_stderr_to_stdout}"
elif [ "${__pa_collector}" = "file" ]; then
_find_based_collector \
"file" \
Expand Down
44 changes: 0 additions & 44 deletions lib/presigned_url_transfer.sh

This file was deleted.

32 changes: 16 additions & 16 deletions lib/run_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
# Run command.
# Arguments:
# string command: command (including arguments)
# boolean log_stderr: send stderr to uac.log (optional) (default: true)
# boolean redirect_stderr_to_stdout: redirect stderr to stdout (optional) (default: false)
# Returns:
# integer: command exit code
_run_command()
{
__rc_command="${1:-}"
__rc_log_stderr="${2:-true}"
__rc_redirect_stderr_to_stdout="${2:-false}"
__rc_stderr_file="${__UAC_TEMP_DATA_DIR}/run_command.stderr.txt"

if [ -z "${__rc_command}" ]; then
_log_msg ERR "_run_command: empty command parameter"
Expand All @@ -22,24 +23,23 @@ _run_command()
return 1
fi

__rc_stderr_file="/dev/null"
if ${__rc_log_stderr}; then
__rc_stderr_file="${__UAC_TEMP_DATA_DIR}/run_command.stderr.txt"
fi
if ${__rc_redirect_stderr_to_stdout}; then
eval "${__rc_command}" 2>&1
__rc_exit_code="$?"
else
eval "${__rc_command}" 2>"${__rc_stderr_file}"
__rc_exit_code="$?"

eval "${__rc_command}" \
2>"${__rc_stderr_file}"
__rc_exit_code="$?"
__rc_stderr=""
if [ -s "${__rc_stderr_file}" ]; then
__rc_stderr=`awk 'BEGIN {ORS="/n"} {print $0}' "${__rc_stderr_file}" | sed -e 's|/n$||' 2>/dev/null`
__rc_stderr=" 2> ${__rc_stderr}"
fi

__rc_stderr=""
if [ -s "${__UAC_TEMP_DATA_DIR}/run_command.stderr.txt" ] && ${__rc_log_stderr}; then
__rc_stderr=`awk 'BEGIN {ORS="/n"} {print $0}' "${__UAC_TEMP_DATA_DIR}/run_command.stderr.txt" | sed -e 's|/n$||' 2>/dev/null`
__rc_stderr=" 2> ${__rc_stderr}"
__rc_command=`echo "${__rc_command}" | awk 'BEGIN {ORS="/n"} {print $0}' | sed -e 's| *| |g' -e 's|/n$||' 2>/dev/null`
_log_msg CMD "${__rc_command}${__rc_stderr}"
fi

__rc_command=`echo "${__rc_command}" | awk 'BEGIN {ORS="/n"} {print $0}' | sed -e 's| *| |g' -e 's|/n$||' 2>/dev/null`
_log_msg CMD "${__rc_command}${__rc_stderr}"

return "${__rc_exit_code}"

}
12 changes: 12 additions & 0 deletions lib/validate_artifact.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ _validate_artifact()
__va_path_pattern=""
__va_path=""
__va_permissions=""
__va_redirect_stderr_to_stdout=""
__va_supported_os=""
}
_cleanup_local_vars
Expand Down Expand Up @@ -336,6 +337,13 @@ _validate_artifact()
done
__va_permissions="${__va_value}"
;;
"redirect_stderr_to_stdout:")
if [ "${__va_value}" != true ] && [ "${__va_value}" != false ]; then
_error_msg "artifact: 'redirect_stderr_to_stdout' must be 'true' or 'false'."
return 1
fi
__va_redirect_stderr_to_stdout="${__va_value}"
;;
"supported_os:")
if echo "${__va_value}" | grep -q -v -E "^\[.*\]$"; then
_error_msg "artifact: 'supported_os' must be an array/list."
Expand Down Expand Up @@ -465,6 +473,10 @@ _validate_artifact()
_error_msg "artifact: invalid 'compress_output_file' property for '${__va_collector}' collector."
return 1
fi
if [ -n "${__va_redirect_stderr_to_stdout}" ]; then
_error_msg "artifact: invalid 'redirect_stderr_to_stdout' property for '${__va_collector}' collector."
return 1
fi
if [ -n "${__va_foreach}" ]; then
_error_msg "artifact: invalid 'foreach' property for '${__va_collector}' collector."
return 1
Expand Down