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
 7_init() {
 8    local SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
 9    source "${SCRIPT_DIR}/report.sh"
10}
11
12_init
13unset -f _init
14
15# wait_for_logs <logToWaitTo> <fileOfLogs> <opt afterLogs>
16#   Wait for a line which contain <logToWaitTo> after <opt afterLogs> in the <fileOfLogs>.
17function wait_for_logs() {
18    local pattern="$1"
19    local log_file="$2"
20    local after_pattern="$3"
21    local timeout=30
22    local start_time=$(date +%s)
23
24    if [[ ! -f "$log_file" ]]; then
25        report "🛑 Error: File $log_file not found."
26        return 1
27    fi
28
29    if [[ -n "$after_pattern" ]]; then
30        ( tail -n +1 -f "$log_file" | sed -u -n "/$after_pattern/,/$pattern/ { p; /$pattern/q; }" > /dev/null ) &
31    else
32        ( tail -n +1 -f "$log_file" | grep -q "$pattern" > /dev/null ) &
33    fi
34    
35    local pid=$!
36
37    ( sleep "$timeout"; kill "$pid" 2>/dev/null ) &
38    local timeout_pid=$!
39
40    wait "$pid" 2>/dev/null
41    local exit_code=$?
42
43    kill "$timeout_pid" 2>/dev/null
44
45    if [[ $exit_code -ne 0 ]]; then
46        report "🛑 Timeout reached waiting for '$pattern' in $log_file"
47        return 1
48    fi
49
50    local end_time=$(date +%s)
51    report "🕐 Found '$pattern' in $((end_time - start_time)) seconds"
52    return 0
53}