From 08129405590bf7a676e22e70003519388ed87234 Mon Sep 17 00:00:00 2001 From: Ed Morley <501702+edmorley@users.noreply.github.com> Date: Fri, 8 Mar 2024 12:25:21 +0000 Subject: [PATCH] Fix ANSI escape code wrapping of multi-line deprecation messages Previously the colourisation of the deprecation messages would break if the output had prefixes added later (for example as happens when using an untrusted builder, or when using Git). Now, each line is wrapped in its own colour code + reset code, which resolves the issue. --- .../end-of-life-buildpack/bin/build | 17 ++++++++++++----- buildpacks-20/end-of-life-buildpack/bin/build | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/builder-classic-22/end-of-life-buildpack/bin/build b/builder-classic-22/end-of-life-buildpack/bin/build index 0245d9ce..8f61a62e 100755 --- a/builder-classic-22/end-of-life-buildpack/bin/build +++ b/builder-classic-22/end-of-life-buildpack/bin/build @@ -2,13 +2,20 @@ set -euo pipefail +ANSI_RED="\033[1;31m" +ANSI_RESET="\033[0m" + function display_error() { - local ansi_red="\033[1;31m" - local ansi_reset="\033[0m" - echo -e "\n${ansi_red}${1}${ansi_reset}\n" >&2 + echo >&2 + # We have to ANSI wrap each line separately to prevent breakage if line prefixes are added + # later (such as when the builder is untrusted, or when Git adds the "remote:" prefix). + while IFS= read -r line; do + echo -e "${ANSI_RED}${line}${ANSI_RESET}" >&2 + done <<< "${1}" + echo >&2 } -read -r -d '' EOL_MESSAGE <<'EOF' || true +display_error "$(cat <<'EOF' ####################################################################### WARNING: This builder image (heroku/builder-classic:22) is deprecated, @@ -31,6 +38,6 @@ documentation for how to adjust the builder image used for your build. ####################################################################### EOF +)" -display_error "${EOL_MESSAGE}" exit 0 diff --git a/buildpacks-20/end-of-life-buildpack/bin/build b/buildpacks-20/end-of-life-buildpack/bin/build index e0447ba6..1096c084 100755 --- a/buildpacks-20/end-of-life-buildpack/bin/build +++ b/buildpacks-20/end-of-life-buildpack/bin/build @@ -2,13 +2,20 @@ set -euo pipefail +ANSI_RED="\033[1;31m" +ANSI_RESET="\033[0m" + function display_error() { - local ansi_red="\033[1;31m" - local ansi_reset="\033[0m" - echo -e "\n${ansi_red}${1}${ansi_reset}\n" >&2 + echo >&2 + # We have to ANSI wrap each line separately to prevent breakage if line prefixes are added + # later (such as when the builder is untrusted, or when Git adds the "remote:" prefix). + while IFS= read -r line; do + echo -e "${ANSI_RED}${line}${ANSI_RESET}" >&2 + done <<< "${1}" + echo >&2 } -read -r -d '' EOL_MESSAGE <<'EOF' || true +display_error "$(cat <<'EOF' ####################################################################### WARNING: This builder image (heroku/buildpacks:20) is deprecated, @@ -31,6 +38,6 @@ documentation for how to adjust the builder image used for your build. ####################################################################### EOF +)" -display_error "${EOL_MESSAGE}" exit 0