From c9d4d151acef59208f11cbfe40524c04c898d028 Mon Sep 17 00:00:00 2001 From: redxef Date: Mon, 21 Nov 2022 23:59:23 +0100 Subject: [PATCH] Allow writing of logfiles instead of all to stderr. --- src/common | 11 +++++++++++ src/scripts/put_files.sh | 34 ++++++++++++++++++++++++++++------ src/scripts/put_files.vars | 2 ++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/common b/src/common index 79e387e..9a0bd83 100755 --- a/src/common +++ b/src/common @@ -68,6 +68,13 @@ get_version() { get_command() { jq -r '.params.command // ["true"]' < "$INPUT_PATH" | jq -r '.[]' | tr '\n"' " '" } +get_stdout_file() { + jq -r '.params.stderr_file // "-"' | jq -r tostring +} +get_stdout_file() { + jq -r '.params.stdout_file // "-"' | jq -r tostring +} + HOSTNAME="$(get_hostname)" PORT="$(get_port)" USER="$(get_user)" @@ -78,6 +85,8 @@ DIRECTORY="$(get_directory)" WORKDIR="$(get_workdir)" VERSION="$(get_version)" COMMAND="$(get_command)" +STDERR_FILE="$(get_stderr_file)" +STDOUT_FILE="$(get_stdout_file)" export HOSTNAME export PORT @@ -89,6 +98,8 @@ export DIRECTORY export WORKDIR export VERSION export COMMAND +export STDERR_FILE +export STDOUT_FILE write_config() { config="$(tmpfile)" diff --git a/src/scripts/put_files.sh b/src/scripts/put_files.sh index 55587a2..9f4fd3b 100644 --- a/src/scripts/put_files.sh +++ b/src/scripts/put_files.sh @@ -8,13 +8,35 @@ directory="${DIRECTORY}" workdir="${WORKDIR}" # shellcheck disable=SC2153 extract="${EXTRACT}" +# shellcheck disable=SC2153 +stderr_file="${STDERR_FILE}" +# shellcheck disable=SC2153 +stdout_file="${STDOUT_FILE}" + ( - if "$extract"; then - cd "$directory" - tar x - fi + if "$extract"; then + cd "$directory" + tar x + fi ) ( - cd "$workdir" - ${COMMAND} >&2 + cd "$workdir" + [ "$stderr_file" != "-" ] && truncate -s 0 "$stderr_file" + [ "$stdout_file" != "-" ] && truncate -s 0 "$stdout_file" + + # don't write anything to stdout, since that would get appended to the tar file + if [ "$stderr_file" = "-" ] && [ "$stdout_file" = "-" ]; then + # redirect stdout to stderr + ${COMMAND} 1>&2 + elif [ "$stderr_file" != "-" ] && [ "$stdout_file" = "-" ]; then + # stderr to file, stdout to stderr + ( ${COMMAND} 2>"$stderr_file" ) 1>&2 + + elif [ "$stderr_file" = "-" ] && [ "$stdout_file" != "-" ]; then + # stdout to file + ${COMMAND} 1>"$stdout_file" + else + # both files specified + ${COMMAND} 1>"$stdout_file" 2>"$stderr_file" + fi ) diff --git a/src/scripts/put_files.vars b/src/scripts/put_files.vars index 50186e7..3a438ef 100644 --- a/src/scripts/put_files.vars +++ b/src/scripts/put_files.vars @@ -2,3 +2,5 @@ ${DIRECTORY} ${EXTRACT} ${WORKDIR} ${COMMAND} +${STDERR_FILE} +${STDOUT_FILE}