From 2ea93868175749c95f012341d6dc01ea63a92fa1 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 24 Feb 2024 11:30:00 +0100 Subject: [PATCH] new: dev: [compose-core] refactoring out the ``if`` condition --- bin/compose-core | 82 +++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/bin/compose-core b/bin/compose-core index caaf9a9..e98dae9 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -1436,42 +1436,40 @@ export -f get_build_hash ## ## Returns on stdout the name of the image if found, or an empty string if not cache:image:registry:get() { - if [ -n "$COMPOSE_DOCKER_REGISTRY" ]; then - local charm="$1" hash="$2" service="$3" - local charm_image_name="cache/charm/$charm" - local charm_image="$charm_image_name:$hash" + local charm="$1" hash="$2" service="$3" + local charm_image_name="cache/charm/$charm" + local charm_image="$charm_image_name:$hash" - Elt "pulling ${DARKPINK}$charm${NORMAL} image from $COMPOSE_DOCKER_REGISTRY" >&2 - if out=$(docker pull "$COMPOSE_DOCKER_REGISTRY/$charm_image" 2>&1); then - docker tag "$COMPOSE_DOCKER_REGISTRY/$charm_image" "$charm_image" || { - err "Failed set image '$COMPOSE_DOCKER_REGISTRY/$charm_image' as '$charm_image'" \ - "for ${DARKYELLOW}$service${NORMAL}." - return 1 - } - print_info "found" >&2 - print_status success >&2 - Feed >&2 - printf "%s" "$charm_image" | tee "$cache_file" - return $? - fi - if [[ "$out" != *"manifest unknown"* ]]; then - print_status failure >&2 - Feed >&2 - err "Failed to pull image '$COMPOSE_DOCKER_REGISTRY/$charm_image'" \ - "for ${DARKYELLOW}$service${NORMAL}:" - e "$out" | prefix " ${GRAY}|${NORMAL} " >&2 + Elt "pulling ${DARKPINK}$charm${NORMAL} image from $COMPOSE_DOCKER_REGISTRY" >&2 + if out=$(docker pull "$COMPOSE_DOCKER_REGISTRY/$charm_image" 2>&1); then + docker tag "$COMPOSE_DOCKER_REGISTRY/$charm_image" "$charm_image" || { + err "Failed set image '$COMPOSE_DOCKER_REGISTRY/$charm_image' as '$charm_image'" \ + "for ${DARKYELLOW}$service${NORMAL}." return 1 - fi - print_info "not found" >&2 - if test "$type_method" = "long"; then - __status="[${NOOP}ABSENT${NORMAL}]" - else - echo -n "${NOOP}" - shift; shift; - echo -n "$*${NORMAL}" - fi >&2 + } + print_info "found" >&2 + print_status success >&2 Feed >&2 + printf "%s" "$charm_image" | tee "$cache_file" + return $? fi + if [[ "$out" != *"manifest unknown"* ]] && [[ "$out" != *"manifest"*"not found"* ]]; then + print_status failure >&2 + Feed >&2 + err "Failed to pull image '$COMPOSE_DOCKER_REGISTRY/$charm_image'" \ + "for ${DARKYELLOW}$service${NORMAL}:" + e "$out"$'\n' | prefix " ${GRAY}|${NORMAL} " >&2 + return 1 + fi + print_info "not found" >&2 + if test "$type_method" = "long"; then + __status="[${NOOP}ABSENT${NORMAL}]" + else + echo -n "${NOOP}" + shift; shift; + echo -n "$*${NORMAL}" + fi >&2 + Feed >&2 } export -f cache:image:registry:get @@ -1630,16 +1628,16 @@ service_ensure_image_ready() { fi ## Can we pull it ? Let's check on $COMPOSE_DOCKER_REGISTRY - - img=$(cache:image:registry:get "$charm" "$hash" "$service") || { - err "Failed to get image '$charm_image_name:$hash' from registry for ${DARKYELLOW}$service${NORMAL}." - return 1 - } - [ -n "$img" ] && { - printf "%s" "$img" | tee "$cache_file" - return $? - } - + if [ -n "$COMPOSE_DOCKER_REGISTRY" ]; then + img=$(cache:image:registry:get "$charm" "$hash" "$service") || { + err "Failed to get image '$charm_image_name:$hash' from registry for ${DARKYELLOW}$service${NORMAL}." + return 1 + } + [ -n "$img" ] && { + printf "%s" "$img" | tee "$cache_file" + return $? + } + fi cache:image:produce "$type" "$src" "$charm" "$hash" "$service" || return 1 cache:image:registry:put "$charm" "$hash" "$service" || return 1 printf "%s" "${charm_image_name}:$hash" | tee "$cache_file"