| garciadeblas | 70461c5 | 2024-07-03 09:17:56 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | |
| 16 | # "Debug mode" variable |
| 17 | DEBUG="${DEBUG:-}" |
| 18 | [[ "${DEBUG,,}" == "true" ]] && set -x |
| 19 | |
| 20 | # If there is an input stream, dumps it into a temporary file and sets it as INFILE |
| 21 | if [[ -n "${INSTREAM}" ]]; |
| 22 | then |
| 23 | # Save input stream to temporary file |
| 24 | TMPFILE=$(mktemp /tmp/INSTREAM.XXXXXXXXXX) || exit 1 |
| 25 | echo "${INSTREAM}" > "${TMPFILE}" |
| 26 | export INFILE="${TMPFILE}" |
| 27 | fi |
| 28 | |
| 29 | # Sets default INPUT and OUTPUT |
| 30 | INFILE="${INFILE:-/dev/stdin}" |
| 31 | OUTFILE="${OUTFILE:-/dev/stdout}" |
| 32 | |
| 33 | # Loads helper functions and KRM functions |
| 34 | source /app/scripts/library/helper-functions.rc |
| 35 | source /app/scripts/library/krm-functions.rc |
| 36 | |
| 37 | # If applicable, loads additional environment variables |
| 38 | if [[ -n "${CUSTOM_ENV}" ]]; |
| 39 | then |
| 40 | set -a |
| 41 | source <(echo "${CUSTOM_ENV}") |
| 42 | set +a |
| 43 | fi |
| 44 | |
| 45 | # In case INFILE and OUTFILE are the same, it uses a temporary output file |
| 46 | if [[ "${INFILE}" == "${OUTFILE}" ]]; |
| 47 | then |
| 48 | TMPOUTFILE="$(mktemp "/results/OUTFILE.XXXXXXXXXX")" || exit 1 |
| 49 | else |
| 50 | TMPOUTFILE="${OUTFILE}" |
| 51 | fi |
| 52 | |
| 53 | #################### EXECUTION #################### |
| 54 | # Debug mode: |
| 55 | if [[ "${DEBUG,,}" == "true" ]]; |
| 56 | then |
| 57 | "$@" < "${INFILE}" | tee "${TMPOUTFILE}" |
| 58 | # Normal mode: |
| 59 | else |
| 60 | "$@" < "${INFILE}" > "${TMPOUTFILE}" |
| 61 | fi |
| 62 | ################################################### |
| 63 | |
| 64 | # In case INFILE and OUTFILE are the same, it renames the temporary file over the OUTFILE (i.e., the same as INFILE) |
| 65 | if [[ "${INFILE}" == "${OUTFILE}" ]]; |
| 66 | then |
| 67 | mv -f "${TMPOUTFILE}" "${OUTFILE}" |
| 68 | fi |