# Shared helpers. Concatenated into the hermenix binary by flake.nix # (relative `use` is unreliable once the script is in the store). 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)" } # `input` hangs when stdin is piped (tests); read the pipe instead. def read-line [prompt: string] { if (is-terminal --stdin) { input $prompt } else { print -n $prompt ( ^cat | lines | first | str trim ) } } def ask [prompt: string, default: string = ""] { let suffix = if $default != "" { $" [($default)]" } else { "" } let reply = (read-line $" ($prompt)($suffix): ") if $reply == "" { $default } else { $reply } } def confirm [prompt: string] { let reply = (read-line $" ($prompt) [y/N] ") (($reply | str starts-with "y") or ($reply | str starts-with "Y")) } def confirm-word [word: string, prompt: string] { (read-line $" ($prompt) ") == $word } def shquote [s: string] { "'" + ($s | str replace --all "'" "'\\''") + "'" } 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)" ] } def run-argv [argv: list] { ^($argv | first) ...($argv | skip 1) } # Baked in at package build time; "dev" only for an unwrapped checkout. def hermenix-version [] { $env.HERMENIX_VERSION? | default "dev" } def "main version" [] { print (hermenix-version) }