GitRoot

craft your forge, build your project, grow your community freely
 1#!/usr/bin/env bash
 2
 3# SPDX-FileCopyrightText: 2026 Romain Maneschi <romain@gitroot.dev>
 4#
 5# SPDX-License-Identifier: EUPL-1.2
 6
 7SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
 8
 9source "${SCRIPT_DIR}/report.sh"
10
11# wait_for_logs <logToWaitTo> <fileOfLogs> <opt afterLogs>
12#   Wait for a line which contain <logToWaitTo> after <opt afterLogs> in the <fileOfLogs>.
13function wait_for_logs() {
14    local pattern="$1"
15    local log_file="$2"
16    local after_pattern="$3"
17    local timeout=30
18    local start_time=$(date +%s)
19
20    if [[ ! -f "$log_file" ]]; then
21        report "🛑 Error: File $log_file not found."
22        return 1
23    fi
24
25    if [[ -n "$after_pattern" ]]; then
26        ( tail -n +1 -f "$log_file" | sed -u -n "/$after_pattern/,/$pattern/ { p; /$pattern/q; }" > /dev/null ) &
27    else
28        ( tail -n +1 -f "$log_file" | grep -q "$pattern" > /dev/null ) &
29    fi
30    
31    local pid=$!
32
33    ( sleep "$timeout"; kill "$pid" 2>/dev/null ) &
34    local timeout_pid=$!
35
36    wait "$pid" 2>/dev/null
37    local exit_code=$?
38
39    kill "$timeout_pid" 2>/dev/null
40
41    if [[ $exit_code -ne 0 ]]; then
42        report "🛑 Timeout reached waiting for '$pattern' in $log_file"
43        return 1
44    fi
45
46    local end_time=$(date +%s)
47    report "🕐 Found '$pattern' in $((end_time - start_time)) seconds"
48    return 0
49}