cloverleaf-larry/bin/_nc_common.sh
Clover df1810cba7 v0.9.4: short on-PATH commands + live 3-level tab-completion + durable fixture
Make the toolkit usable BY HAND without the `larry tools <name>` prefix.

- bin/ of thin wrappers (tbn/tbp/tbh/tbpr/where/paths/route_test + a full-name
  passthrough per operator tool). Installer symlinks them into LARRY_BIN_DIR so
  `tbn adt` runs directly. Each resolves lib/ via bin/_nc_common.sh
  (LARRY_LIB_DIR -> ../lib -> $LARRY_HOME/lib) and execs the matching tool.
- -h/--help on every wrapper.
- bin/nc-completion.bash: dynamic bash completion, 3 levels (command / SITE /
  THREAD) enumerated LIVE from the NetConfig tree under $HCIROOT via the same
  lib/nc-parse.sh the tools use; cached per (HCIROOT, newest-NetConfig-mtime).
  Installer appends a guarded source line to the user's bash rc.
- fixtures/integrator: durable 3-site demo (epic->ancout->codamx) with cross-
  site fan-out + fan-in and a multi-route inbound. RESOLVES the v0.9.3 fixture
  conflict: cross-site destination blocks are XS_*-prefixed so they never
  collide with a local protocol name (a collision makes nc-paths
  _xsite_down_targets suppress the cross-site hop, lib/nc-paths.sh:378).
- DEFERRED: fetch-token.sh broker wiring (broker contract still finalizing).

VERSION+LARRY_VERSION -> 0.9.4; MANIFEST regenerated (--check clean); bash -n
clean; verified live on .135 (short commands off PATH + all 3 completion levels).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 11:58:25 -07:00

73 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# _nc_common.sh — shared resolver sourced by every bin/ short-command wrapper
# AND by the bash-completion script (bin/nc-completion.bash).
#
# It is NOT a standalone tool — it only defines helpers. The wrappers are thin:
# they resolve the lib/ toolkit dir, then exec the right lib/nc-*.sh with the
# user's args mapped to that tool's flags. Putting the wrappers on PATH is what
# makes `tbn adt` work directly (no `larry tools` / `nc-larry` prefix).
#
# Resolution order for the lib/ dir (first hit wins):
# 1. $LARRY_LIB_DIR if set and valid
# 2. <dir-of-this-bin>/../lib (repo layout: bin/.. /lib)
# 3. $LARRY_HOME/lib (installed layout)
# 4. <dir-of-this-bin>/../../lib (defensive: bin symlinked one level deeper)
#
# Site/thread enumeration is LIVE off the NetConfig tree under $HCIROOT, reusing
# the SAME parser the tools use (lib/nc-parse.sh) so completion can never drift
# from what the tools actually see.
# --- lib/ dir resolution -----------------------------------------------------
_nc_resolve_lib() {
if [ -n "${LARRY_LIB_DIR:-}" ] && [ -f "$LARRY_LIB_DIR/nc-parse.sh" ]; then
printf '%s' "$LARRY_LIB_DIR"; return 0
fi
# dir of the *real* file backing this source (follow one symlink level)
local src="${BASH_SOURCE[0]}"
if [ -L "$src" ]; then src="$(readlink "$src")"; fi
local bindir
bindir="$(cd "$(dirname "$src")" 2>/dev/null && pwd)"
local c
for c in "$bindir/../lib" "${LARRY_HOME:-$HOME/.larry}/lib" "$bindir/../../lib"; do
if [ -d "$c" ] && [ -f "$c/nc-parse.sh" ]; then
( cd "$c" && pwd ); return 0
fi
done
return 1
}
# --- HCIROOT resolution (env, then a couple of sane fallbacks) ----------------
_nc_hciroot() {
if [ -n "${HCIROOT:-}" ] && [ -d "$HCIROOT" ]; then printf '%s' "$HCIROOT"; return 0; fi
printf '%s' "${HCIROOT:-}"; return 0
}
# --- live site enumeration ----------------------------------------------------
# A "site" = an immediate subdir of $HCIROOT that contains a NetConfig file.
# This is what nc-find / nc-paths actually walk, so completion stays truthful.
_nc_sites() {
local root; root="$(_nc_hciroot)"
[ -n "$root" ] && [ -d "$root" ] || return 0
local d
for d in "$root"/*/; do
[ -f "${d}NetConfig" ] && basename "${d%/}"
done 2>/dev/null
}
# --- live thread enumeration --------------------------------------------------
# Every protocol (thread) name across every site's NetConfig, deduped. Uses the
# library parser so it matches the tools' view exactly. Optional arg = restrict
# to one site.
_nc_threads() {
local only_site="${1:-}"
local root lib; root="$(_nc_hciroot)"; lib="$(_nc_resolve_lib)" || return 0
[ -n "$root" ] && [ -d "$root" ] || return 0
local nc site
for nc in "$root"/*/NetConfig; do
[ -f "$nc" ] || continue
site="$(basename "$(dirname "$nc")")"
[ -n "$only_site" ] && [ "$site" != "$only_site" ] && continue
bash "$lib/nc-parse.sh" list-protocols "$nc" 2>/dev/null
done | sort -u
}