prvng_core/nulib/lib_minimal.nu

182 lines
6.2 KiB
Text
Raw Normal View History

#!/usr/bin/env nu
# Minimal Library - Fast path for interactive commands
# NO config loading, NO platform bootstrap
# Follows: @.claude/guidelines/nushell/NUSHELL_GUIDELINES.md
# Error handling: Result pattern (hybrid, no try-catch)
use lib_provisioning/result.nu *
# Get user config path (centralized location)
# Rule 2: Single purpose function
# Cross-platform support (macOS, Linux, Windows)
def get-user-config-path [] {
let home = $env.HOME
let os_name = (uname | get operating-system | str downcase)
let config_path = match $os_name {
"darwin" => $"($home)/Library/Application Support/provisioning/user_config.yaml",
_ => $"($home)/.config/provisioning/user_config.yaml"
}
$config_path | path expand
}
# List all registered workspaces
# Rule 1: Explicit types, Rule 4: Early returns
# Rule 2: Single purpose - only list workspaces
# Result: {ok: list, err: null} on success; {ok: null, err: message} on error
export def workspace-list [] {
let user_config = (get-user-config-path)
# Guard: Early return if config doesn't exist
if not ($user_config | path exists) {
return (ok [])
}
# Guard: File is guaranteed to exist, open directly (no try-catch)
let config = (open $user_config)
let active = ($config | get --optional active_workspace | default "")
let workspaces = ($config | get --optional workspaces | default [])
# Guard: No workspaces registered
if ($workspaces | length) == 0 {
return (ok [])
}
# Pure transformation
let result = ($workspaces | each {|ws|
{
name: $ws.name
path: $ws.path
active: ($ws.name == $active)
last_used: ($ws | get --optional last_used | default "Never")
}
})
ok $result
}
# Get active workspace name
# Rule 1: Explicit types, Rule 4: Early returns
# Result: {ok: string, err: null} on success; {ok: null, err: message} on error
export def workspace-active [] {
let user_config = (get-user-config-path)
# Guard: Config doesn't exist
if not ($user_config | path exists) {
return (ok "")
}
# Guard: File exists, read directly
let active_name = (open $user_config | get --optional active_workspace | default "")
ok $active_name
}
# Get workspace info by name
# Rule 1: Explicit types, Rule 4: Early returns
# Result: {ok: record, err: null} on success; {ok: null, err: message} on error
export def workspace-info [name: string] {
# Guard: Input validation
if ($name | is-empty) {
return (err "workspace name is required")
}
let user_config = (get-user-config-path)
# Guard: Config doesn't exist
if not ($user_config | path exists) {
return (ok {name: $name, path: "", exists: false})
}
# Guard: File exists, read directly
let config = (open $user_config)
let workspaces = ($config | get --optional workspaces | default [])
let ws = ($workspaces | where { $in.name == $name } | first)
# Guard: Workspace not found
if ($ws | is-empty) {
feat(core): three-layer DAG, unified component arch, commands-registry cache, Nushell 0.112.2 migration - DAG architecture: `dag show/validate/export` (nulib/main_provisioning/dag.nu), config loader (lib_provisioning/config/loader/dag.nu), taskserv dag-executor. Backed by schemas/lib/dag/*.ncl; orchestrator emits NATS events via WorkspaceComposition::into_workflow. See ADR-020, ADR-021. - Unified Component Architecture: components/mod.nu, main_provisioning/ {components,workflow,extensions,ontoref-queries}.nu. Full workflow engine with topological sort and NATS subject emission. Blocks A-H complete (libre-daoshi). - Commands-registry: nulib/commands-registry.ncl (Nickel source, 314 lines) + JSON cache at ~/.cache/provisioning/commands-registry.json rebuilt on source change. cli/provisioning fast-path alias expansion avoids cold Nu startup. ADDING_COMMANDS.md documents new-command workflow. - Platform service manager: service-manager.nu (+573), startup.nu (+611), service-check.nu (+255); autostart/bootstrap/health/target refactored. - Nushell 0.112.2 migration: removed all try/catch and bash redirections; external commands prefixed with ^; type signatures enforced. Driven by scripts/refactor-try-catch{,-simplified}.nu. - TTY stack: removed shlib/*-tty.sh; replaced by cli/tty-dispatch.sh, tty-filter.sh, tty-commands.conf. - New domain modules: images/ (golden image lifecycle), workspace/{state,sync}.nu, main_provisioning/{bootstrap,cluster-deploy,fip,state}.nu, commands/{state, build,integrations/auth,utilities/alias}.nu, platform.nu expanded (+874). - Config loader overhaul: loader/core.nu slimmed (-759), cache/core.nu refactored (-454), removed legacy loaders/file_loader.nu (-330). - Thirteen new provisioning-<domain>.nu top-level modules for bash dispatcher. - Tests: test_workspace_state.nu (+351); updates to test_oci_registry, test_services. - README + CHANGELOG updated.
2026-04-17 04:27:33 +01:00
return (ok {name: $name, path: "", exists: false, default_infra: "", infrastructures: []})
}
feat(core): three-layer DAG, unified component arch, commands-registry cache, Nushell 0.112.2 migration - DAG architecture: `dag show/validate/export` (nulib/main_provisioning/dag.nu), config loader (lib_provisioning/config/loader/dag.nu), taskserv dag-executor. Backed by schemas/lib/dag/*.ncl; orchestrator emits NATS events via WorkspaceComposition::into_workflow. See ADR-020, ADR-021. - Unified Component Architecture: components/mod.nu, main_provisioning/ {components,workflow,extensions,ontoref-queries}.nu. Full workflow engine with topological sort and NATS subject emission. Blocks A-H complete (libre-daoshi). - Commands-registry: nulib/commands-registry.ncl (Nickel source, 314 lines) + JSON cache at ~/.cache/provisioning/commands-registry.json rebuilt on source change. cli/provisioning fast-path alias expansion avoids cold Nu startup. ADDING_COMMANDS.md documents new-command workflow. - Platform service manager: service-manager.nu (+573), startup.nu (+611), service-check.nu (+255); autostart/bootstrap/health/target refactored. - Nushell 0.112.2 migration: removed all try/catch and bash redirections; external commands prefixed with ^; type signatures enforced. Driven by scripts/refactor-try-catch{,-simplified}.nu. - TTY stack: removed shlib/*-tty.sh; replaced by cli/tty-dispatch.sh, tty-filter.sh, tty-commands.conf. - New domain modules: images/ (golden image lifecycle), workspace/{state,sync}.nu, main_provisioning/{bootstrap,cluster-deploy,fip,state}.nu, commands/{state, build,integrations/auth,utilities/alias}.nu, platform.nu expanded (+874). - Config loader overhaul: loader/core.nu slimmed (-759), cache/core.nu refactored (-454), removed legacy loaders/file_loader.nu (-330). - Thirteen new provisioning-<domain>.nu top-level modules for bash dispatcher. - Tests: test_workspace_state.nu (+351); updates to test_oci_registry, test_services. - README + CHANGELOG updated.
2026-04-17 04:27:33 +01:00
# Collect infra dirs with server counts
let infra_root = ($ws.path | path join 'infra')
let infrastructures = if ($infra_root | path exists) {
ls $infra_root
| where type == 'dir'
| each {|inf|
let inf_name = ($inf.name | path basename)
let sf_direct = ($infra_root | path join $inf_name | path join 'servers.ncl')
let sf_defs = ($infra_root | path join $inf_name | path join 'defs' | path join 'servers.ncl')
let sf = if ($sf_direct | path exists) { $sf_direct } else { $sf_defs }
let server_count = if ($sf | path exists) {
open $sf --raw | split row "\n" | where {|l| $l =~ 'hostname\s*=\s*"' } | length
} else { 0 }
{ name: $inf_name, servers: $server_count }
}
} else { [] }
ok {
name: $ws.name
path: $ws.path
exists: true
last_used: ($ws | get --optional last_used | default "Never")
feat(core): three-layer DAG, unified component arch, commands-registry cache, Nushell 0.112.2 migration - DAG architecture: `dag show/validate/export` (nulib/main_provisioning/dag.nu), config loader (lib_provisioning/config/loader/dag.nu), taskserv dag-executor. Backed by schemas/lib/dag/*.ncl; orchestrator emits NATS events via WorkspaceComposition::into_workflow. See ADR-020, ADR-021. - Unified Component Architecture: components/mod.nu, main_provisioning/ {components,workflow,extensions,ontoref-queries}.nu. Full workflow engine with topological sort and NATS subject emission. Blocks A-H complete (libre-daoshi). - Commands-registry: nulib/commands-registry.ncl (Nickel source, 314 lines) + JSON cache at ~/.cache/provisioning/commands-registry.json rebuilt on source change. cli/provisioning fast-path alias expansion avoids cold Nu startup. ADDING_COMMANDS.md documents new-command workflow. - Platform service manager: service-manager.nu (+573), startup.nu (+611), service-check.nu (+255); autostart/bootstrap/health/target refactored. - Nushell 0.112.2 migration: removed all try/catch and bash redirections; external commands prefixed with ^; type signatures enforced. Driven by scripts/refactor-try-catch{,-simplified}.nu. - TTY stack: removed shlib/*-tty.sh; replaced by cli/tty-dispatch.sh, tty-filter.sh, tty-commands.conf. - New domain modules: images/ (golden image lifecycle), workspace/{state,sync}.nu, main_provisioning/{bootstrap,cluster-deploy,fip,state}.nu, commands/{state, build,integrations/auth,utilities/alias}.nu, platform.nu expanded (+874). - Config loader overhaul: loader/core.nu slimmed (-759), cache/core.nu refactored (-454), removed legacy loaders/file_loader.nu (-330). - Thirteen new provisioning-<domain>.nu top-level modules for bash dispatcher. - Tests: test_workspace_state.nu (+351); updates to test_oci_registry, test_services. - README + CHANGELOG updated.
2026-04-17 04:27:33 +01:00
default_infra: ($ws | get --optional default_infra | default "")
infrastructures: $infrastructures
}
}
# Quick status check (orchestrator health + active workspace)
# Rule 1: Explicit types, Rule 4: Early returns
# Result: {ok: record, err: null} on success; {ok: null, err: message} on error
export def status-quick [] {
feat(core): three-layer DAG, unified component arch, commands-registry cache, Nushell 0.112.2 migration - DAG architecture: `dag show/validate/export` (nulib/main_provisioning/dag.nu), config loader (lib_provisioning/config/loader/dag.nu), taskserv dag-executor. Backed by schemas/lib/dag/*.ncl; orchestrator emits NATS events via WorkspaceComposition::into_workflow. See ADR-020, ADR-021. - Unified Component Architecture: components/mod.nu, main_provisioning/ {components,workflow,extensions,ontoref-queries}.nu. Full workflow engine with topological sort and NATS subject emission. Blocks A-H complete (libre-daoshi). - Commands-registry: nulib/commands-registry.ncl (Nickel source, 314 lines) + JSON cache at ~/.cache/provisioning/commands-registry.json rebuilt on source change. cli/provisioning fast-path alias expansion avoids cold Nu startup. ADDING_COMMANDS.md documents new-command workflow. - Platform service manager: service-manager.nu (+573), startup.nu (+611), service-check.nu (+255); autostart/bootstrap/health/target refactored. - Nushell 0.112.2 migration: removed all try/catch and bash redirections; external commands prefixed with ^; type signatures enforced. Driven by scripts/refactor-try-catch{,-simplified}.nu. - TTY stack: removed shlib/*-tty.sh; replaced by cli/tty-dispatch.sh, tty-filter.sh, tty-commands.conf. - New domain modules: images/ (golden image lifecycle), workspace/{state,sync}.nu, main_provisioning/{bootstrap,cluster-deploy,fip,state}.nu, commands/{state, build,integrations/auth,utilities/alias}.nu, platform.nu expanded (+874). - Config loader overhaul: loader/core.nu slimmed (-759), cache/core.nu refactored (-454), removed legacy loaders/file_loader.nu (-330). - Thirteen new provisioning-<domain>.nu top-level modules for bash dispatcher. - Tests: test_workspace_state.nu (+351); updates to test_oci_registry, test_services. - README + CHANGELOG updated.
2026-04-17 04:27:33 +01:00
# Guard: HTTP check with do/complete pattern (no try-catch)
let health_result = (do { http get --max-time 2sec "http://localhost:9090/health" } | complete)
let orch_health = if ($health_result.exit_code == 0) { $health_result.stdout } else { null }
let orch_status = if ($orch_health != null) { "running" } else { "stopped" }
# Guard: Get active workspace safely
let ws_result = (workspace-active)
let active_ws = (if (is-ok $ws_result) { $ws_result.ok } else { "" })
# Pure transformation
ok {
orchestrator: $orch_status
workspace: $active_ws
timestamp: (date now | format date "%Y-%m-%d %H:%M:%S")
}
}
# Display essential environment variables
# Rule 1: Explicit types, Rule 8: Pure function (read-only)
# Result: {ok: record, err: null} on success; {ok: null, err: message} on error
export def env-quick [] {
# Pure transformation with optional operator
let vars = {
PROVISIONING_ROOT: ($env.PROVISIONING_ROOT? | default "not set")
PROVISIONING_ENV: ($env.PROVISIONING_ENV? | default "not set")
PROVISIONING_DEBUG: ($env.PROVISIONING_DEBUG? | default "false")
HOME: $env.HOME
PWD: $env.PWD
}
ok $vars
}
# Show quick help for fast-path commands
# Rule 1: Explicit types, Rule 8: Pure function
export def quick-help [] {
"Provisioning CLI - Fast Path Commands
Quick Commands (< 100ms):
workspace list List all registered workspaces
workspace active Show currently active workspace
status Quick health check
env Show essential environment variables
help [command] Show help for a command
For full help:
provisioning help Show all available commands
provisioning help <command> Show help for specific command"
}