Skip to content

Commit ae67947

Browse files
committed
Handle potential json output when multiple buildpacks are detected
1 parent cc46ea3 commit ae67947

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

include/buildpack.bash

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,27 @@ _select-buildpack() {
5353
unprivileged "$buildpack/bin/detect" "$build_path" &>/dev/null && valid_buildpacks+=("$buildpack")
5454
done
5555
if [[ ${#valid_buildpacks[@]} -gt 1 ]]; then
56-
title "Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used."
57-
# shellcheck disable=SC2001
58-
echo "Detected buildpacks: $(sed -e "s:/tmp/buildpacks/[0-9][0-9]_buildpack-::g" <<<"${valid_buildpacks[@]}")" | indent
56+
if [[ "${BUILDPACK_DETECT_OUTPUT}" == "json" ]]; then
57+
buildpack_list="["
58+
for bp in "${valid_buildpacks[@]}"; do
59+
# Strip leading number and "buildpack-" prefix
60+
buildpack_id=$(basename "$bp")
61+
buildpack_id="${buildpack_id#*_buildpack-}"
62+
buildpack_name=$(unprivileged "$bp/bin/detect" "$build_path")
63+
buildpack_list+="{\"name\": \"$buildpack_name\", \"id\": \"$buildpack_id\"},"
64+
done
65+
buildpack_list="${buildpack_list%,}]"
66+
67+
jq -n \
68+
--arg message "Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used." \
69+
--argjson buildpacks "$buildpack_list" \
70+
'{message: $message, buildpacks: $buildpacks}'
71+
exit 0
72+
else
73+
title "Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used."
74+
# shellcheck disable=SC2001
75+
echo "Detected buildpacks: $(sed -e "s:/tmp/buildpacks/[0-9][0-9]_buildpack-::g" <<<"${valid_buildpacks[@]}")" | indent
76+
fi
5977
fi
6078
if [[ ${#valid_buildpacks[@]} -gt 0 ]]; then
6179
selected_path="${valid_buildpacks[0]}"

0 commit comments

Comments
 (0)