From f0463551865c076ab0263b7a9ed97948ff641a5a Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Mon, 17 Dec 2018 11:26:42 +0100 Subject: [PATCH] new: ``!expand-vars`` tag for relations and options. Will interpret any code like if it was in a shell here-doc string. This will interpret basically ``$VAR`` and ``$()`` constructs. --- bin/compose-core | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/bin/compose-core b/bin/compose-core index 562a6a6..1c85c14 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -1513,7 +1513,7 @@ export -f setup_host_resources relation-get () { local key="$1" out - if ! out=$(cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null); then + if ! out=$(cat "$RELATION_DATA_FILE" | shyaml -y get-value "$key" 2>/dev/null); then err "The key $WHITE$key$NORMAL was not found in relation's data." return 1 fi @@ -1522,6 +1522,35 @@ relation-get () { export -f relation-get +expand_vars() { + local unlikely_prefix="UNLIKELY_PREFIX" + content=$(cat -) + ## find first identifier not in content + remaining_lines=$(echo "$content" | grep "^$unlikely_prefix") + while [ "$(echo "$remaining_lines" | grep "^$unlikely_prefix$")" ]; do + size_prefix="${#unlikely_prefix}" + first_matching=$(echo "$remaining_lines" | + grep -v "^$unlikely_prefix$" | + uniq -w "$((size_prefix + 1))" -c | + sort -rn | + head -n 1) + first_matching=${first_matching#"${x%%[![:space:]]*}"} + first_matching="${first_matching#* }" + next_char=${first_matching:$size_prefix:1} + if [ "$next_char" != "0" ]; then + unlikely_prefix+="0" + else + unlikely_prefix+="1" + fi + remaining_lines=$(echo "$remaining_lines" | grep "^$unlikely_prefix") + done + eval "cat <<$unlikely_prefix +$content +$unlikely_prefix" +} +export -f expand_vars + + yaml_get_interpret() { local content tag content=$(cat -) @@ -1539,6 +1568,12 @@ yaml_get_interpret() { return 1 } ;; + "!var-expand") + echo "$content" | expand_vars || { + err "shell expansion failed" + return 1 + } + ;; *) err "Invalid object tag ${WHITE}$tag${NORMAL}" return 1 @@ -1573,7 +1608,7 @@ export -f relation-base-compose-get relation-target-compose-get () { local key="$1" out - if ! out=$(echo "$RELATION_TARGET_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null); then + if ! out=$(echo "$RELATION_TARGET_COMPOSE_DEF" | shyaml -y get-value "options.$key" 2>/dev/null); then err "The key $WHITE$key$NORMAL was not found in base service compose definition.." return 1 fi