omnixient/modules/dev-env/scripts/nav.sh
2026-06-28 09:59:05 +02:00

203 lines
6.1 KiB
Bash

#!/usr/bin/env bash
# dev-env: navigation helpers
#
# Sourced by user shells (not invoked as a script) — placed in
# /etc/profile.d/dev-env-nav.sh by config.nix. Every function reads
# /etc/dev-env/config.sh at call time so adding a new worktree on disk
# is immediately visible without a nixos-rebuild.
#
# Bugfix vs ~/dev/.devenv.d/00-navigation.sh: the original defined
# `ln() { sn lamassu-next; }` which shadows /usr/bin/ln. That's a real
# footgun — interactive shells could no longer create symlinks without
# `command ln`. We drop `ln` and add `lam` instead.
# Shared config loader (_devenv_load_config) lives in /etc/dev-env/lib.sh.
if [[ -r /etc/dev-env/lib.sh ]]; then
# shellcheck disable=SC1091
source /etc/dev-env/lib.sh
fi
# Navigate to the dev root.
dev() {
_devenv_load_config
cd "$DEV_ROOT" || return 1
}
# Navigate to an lnbits worktree.
# Usage: lb [env] env ∈ {dev, main} or whatever the filesystem shows
lb() {
_devenv_load_config
local env="${1:-}"
local lnbits_dir="${LNBITS_DIR:-$DEV_ROOT/lnbits}"
if [[ -z "$env" ]]; then
echo "Usage: lb <worktree>"
echo ""
echo "Current lnbits worktrees:"
if [[ -d "$lnbits_dir" ]]; then
for d in "$lnbits_dir"/*/; do
[[ -d "$d" ]] || continue
local name branch
name="$(basename "$d")"
branch="$(git -C "$d" branch --show-current 2>/dev/null || echo '?')"
printf " %-12s (%s)\n" "$name" "$branch"
done
fi
return 0
fi
if [[ -d "$lnbits_dir/$env" ]]; then
cd "$lnbits_dir/$env" || return 1
else
echo "Unknown lnbits worktree: $env"
return 1
fi
}
# Navigate to a webapp target branch worktree.
wa() {
_devenv_load_config
local target="${1:-}"
local webapp_dir="${WEBAPP_DIR:-$DEV_ROOT/webapp}"
if [[ -z "$target" ]]; then
echo "Usage: wa <target>"
echo ""
echo "Current webapp worktrees:"
if [[ -d "$webapp_dir" ]]; then
for d in "$webapp_dir"/*/; do
[[ -d "$d" ]] || continue
local name branch
name="$(basename "$d")"
branch="$(git -C "$d" branch --show-current 2>/dev/null || echo '?')"
printf " %-12s (%s)\n" "$name" "$branch"
done
fi
return 0
fi
if [[ -d "$webapp_dir/$target" ]]; then
cd "$webapp_dir/$target" || return 1
else
echo "Unknown webapp target: $target"
return 1
fi
}
# Navigate within a project-group folder (e.g. bitspire/, lamassu-next/).
# Sub-repos may be single clones or bare-repo worktree sets; worktree
# sets accept an optional worktree name (defaults to dev).
_dev_group_nav() {
local group_dir="$1" repo="${2:-}" worktree="${3:-dev}"
if [[ -z "$repo" ]]; then
echo "repos under $(basename "$group_dir")/:"
if [[ -d "$group_dir" ]]; then
for d in "$group_dir"/*/; do
[[ -d "$d" ]] && echo " $(basename "$d")"
done
fi
return 0
fi
# Single-clone sub-repo (has its own .git at the top level)
if [[ -d "$group_dir/$repo/.git" ]]; then
cd "$group_dir/$repo" || return 1
return 0
fi
# Worktree-based sub-repo
if [[ -d "$group_dir/$repo/$worktree" ]]; then
cd "$group_dir/$repo/$worktree" || return 1
elif [[ -d "$group_dir/$repo" ]]; then
cd "$group_dir/$repo" || return 1
else
echo "Unknown repo under $(basename "$group_dir")/: $repo"
return 1
fi
}
# bitSpire ATM stack — ~/dev/bitspire/{bitspire,atm-tui}
# Usage: bs [repo] [worktree] (defaults: bs bitspire dev)
bs() {
_devenv_load_config
_dev_group_nav "${BITSPIRE_DIR:-$DEV_ROOT/bitspire}" "${1:-bitspire}" "${2:-dev}"
}
# lamassu-next legacy ATM stack — ~/dev/lamassu-next/{lamassu-next,lightning-pub}
# Usage: lam [repo] [worktree] (defaults: lam lamassu-next dev)
lam() {
_devenv_load_config
_dev_group_nav "${LAMASSU_NEXT_DIR:-$DEV_ROOT/lamassu-next}" "${1:-lamassu-next}" "${2:-dev}"
}
# Navigate to an extension under shared/extensions.
ext() {
_devenv_load_config
local extension="${1:-}"
local ext_root="${SHARED_DIR:-$DEV_ROOT/shared}/extensions"
if [[ -z "$extension" ]]; then
echo "Available extensions:"
[[ -d "$ext_root" ]] && ls -1 "$ext_root"
return 0
fi
if [[ -d "$ext_root/$extension" ]]; then
cd "$ext_root/$extension" || return 1
else
echo "Unknown extension: $extension"
[[ -d "$ext_root" ]] && ls -1 "$ext_root"
return 1
fi
}
# Navigate to a deploy host config inside the deploy flake working copy.
deploy_nav() {
_devenv_load_config
local host="${1:-}"
local deploy_root="${DEPLOY_DIR:-$DEV_ROOT/deploy}"
if [[ -z "$host" ]]; then
echo "Usage: deploy <host>"
echo ""
echo "Deploy targets (from /etc/dev-env/config.sh):"
# DEPLOY_TARGETS is an array exported by config.sh
if [[ -n "${DEPLOY_TARGETS[*]:-}" ]]; then
printf ' %s\n' "${DEPLOY_TARGETS[@]}"
fi
return 1
fi
# Prefer a per-host config dir if present; otherwise the unified root
if [[ -d "$deploy_root/unified/hosts/$host" ]]; then
cd "$deploy_root/unified/hosts/$host" || return 1
elif [[ -d "$deploy_root/unified" ]]; then
cd "$deploy_root/unified" || return 1
elif [[ -d "$deploy_root/$host" ]]; then
cd "$deploy_root/$host" || return 1
else
echo "Deploy path not found: $deploy_root/{unified,<host>}"
return 1
fi
}
# Rename to avoid colliding with the `deploy` function from the legacy
# script if it's still sourced during migration. `dep` is free.
alias dep=deploy_nav
# Navigate to the shared repos directory.
shared() {
_devenv_load_config
cd "${SHARED_DIR:-$DEV_ROOT/shared}" || return 1
}
# Navigate to the bare-repos directory.
repos() {
_devenv_load_config
cd "${REPOS_DIR:-$DEV_ROOT/repos}" || return 1
}
# Navigate to the upstream-prs directory.
prs() {
_devenv_load_config
cd "${UPSTREAM_PRS_DIR:-$DEV_ROOT/upstream-prs}" || return 1
}