#!/usr/bin/env bash # _common.sh — sourced by all dev-scripts. Do not run directly. set -euo pipefail # ── Colores ──────────────────────────────────────────────────────────────── RED='\033[0;31m' GRN='\033[0;32m' YLW='\033[0;33m' BLU='\033[0;34m' DIM='\033[2m' RST='\033[0m' ok() { echo -e "${GRN}✓${RST} $*"; } info() { echo -e "${BLU}→${RST} $*"; } warn() { echo -e "${YLW}!${RST} $*"; } fail() { echo -e "${RED}✗${RST} $*" >&2; exit 1; } dim() { echo -e "${DIM}$*${RST}"; } # ── Repo root ────────────────────────────────────────────────────────────── # Scripts can be called from any directory; we always operate from repo root. REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$REPO_ROOT" # ── .env loader ─────────────────────────────────────────────────────────── load_env() { local env_file="${1:-.env}" [[ -f "$env_file" ]] || fail ".env not found at $REPO_ROOT/$env_file — copy .env.example and fill it in" # Export only lines that are KEY=VALUE (skip comments and blanks) set -o allexport # shellcheck disable=SC1090 source <(grep -E '^[A-Z_]+=.+' "$env_file") set +o allexport } # ── Go tooling ───────────────────────────────────────────────────────────── GO=${GO_BIN:-go} export PATH="$PATH:/usr/local/go/bin" command -v "$GO" &>/dev/null || fail "go not found — install Go or set GO_BIN" # ── Process helpers ──────────────────────────────────────────────────────── RUN_DIR="$REPO_ROOT/run" mkdir -p "$RUN_DIR" pid_file() { echo "$RUN_DIR/$1.pid"; } log_file() { echo "$RUN_DIR/$1.log"; } read_pid() { local f; f="$(pid_file "$1")" [[ -f "$f" ]] && cat "$f" || echo 0 } is_running() { local pid; pid="$(read_pid "$1")" [[ "$pid" -gt 0 ]] && kill -0 "$pid" 2>/dev/null } agent_status() { local id="$1" enabled="$2" if [[ "$enabled" != "true" ]]; then echo "disabled" elif is_running "$id"; then echo "running" else echo "stopped" fi } # ── Agent discovery ──────────────────────────────────────────────────────── # Prints: id|version|enabled|description (one line per agent) list_agents_raw() { for cfg in agents/*/config.yaml; do [[ -f "$cfg" ]] || continue local id version enabled desc id=$(grep -m1 '^ id:' "$cfg" | awk '{print $2}') version=$(grep -m1 '^ version:' "$cfg" | awk '{print $2}' | tr -d '"') enabled=$(grep -m1 '^ enabled:' "$cfg" | awk '{print $2}') desc=$(grep -m1 '^ description:' "$cfg" | cut -d'"' -f2) [[ -n "$id" ]] && echo "${id}|${version}|${enabled}|${desc}|${cfg}" done } # ── Usage helper ────────────────────────────────────────────────────────── need_arg() { [[ -n "${1:-}" ]] || { echo "Usage: $0 "; exit 1; } }