[IMP] simplify PostgreSQL container detection with readarray
Replace double grep pattern with readarray for cleaner container detection: - Single grep call instead of two - Native bash array instead of string manipulation - Array length check instead of grep -c - Proper formatting when listing multiple containers The readarray approach is more idiomatic and avoids edge cases with empty strings and newline handling.
This commit is contained in:
11
upgrade.sh
11
upgrade.sh
@@ -41,20 +41,19 @@ export FINALE_DB_NAME="ou${FINAL_VERSION}"
|
|||||||
readonly FINALE_DB_NAME
|
readonly FINALE_DB_NAME
|
||||||
readonly FINALE_SERVICE_NAME="${FINALE_DB_NAME}"
|
readonly FINALE_SERVICE_NAME="${FINALE_DB_NAME}"
|
||||||
|
|
||||||
postgres_containers=$(docker ps --format '{{.Names}}' | grep postgres || true)
|
readarray -t postgres_containers < <(docker ps --format '{{.Names}}' | grep postgres || true)
|
||||||
postgres_count=$(echo "$postgres_containers" | grep -c . || echo 0)
|
|
||||||
|
|
||||||
if [[ "$postgres_count" -eq 0 ]]; then
|
if [[ ${#postgres_containers[@]} -eq 0 ]]; then
|
||||||
log_error "No running PostgreSQL container found. Please start a PostgreSQL container and try again."
|
log_error "No running PostgreSQL container found. Please start a PostgreSQL container and try again."
|
||||||
exit 1
|
exit 1
|
||||||
elif [[ "$postgres_count" -gt 1 ]]; then
|
elif [[ ${#postgres_containers[@]} -gt 1 ]]; then
|
||||||
log_error "Multiple PostgreSQL containers found:"
|
log_error "Multiple PostgreSQL containers found:"
|
||||||
echo "$postgres_containers" >&2
|
printf ' %s\n' "${postgres_containers[@]}" >&2
|
||||||
log_error "Please ensure only one PostgreSQL container is running."
|
log_error "Please ensure only one PostgreSQL container is running."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export POSTGRES_SERVICE_NAME="$postgres_containers"
|
export POSTGRES_SERVICE_NAME="${postgres_containers[0]}"
|
||||||
readonly POSTGRES_SERVICE_NAME
|
readonly POSTGRES_SERVICE_NAME
|
||||||
|
|
||||||
log_step "INPUT PARAMETERS"
|
log_step "INPUT PARAMETERS"
|
||||||
|
|||||||
Reference in New Issue
Block a user