completions
This commit is contained in:
1 parent
21e87b6bfa
commit
45d2d9e489
4 files changed
+233
No files matched your search
@@ -73,6 +73,12 @@
|
||||
}
|
||||
nuScript;
|
||||
|
||||
hermenix-completions = pkgs.writeTextFile {
|
||||
name = "hermenix-completions";
|
||||
destination = "/share/nushell/vendor/autoload/hermenix.nu";
|
||||
text = builtins.readFile ./nu/completions.nu;
|
||||
};
|
||||
|
||||
default = self.packages.${system}.hermenix;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -809,6 +809,14 @@ in
|
||||
};
|
||||
|
||||
environment.systemPackages = [
|
||||
# Nushell tab-completion definitions for hermenix. Installed to
|
||||
# vendor/autoload so nushell picks them up automatically.
|
||||
(pkgs.writeTextFile {
|
||||
name = "hermenix-completions";
|
||||
destination = "/share/nushell/vendor/autoload/hermenix.nu";
|
||||
text = builtins.readFile ../nu/completions.nu;
|
||||
})
|
||||
|
||||
# Single consolidated CLI: `hermenix vm *` controls the VM,
|
||||
# `hermenix ctl *` drives Hermes inside it. Per-host values are
|
||||
# passed as env vars via makeWrapperArgs --set.
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
# Tab-completion definitions for hermenix.
|
||||
#
|
||||
# !! GENERATED — do not edit by hand.
|
||||
# !! Regenerate with: nu nu/gen-completions.nu
|
||||
#
|
||||
# Installed to $out/share/nushell/vendor/autoload/ so nushell on NixOS
|
||||
# picks them up automatically from environment.systemPackages. No manual
|
||||
# sourcing needed.
|
||||
|
||||
def "nu-complete hermenix top" [] {
|
||||
[
|
||||
[value description];
|
||||
[setup "guided host-side setup"]
|
||||
[migrate "carry an existing flat HERMES_HOME into the guest"]
|
||||
[remote "run setup/migrate on a remote host over SSH"]
|
||||
[vm "control the VM (start/stop/console/snapshots/backups)"]
|
||||
[ctl "drive Hermes inside the guest (identity/config/secrets/gateway)"]
|
||||
]
|
||||
}
|
||||
|
||||
def "nu-complete hermenix vm" [] {
|
||||
[
|
||||
[value description];
|
||||
[ssh "open a shell or run a single command in the guest"]
|
||||
[console "follow the VM's journal output (journalctl -f)"]
|
||||
[start "systemctl start hermes-vm"]
|
||||
[stop "systemctl stop hermes-vm"]
|
||||
[restart "systemctl restart hermes-vm"]
|
||||
[config "show current generation, history and git status"]
|
||||
[config-pull "copy the guest's /etc/nixos here for reading/diffing"]
|
||||
[snapshot "trigger a state-disk snapshot now"]
|
||||
[snapshots "list existing snapshots"]
|
||||
[rollback "restore the state disk from a snapshot"]
|
||||
[backup "trigger a restic backup now"]
|
||||
[backups "list restic snapshots"]
|
||||
]
|
||||
}
|
||||
|
||||
def "nu-complete hermenix ctl" [] {
|
||||
[
|
||||
[value description];
|
||||
[soul "show or edit SOUL.md"]
|
||||
[memory "show or edit memories/USER.md"]
|
||||
[conf "show or edit config.yaml"]
|
||||
[secrets "manage .env (keys masked by default)"]
|
||||
[status "gateway service status"]
|
||||
[logs "follow gateway journal (-f)"]
|
||||
[restart "restart the gateway"]
|
||||
[units "list all hermes* user units"]
|
||||
[reseed "overwrite identity from /etc/nixos/hermes"]
|
||||
[pull "copy live identity back to the host"]
|
||||
]
|
||||
}
|
||||
|
||||
# Context-aware completer: inspects already-typed words to decide
|
||||
# whether to offer top-level subcommands or a second-level list.
|
||||
def "nu-complete hermenix ctx" [context: string, offset: int] {
|
||||
let words = ($context | str trim | split row " " | where { $in != "" })
|
||||
match ($words | length) {
|
||||
1 => (nu-complete hermenix top)
|
||||
2 => {
|
||||
match ($words | get 1) {
|
||||
"vm" => (nu-complete hermenix vm)
|
||||
"ctl" => (nu-complete hermenix ctl)
|
||||
_ => []
|
||||
}
|
||||
}
|
||||
_ => []
|
||||
}
|
||||
}
|
||||
|
||||
extern "hermenix" [...args: string@"nu-complete hermenix ctx"]
|
||||
@@ -0,0 +1,147 @@
|
||||
#!/usr/bin/env nu
|
||||
#
|
||||
# gen-completions.nu — regenerate nu/completions.nu from the usage print
|
||||
# blocks in the source files.
|
||||
#
|
||||
# Run from the repo root:
|
||||
# nu nu/gen-completions.nu
|
||||
#
|
||||
# The single source of truth for each subcommand's name and description is
|
||||
# its line in the relevant usage block (main [], main vm [], main ctl []).
|
||||
# This script extracts those lines and rewrites completions.nu in place so
|
||||
# they never drift apart.
|
||||
|
||||
# Parse the usage print lines from a source file.
|
||||
#
|
||||
# Looks for lines of the form:
|
||||
# print " <name>[optional args] <description>"
|
||||
# where name and description are separated by 2+ spaces.
|
||||
#
|
||||
# strip_prefix: if non-empty, strip " <prefix> " from the front of each
|
||||
# line's content before splitting (used for hermenix.nu, where lines read
|
||||
# " hermenix setup desc" and we want just "setup").
|
||||
#
|
||||
# Returns a list of {value, description} records, skipping any line whose
|
||||
# extracted name contains a quote or starts with "hermenix" (those are
|
||||
# passthrough-example lines, not real subcommands).
|
||||
def parse-usage [file: string, strip_prefix: string = ""] {
|
||||
open --raw $file
|
||||
| lines
|
||||
| where { $in =~ 'print " \S' }
|
||||
| each {|line|
|
||||
# Strip everything up to and including the opening `print "` and the
|
||||
# closing `"` (last one), leaving just the inner usage string.
|
||||
let inner = ($line
|
||||
| str replace -r '^[^"]*"' ""
|
||||
| str replace -r '"[^"]*$' "")
|
||||
|
||||
# Optionally remove a leading " <prefix> " so that
|
||||
# " hermenix setup desc" becomes " setup desc".
|
||||
let body = if ($strip_prefix != "") {
|
||||
$inner | str replace $" ($strip_prefix) " " "
|
||||
} else {
|
||||
$inner
|
||||
}
|
||||
|
||||
# Split on 2+ spaces — left side is "name [optional args]", right is desc.
|
||||
let parts = ($body | str trim | split row -r " +" | where { $in != "" })
|
||||
|
||||
if ($parts | length) < 2 {
|
||||
null
|
||||
} else {
|
||||
let name = ($parts | first | split row " " | first)
|
||||
# Re-join all remaining parts — descriptions like "systemctl stop hermes-vm"
|
||||
# would otherwise get cut at the double-space inside the description.
|
||||
let desc = ($parts | skip 1 | str join " " | str trim)
|
||||
{value: $name, description: $desc}
|
||||
}
|
||||
}
|
||||
| where { $in != null }
|
||||
| where { $in.value != "" }
|
||||
| where { not ($in.value | str contains '"') }
|
||||
| where { not ($in.value | str starts-with "hermenix") }
|
||||
| uniq-by value
|
||||
}
|
||||
|
||||
# Render one `nu-complete` function that returns a [value description] table.
|
||||
def render-complete [fn_name: string, entries: list] {
|
||||
let rows = ($entries
|
||||
| each {|e| $" [($e.value) \"($e.description)\"]" }
|
||||
| str join "\n")
|
||||
[
|
||||
$"def \"nu-complete ($fn_name)\" [] {"
|
||||
" ["
|
||||
" [value description];"
|
||||
$rows
|
||||
" ]"
|
||||
"}"
|
||||
] | str join "\n"
|
||||
}
|
||||
|
||||
# Render the context-aware dispatcher that picks between the three tables.
|
||||
# Written as a single-quoted literal — no interpolation needed.
|
||||
def render-ctx [] {
|
||||
'# Context-aware completer: inspects already-typed words to decide
|
||||
# whether to offer top-level subcommands or a second-level list.
|
||||
def "nu-complete hermenix ctx" [context: string, offset: int] {
|
||||
let words = ($context | str trim | split row " " | where { $in != "" })
|
||||
match ($words | length) {
|
||||
1 => (nu-complete hermenix top)
|
||||
2 => {
|
||||
match ($words | get 1) {
|
||||
"vm" => (nu-complete hermenix vm)
|
||||
"ctl" => (nu-complete hermenix ctl)
|
||||
_ => []
|
||||
}
|
||||
}
|
||||
_ => []
|
||||
}
|
||||
}'
|
||||
}
|
||||
|
||||
def render-extern [] {
|
||||
'extern "hermenix" [...args: string@"nu-complete hermenix ctx"]'
|
||||
}
|
||||
|
||||
def render-header [] {
|
||||
[
|
||||
"# Tab-completion definitions for hermenix."
|
||||
"#"
|
||||
"# !! GENERATED — do not edit by hand."
|
||||
"# !! Regenerate with: nu nu/gen-completions.nu"
|
||||
"#"
|
||||
"# Installed to $out/share/nushell/vendor/autoload/ so nushell on NixOS"
|
||||
"# picks them up automatically from environment.systemPackages. No manual"
|
||||
"# sourcing needed."
|
||||
] | str join "\n"
|
||||
}
|
||||
|
||||
# ── main ────────────────────────────────────────────────────────────────
|
||||
|
||||
let repo_root = ($env.FILE_PWD | path dirname)
|
||||
let out_file = ($repo_root | path join "nu" "completions.nu")
|
||||
|
||||
let top = (parse-usage ($repo_root | path join "nu" "hermenix.nu") "hermenix")
|
||||
let vm = (parse-usage ($repo_root | path join "nu" "vm" "vm.nu"))
|
||||
let ctl = (parse-usage ($repo_root | path join "nu" "ctl" "ctl.nu"))
|
||||
|
||||
if ($top | is-empty) { print -e "warn: no top-level completions found in hermenix.nu" }
|
||||
if ($vm | is-empty) { print -e "warn: no vm completions found in vm/vm.nu" }
|
||||
if ($ctl | is-empty) { print -e "warn: no ctl completions found in ctl/ctl.nu" }
|
||||
|
||||
[
|
||||
(render-header)
|
||||
""
|
||||
(render-complete "hermenix top" $top)
|
||||
""
|
||||
(render-complete "hermenix vm" $vm)
|
||||
""
|
||||
(render-complete "hermenix ctl" $ctl)
|
||||
""
|
||||
(render-ctx)
|
||||
""
|
||||
(render-extern)
|
||||
""
|
||||
] | str join "\n" | save -f $out_file
|
||||
|
||||
print $"wrote ($out_file) [top=($top | length) vm=($vm | length) ctl=($ctl | length)]"
|
||||
Reference in new issue
Block a user