From 8c0d97facbe3dba61b781adefff020a09db25df5 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Thu, 15 Feb 2024 13:09:55 +0100 Subject: [PATCH] new: [compose-core] provide ``get_service_incoming_relations`` to list services linked to a service --- bin/compose-core | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/bin/compose-core b/bin/compose-core index 5706777..5a154bf 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -2209,6 +2209,37 @@ get_service_relation() { export -f get_service_relation +## From a service and a relation, get all relations targeting given +## service with given relation. +## +## Returns a NUL separated list of couple of: +## (base_service, relation_config) +## +get_service_incoming_relations() { + local service="$1" relation="$2" cache_file="$state_tmpdir/$FUNCNAME.cache.$(H "$@" "$ALL_RELATIONS")" \ + s rn ts rc td + if [ -e "$cache_file" ]; then + #debug "$FUNCNAME: SESSION cache hit $1" + cat "$cache_file" + return 0 + fi + if [ -z "$ALL_RELATIONS" ]; then + err "Can't access global \$ALL_RELATIONS" + return 1 + fi + + while read-0 s rn ts rc _td; do + [[ "$ts" == "$service" ]] || continue + [[ "$rn" == "$relation" ]] || continue + relation_data_file=$(get_relation_data_file "$s" "$ts" "$rn" "$rc") || return 1 + printf "%s\0" "$s" "$(cat "$relation_data_file")" + done < <(cat "$ALL_RELATIONS") > "$cache_file" + + cat "$cache_file" +} +export -f get_service_incoming_relations + + export TRAVERSE_SEPARATOR=: ## Traverse on first service satisfying relation service:traverse() {