Browse Source

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.
hostresources
Valentin Lab 5 years ago
parent
commit
f046355186
  1. 39
      bin/compose-core

39
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

Loading…
Cancel
Save