From e240392ec2302185757b9880789329638b9297c9 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sun, 16 Dec 2018 16:59:01 +0100 Subject: [PATCH] new: can interpret ``bash-stdout`` tags in relations options --- bin/compose-core | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/bin/compose-core b/bin/compose-core index 4fd87d3..f52a7de 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -1512,34 +1512,60 @@ export -f setup_host_resources relation-get () { - local key="$1" - cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null - if [ "$?" != 0 ]; then + local key="$1" out + if ! out=$(cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null); then err "The key $WHITE$key$NORMAL was not found in relation's data." return 1 fi + echo "$out" | yaml_get_interpret } export -f relation-get +yaml_get_interpret() { + local content tag + content=$(cat -) + tag=$(echo "$content" | shyaml -y get-value) || return 1 + tag="${tag%% *}" + content=$(echo "$content" | shyaml get-value) || return 1 + if ! [ "${tag:0:1}" == "!" ]; then + echo "$content" || return 1 + return 0 + fi + case "$tag" in + "!bash-stdout") + echo "$content" | bash || { + err "shell code didn't end with errorlevel 0" + return 1 + } + ;; + *) + err "Invalid object tag ${WHITE}$tag${NORMAL}" + return 1 + ;; + esac +} +export -f yaml_get_interpret + + relation-base-compose-get () { - local key="$1" - echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null - if [ "$?" != 0 ]; then + local key="$1" out + if ! out=$(echo "$RELATION_BASE_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 + echo "$out" | yaml_get_interpret } export -f relation-base-compose-get relation-target-compose-get () { - local key="$1" - echo "$RELATION_BASE_COMPOSE_DEF" | shyaml get-value "options.$key" 2>/dev/null - if [ "$?" != 0 ]; then + local key="$1" out + if ! out=$(echo "$RELATION_TARGET_COMPOSE_DEF" | shyaml 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 + echo "$out" | yaml_get_interpret } export -f relation-target-compose-get