perf(server): split server-list to dedicated thin handler + ADR-025 pre-commit guard

This commit is contained in:
Jesús Pérez 2026-04-17 21:58:40 +01:00
parent 271f41aa53
commit 889feeb37c
Signed by: jesus
GPG key ID: 9F243E355E0BC939
3 changed files with 115 additions and 1 deletions

View file

@ -3,3 +3,35 @@
use toolkit.nu fmt
fmt # --check --verbose
# ADR-025: Block root star-imports of lib_provisioning / main_provisioning.
# A line matching `use lib_provisioning *` or `use main_provisioning *` at the
# start of a file (top-level) reintroduces the transitive parse cost this
# refactor was designed to eliminate. All imports must be selective.
let staged = (git diff --cached --name-only | lines | where { str ends-with ".nu" })
if ($staged | length) > 0 {
let violations = (
$staged
| each {|f|
let hits = (
do { git show $":($f)" } | complete
| if $in.exit_code == 0 { $in.stdout } else { "" }
| lines
| enumerate
| where { $it.item | str starts-with "use lib_provisioning *" or $it.item | str starts-with "use main_provisioning *" }
| each {|row| $" ($f):($row.index + 1): ($row.item | str trim)"}
)
$hits
}
| flatten
)
if ($violations | length) > 0 {
print "❌ ADR-025 star-import violation — selective imports required:"
for v in $violations { print $v }
print ""
print "Replace `use lib_provisioning *` with explicit `use lib_provisioning/path/to/module.nu [sym1 sym2]`"
exit 1
}
}

View file

@ -1115,8 +1115,15 @@ else
esac
;;
server | s)
# Intercept subcommand --help before Nu absorbs it at the top-level main
# Route list/sync to the lightweight handler (loads only list.nu, ~255ms).
# All other subcommands go to the full handler (~1.15s).
_srv_sub="${2:-}"
case "$_srv_sub" in
list|ls|l|sync)
$NU "${NU_ARGS[@]}" "$PROVISIONING/core/nulib/provisioning-server-list.nu" $CMD_ARGS </dev/null
exit $? ;;
esac
# Intercept subcommand --help before Nu absorbs it at the top-level main
_has_help=false
for _a in "$@"; do [ "$_a" = "--help" ] || [ "$_a" = "-h" ] && _has_help=true && break; done
if [ "$_has_help" = "true" ]; then

View file

@ -0,0 +1,75 @@
#!/usr/bin/env nu
# Thin entry for `server list` and `server sync`.
# Loads only servers/list.nu (~255ms vs ~1.15s for the full server handler).
# Bash wrapper routes `server list/ls/l/sync` here; all other server subcommands
# go to provisioning-server.nu.
export-env {
let lib_dirs_raw = ($env.NU_LIB_DIRS? | default "")
let current_lib_dirs = if ($lib_dirs_raw | type) == "string" {
if ($lib_dirs_raw | is-empty) { [] } else { ($lib_dirs_raw | split row ":") }
} else {
$lib_dirs_raw
}
let dynamic = ($env.PROVISIONING? | default "" | path join "core" "nulib")
$env.NU_LIB_DIRS = ([
"/opt/provisioning/core/nulib"
"/usr/local/provisioning/core/nulib"
] | append $current_lib_dirs | append (if ($dynamic | is-not-empty) { [$dynamic] } else { [] }))
let args_raw = ($env.PROVISIONING_ARGS? | default "")
$env.PROVISIONING_ARGS = ($args_raw | str replace --regex '^(server|s)\s+' '')
let _coerce = {|raw| $raw == "true" or $raw == "1" }
let raw_no_titles = ($env.PROVISIONING_NO_TITLES? | default "")
if ($raw_no_titles | describe) == "string" and ($raw_no_titles | is-not-empty) {
$env.PROVISIONING_NO_TITLES = (do $_coerce $raw_no_titles)
}
let raw_no_terminal = ($env.PROVISIONING_NO_TERMINAL? | default "")
if ($raw_no_terminal | describe) == "string" and ($raw_no_terminal | is-not-empty) {
$env.PROVISIONING_NO_TERMINAL = (do $_coerce $raw_no_terminal)
}
let raw_titles_shown = ($env.PROVISIONING_TITLES_SHOWN? | default "")
if ($raw_titles_shown | describe) == "string" and ($raw_titles_shown | is-not-empty) {
$env.PROVISIONING_TITLES_SHOWN = (do $_coerce $raw_titles_shown)
}
let raw_debug = ($env.PROVISIONING_DEBUG? | default "")
if ($raw_debug | describe) == "string" and ($raw_debug | is-not-empty) {
$env.PROVISIONING_DEBUG = (do $_coerce $raw_debug)
}
}
use servers/list.nu *
def main [
...args: string
--infra (-i): string = ""
--debug (-x)
--out: string = ""
]: nothing -> nothing {
if $debug { $env.PROVISIONING_DEBUG = true }
let first = ($args | get 0? | default "")
let rest = if $first in ["server" "s"] { $args | skip 1 } else { $args }
let subcmd = ($rest | get 0? | default "list")
match $subcmd {
"list" | "ls" | "lis" | "l" | "" => {
if ($infra | is-not-empty) {
main list --infra $infra --debug=$debug --out=$out
} else {
main list --debug=$debug --out=$out
}
}
"sync" => {
if ($infra | is-not-empty) {
main sync --infra $infra
} else {
main sync
}
}
_ => {
error make { msg: $"server-list handler received unexpected subcommand '($subcmd)'" }
}
}
}