Files
mesh-catalog/modules/mesh-cli/cmd/mesh-cli/main.go
T
jschoubben 3ff4df5c6e mesh-cli: nox licence is the licences' overview; the controller's leftovers go
The operator typed `nox licence` and got the retired controller's refusal
instead of the licences. `nox licence` and `nox licence list` now call the
licence manager's licences and bindings and print one table of each, every
time local (1126/AC1); `nox licence usage [<licence>]` prints the usage
readings as a table (1126/AC2). `--json` prints the raw answer on every
rendered line, `nox nodes` included: one behaviour for one flag; for the
two calls one object {licences, bindings} (1126/AC3).

In the same change, the operator (2026-11-22 20:41): "why are there still
references to the controller?". Every retired controller line nox answered
with "this was the controller's" (its `instead` lines and the `unowned`
map) is deleted with its text: none has a seat verb of the same job; a
retired line now answers "is not a command" with exit 2, and nothing nox
prints names the controller (1126/AC4). The comments, help and README no
longer name the controller or issue 1126.

Builds: issue 1126 note 151, answers 1-5, and the note after it (acceptance
1126/AC1..AC4, the operator's 20:41 line); one-question-one-tool;
principle 4, a module is steered by its tools.
2026-11-22 20:50:22 +01:00

278 lines
9.0 KiB
Go

// Command mesh-cli is the operator's command on every node: `mesh-cli <command line>`, in interactive zsh also `nox`.
//
// Each line calls the seat verb or the module tool that owns its job, through this machine's console, the mesh MCP
// server on loopback (console.go). A line nox does not know says so and calls nothing. `enrol open` runs here and asks
// nothing.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/signal"
"runtime/debug"
"sort"
"strings"
"syscall"
)
// standardInput is mesh-cli's own standard input, and whether it is a terminal (or a device such as /dev/null); a
// variable so a test can say.
var standardInput = func() (io.Reader, bool) {
info, err := os.Stdin.Stat()
if err != nil {
return os.Stdin, true
}
return os.Stdin, info.Mode()&os.ModeCharDevice != 0
}
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
code := run(ctx, os.Args[1:], os.Stdout, os.Stderr)
stop()
os.Exit(code)
}
func run(ctx context.Context, args []string, stdout, stderr io.Writer) int {
if len(args) == 0 || args[0] == "-h" || args[0] == "--help" || args[0] == "help" {
fmt.Fprint(stderr, usage())
if len(args) == 0 {
return 2
}
return 0
}
// A verb's own process carries the verb it runs for; a verb is never a person.
if verb := os.Getenv("MESH_VERB"); verb != "" {
fmt.Fprintf(stderr, "mesh-cli: this process runs for the verb %q, and a verb is never the operator. "+
"Nothing was called\n", verb)
return 1
}
switch args[0] {
case "version":
fmt.Fprintln(stdout, version())
return 0
case "search":
if len(args) < 2 {
fmt.Fprintln(stderr, "mesh-cli: give the words: nox search <words>")
return 2
}
return show(ctx, "mesh_search", map[string]any{"query": strings.Join(args[1:], " ")}, nil, stdout, stderr)
case "describe":
if len(args) != 2 {
fmt.Fprintln(stderr, "mesh-cli: give one address: nox describe <address>")
return 2
}
return show(ctx, "mesh_describe", map[string]any{"address": args[1]}, nil, stdout, stderr)
}
// `enrol open` is local: it opens a sealed enrolment token with the operator's private key and asks nothing of the
// mesh (novox/hq ADR 0587 §8).
if len(args) >= 2 && args[0] == "enrol" && args[1] == "open" {
in, terminal := standardInput()
return enrolOpen(args[2:], in, terminal, stdout, stderr)
}
c, rest := find(args)
if c == nil {
fmt.Fprintf(stderr, "mesh-cli: %q is not a command. nox help lists them; nox search <words> finds a seat verb "+
"or a tool, and nox call <address> calls it\n", args[0])
return 2
}
k, err := c.read(rest)
if err != nil {
fmt.Fprintf(stderr, "mesh-cli: %v. Nothing was called\n nox %s %s\n", err, strings.Join(c.words, " "), c.usage)
return 2
}
if h, ok := k.Args["act"].(*handAct); ok {
id, code := recordAct(ctx, *h, stderr)
if code != 0 {
return code
}
k.Args["act"] = id
}
if k.Address == "mesh-delivery-view.view_status" {
return status(ctx, stdout, stderr)
}
render := c.render
l, _ := parseLine(rest)
if l.has("json") {
render = nil
}
if c.also != nil {
return showTwo(ctx, k, c.also, !l.has("json"), stdout, stderr)
}
return show(ctx, "mesh_call", map[string]any{"address": k.Address, "arguments": k.Args}, render, stdout, stderr)
}
// showTwo calls a line's two addresses in turn and prints both answers: through the line's render, or raw as one
// object under the line's two keys. The second is not called when the first fails, and either failing is said with
// exit 1.
func showTwo(ctx context.Context, first call, also *second, rendered bool, stdout, stderr io.Writer) int {
var texts [2]string
for i, k := range []call{first, {also.address, map[string]any{}}} {
a, err := consoleTool(ctx, "mesh_call", map[string]any{"address": k.Address, "arguments": k.Args})
if err != nil {
return failed(err, stderr)
}
if a.IsError {
fmt.Fprintln(stderr, "mesh-cli: "+k.Address+": "+strings.TrimRight(a.Text, "\n"))
return 1
}
texts[i] = a.Text
}
if rendered {
fmt.Fprintln(stdout, strings.TrimRight(also.render(texts[0], texts[1]), "\n"))
return 0
}
whole := map[string]json.RawMessage{}
var after []string
for i, text := range texts {
v, rest := splitAnswer(text)
if v == nil {
fmt.Fprintf(stderr, "mesh-cli: %s answered no JSON: %q\n", []string{first.Address, also.address}[i], text)
return 1
}
whole[also.keys[i]] = v
if rest != "" && (len(after) == 0 || after[0] != rest) {
after = append(after, rest)
}
}
b, err := json.MarshalIndent(whole, "", " ")
if err != nil {
return failed(err, stderr)
}
fmt.Fprintln(stdout, string(b))
for _, a := range after {
fmt.Fprintln(stdout, a)
}
return 0
}
// recordAct records the hand act a sensitive vault verb is warranted by, through mesh-hand-acts, and answers its id;
// a recording refused or unread calls the vault not at all (novox/hq ADR 0590 §8).
func recordAct(ctx context.Context, h handAct, stderr io.Writer) (string, int) {
a, err := meshCall(ctx, "mesh-hand-acts.hand-act", map[string]any{"verb": h.Verb, "what": h.What, "why": h.Why,
"cause": "operator"})
if err != nil {
return "", failed(err, stderr)
}
if a.IsError {
fmt.Fprintln(stderr, "mesh-cli: the hand act was not recorded, so "+h.Verb+" was not called: "+strings.TrimRight(a.Text, "\n"))
return "", 1
}
var rec struct {
Act struct {
ID string `json:"id"`
} `json:"act"`
}
if err := json.NewDecoder(strings.NewReader(a.Text)).Decode(&rec); err != nil || rec.Act.ID == "" {
fmt.Fprintf(stderr, "mesh-cli: mesh-hand-acts answered no act's id (%q), so %s was not called\n", a.Text, h.Verb)
return "", 1
}
return rec.Act.ID, 0
}
// show calls one of the console's tools and prints what it answered: an answer marked an error on standard error, with
// exit 1.
func show(ctx context.Context, tool string, args map[string]any, render func(string) string, stdout, stderr io.Writer) int {
a, err := consoleTool(ctx, tool, args)
if err != nil {
return failed(err, stderr)
}
if a.IsError {
fmt.Fprintln(stderr, "mesh-cli: "+strings.TrimRight(a.Text, "\n"))
return 1
}
text := a.Text
if render != nil {
text = render(text)
}
fmt.Fprintln(stdout, strings.TrimRight(text, "\n"))
return 0
}
// failed says why a call did not come back, and never as nothing having run unless nothing answered.
func failed(err error, stderr io.Writer) int {
switch {
case errors.Is(err, context.Canceled):
fmt.Fprintln(stderr, "mesh-cli: stopped waiting; the call may still run")
return 130
case errors.Is(err, context.DeadlineExceeded):
fmt.Fprintln(stderr, "mesh-cli: no answer in time; the call may have run. Check before repeating a change")
return 1
}
fmt.Fprintf(stderr, "mesh-cli: %v\n", err)
return 1
}
// status is the delivery view's status. When the view does not answer, that is said first and loudly, and what the
// condition keeper and mesh-delivery hold is shown in its place, each under its address, with exit 1: never shown as
// the status.
func status(ctx context.Context, stdout, stderr io.Writer) int {
a, err := meshCall(ctx, "mesh-delivery-view.view_status", nil)
if err == nil && !a.IsError {
fmt.Fprintln(stdout, strings.TrimRight(a.Text, "\n"))
return 0
}
if errors.Is(err, errNoConsole) || errors.Is(err, context.Canceled) {
return failed(err, stderr)
}
why := a.Text
if err != nil {
why = err.Error()
}
fmt.Fprintf(stderr, "mesh-cli: the status (mesh-delivery-view.view_status) did not answer: %s\n"+
"mesh-cli: what follows is not the status: the open conditions and the deliveries, read on their own\n",
strings.TrimRight(why, "\n"))
for _, address := range []string{"mesh-conditions.conditions", "mesh-delivery.deliveries"} {
fmt.Fprintf(stdout, "== %s\n", address)
show(ctx, "mesh_call", map[string]any{"address": address, "arguments": map[string]any{}}, nil, stdout, stderr)
}
return 1
}
// version is what this binary was built from.
func version() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return "mesh-cli (unknown build)"
}
v := "mesh-cli " + info.Main.Version
for _, s := range info.Settings {
if s.Key == "vcs.revision" {
v += " " + s.Value
}
}
return v
}
// usage is every line nox takes, grouped by its first word.
func usage() string {
var b strings.Builder
b.WriteString(`mesh-cli: the operator's command (nox in interactive zsh)
Each line calls the seat verb or module tool that owns the job, through this machine's console (the mesh MCP
server on loopback).
`)
lines := make([]string, 0, len(commands))
for _, c := range commands {
lines = append(lines, fmt.Sprintf(" nox %s %s\n %s", strings.Join(c.words, " "), c.usage, c.does))
}
sort.Strings(lines)
b.WriteString(strings.Join(lines, "\n"))
b.WriteString(`
nox search <words>
find a seat verb or a module tool
nox describe <address>
one address's arguments
nox enrol open --key <file>
open an enrolment token sealed to the operator key, here; asks nothing of the mesh
nox version
Every answer is the owner's own, as JSON, or a table where the line says so; --json prints it raw.
`)
return b.String()
}