Files
hermenix/nu/lib/common.nu
T
2026-09-16 22:09:42 -04:00

58 lines
2.2 KiB
Nu

# hermenix — shared helpers for the host-side nushell tools (hermenix,
# hermes-vm, hermes-ctl).
#
# Not a standalone script: nu can't reliably resolve relative `use` imports
# once a script has been copied into the Nix store as a single wrapped
# binary, so the flake concatenates this file with each tool's own script
# instead (see flake.nix / modules/host.nix). Keep this file free of a
# `main` def and of anything specific to one tool.
# ---------------------------------------------------------------- output ---
def say [msg: string] { print $" ($msg)" }
def note [msg: string] { print $" (ansi dark_gray)($msg)(ansi reset)" }
def ok [msg: string] { print $" (ansi green)✓(ansi reset) ($msg)" }
def warn [msg: string] { print $" (ansi yellow)!(ansi reset) ($msg)" }
def die [msg: string] { print -e $"\n(ansi red)error:(ansi reset) ($msg)"; exit 1 }
def step [n: int, title: string] { print $"\n(ansi attr_bold)== ($n). ($title)(ansi reset)" }
def ask [prompt: string, default: string = ""] {
let suffix = if $default != "" { $" [($default)]" } else { "" }
let reply = (input $" ($prompt)($suffix): ")
if $reply == "" { $default } else { $reply }
}
def confirm [prompt: string] {
let reply = (input $" ($prompt) [y/N] ")
(($reply | str starts-with "y") or ($reply | str starts-with "Y"))
}
def confirm-word [word: string, prompt: string] {
(input $" ($prompt) ") == $word
}
# Single-quote a string for safe embedding in a remote shell command line.
def shquote [s: string] {
"'" + ($s | str replace --all "'" "'\\''") + "'"
}
# ------------------------------------------------------------------- ssh ---
# argv for reaching the guest over vsock — no inbound network path needed.
# Shared by every tool that talks to the guest (hermenix migrate, hermes-vm,
# hermes-ctl).
def vsock-ssh-args [vsock_cid: int, guest_user: string, guest_host: string] {
[
"ssh"
"-o" $"ProxyCommand=socat - VSOCK-CONNECT:($vsock_cid):22"
"-o" "StrictHostKeyChecking=accept-new"
$"($guest_user)@($guest_host)"
]
}
# Run an argv list (e.g. from vsock-ssh-args, with trailing command words
# appended) as an external command.
def run-argv [argv: list<string>] {
^($argv | first) ...($argv | skip 1)
}