diff --git a/modules/networkmanager/cmd/uplink-tools/main.go b/modules/networkmanager/cmd/uplink-tools/main.go index 116251870..a122c0094 100644 --- a/modules/networkmanager/cmd/uplink-tools/main.go +++ b/modules/networkmanager/cmd/uplink-tools/main.go @@ -1,5 +1,6 @@ // The uplink holder's tools bundle: the node-uplink seat's verbs (novox/hq ADR 0241), served by the node's -// runtime as the operator account. uplink.go is every holder's; manager.go is this holder's own. stdout is +// runtime as the operator account. uplink.go is every holder's; manager.go is this holder's own, and gives the +// holder's own subcommands, tools and the work its bundle starts beside them (design 08 sign-in-page §11). stdout is // the MCP channel; this says nothing else. package main @@ -11,8 +12,16 @@ import ( stdio "git.novox.be/novox/mesh-sdk/go" ) -func tools(m Machine) []stdio.Tool { - return []stdio.Tool{ +// holderOwn is what a holder adds to the seat's verbs, from its manager.go: its subcommands (a process of the module +// runs one, as `uplink-tools `), its own tools and the work its bundle starts once it serves. +type holderOwn struct { + Commands map[string]func(args []string) error + Tools []stdio.Tool + Start func() +} + +func tools(m Machine, own holderOwn) []stdio.Tool { + return append([]stdio.Tool{ {Name: seat + ".resolvers", Description: "The machine's resolver file as it is now: the resolvers, search domains and options it lists, " + "whether it is the file the mesh declares, and when it is not, who wrote it as far as the machine shows " + @@ -23,12 +32,40 @@ func tools(m Machine) []stdio.Tool { "leaves through it, and the resolvers and search domains the network manager knows for it — a VPN's " + "tunnel included. (r)", Run: func(map[string]any) (any, error) { return m.Links(context.Background()) }}, + }, own.Tools...) +} + +// command is the holder's subcommand args name, or an error naming it when the holder has none such; nil with no args, +// which is the bundle. +func command(own holderOwn, args []string) (func(args []string) error, error) { + if len(args) == 0 { + return nil, nil } + if run, ok := own.Commands[args[0]]; ok { + return run, nil + } + return nil, fmt.Errorf("uplink-tools has no subcommand %q", args[0]) } func main() { m := Machine{ResolvPath: ResolvConf, Run: execRunner, Running: running, LinkDNS: managerDNS} - if err := stdio.Serve("", tools(m)); err != nil { + own := holder(m) + run, err := command(own, os.Args[1:]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + if run != nil { + if err := run(os.Args[2:]); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + return + } + if own.Start != nil { + go own.Start() + } + if err := stdio.Serve("", tools(m, own)); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } diff --git a/modules/networkmanager/cmd/uplink-tools/manager.go b/modules/networkmanager/cmd/uplink-tools/manager.go index ad35dfc25..2c1d23787 100644 --- a/modules/networkmanager/cmd/uplink-tools/manager.go +++ b/modules/networkmanager/cmd/uplink-tools/manager.go @@ -50,3 +50,6 @@ func parseNmcli(out string) map[string]LinkDNS { flush() return links } + +// holder is what networkmanager adds to the seat's verbs (design 08 sign-in-page §11). +func holder(m Machine) holderOwn { return holderOwn{} } diff --git a/modules/networkmanager/cmd/uplink-tools/uplink_test.go b/modules/networkmanager/cmd/uplink-tools/uplink_test.go index 1f0449800..f6272f3e3 100644 --- a/modules/networkmanager/cmd/uplink-tools/uplink_test.go +++ b/modules/networkmanager/cmd/uplink-tools/uplink_test.go @@ -7,6 +7,8 @@ import ( "path/filepath" "strings" "testing" + + stdio "git.novox.be/novox/mesh-sdk/go" ) // The node-uplink seat's verbs (novox/hq ADR 0241), every holder's: the resolver file as it is, the mesh's @@ -148,3 +150,30 @@ func TestTheLinksCarryTheirDefaultRouteAndTheirNames(t *testing.T) { t.Fatalf("a manager that does not answer is not said: %+v", answer) } } + +// main.go serves the seat's two verbs first and the holder's own tools after them, and runs a subcommand only when +// the holder's manager.go gives it (design 08 sign-in-page §11). +func TestTheHoldersOwnPartComesBesideTheSeatsVerbs(t *testing.T) { + ran := "" + own := holderOwn{ + Commands: map[string]func([]string) error{"watch": func(args []string) error { ran = strings.Join(args, " "); return nil }}, + Tools: []stdio.Tool{{Name: "own_tool"}}, + } + var names []string + for _, tool := range tools(Machine{}, own) { + names = append(names, tool.Name) + } + if strings.Join(names, " ") != "node-uplink.resolvers node-uplink.links own_tool" { + t.Fatalf("served %v", names) + } + if run, err := command(own, nil); run != nil || err != nil { + t.Fatalf("no arguments is the bundle, got a subcommand or %v", err) + } + if _, err := command(own, []string{"nothing"}); err == nil || !strings.Contains(err.Error(), `"nothing"`) { + t.Fatalf("a subcommand the holder has not was taken: %v", err) + } + run, err := command(own, []string{"watch", "a"}) + if err != nil || run([]string{"a"}) != nil || ran != "a" { + t.Fatalf("the holder's subcommand did not run: %v %q", err, ran) + } +} diff --git a/modules/systemd-networkd/cmd/uplink-tools/main.go b/modules/systemd-networkd/cmd/uplink-tools/main.go index 116251870..a122c0094 100644 --- a/modules/systemd-networkd/cmd/uplink-tools/main.go +++ b/modules/systemd-networkd/cmd/uplink-tools/main.go @@ -1,5 +1,6 @@ // The uplink holder's tools bundle: the node-uplink seat's verbs (novox/hq ADR 0241), served by the node's -// runtime as the operator account. uplink.go is every holder's; manager.go is this holder's own. stdout is +// runtime as the operator account. uplink.go is every holder's; manager.go is this holder's own, and gives the +// holder's own subcommands, tools and the work its bundle starts beside them (design 08 sign-in-page §11). stdout is // the MCP channel; this says nothing else. package main @@ -11,8 +12,16 @@ import ( stdio "git.novox.be/novox/mesh-sdk/go" ) -func tools(m Machine) []stdio.Tool { - return []stdio.Tool{ +// holderOwn is what a holder adds to the seat's verbs, from its manager.go: its subcommands (a process of the module +// runs one, as `uplink-tools `), its own tools and the work its bundle starts once it serves. +type holderOwn struct { + Commands map[string]func(args []string) error + Tools []stdio.Tool + Start func() +} + +func tools(m Machine, own holderOwn) []stdio.Tool { + return append([]stdio.Tool{ {Name: seat + ".resolvers", Description: "The machine's resolver file as it is now: the resolvers, search domains and options it lists, " + "whether it is the file the mesh declares, and when it is not, who wrote it as far as the machine shows " + @@ -23,12 +32,40 @@ func tools(m Machine) []stdio.Tool { "leaves through it, and the resolvers and search domains the network manager knows for it — a VPN's " + "tunnel included. (r)", Run: func(map[string]any) (any, error) { return m.Links(context.Background()) }}, + }, own.Tools...) +} + +// command is the holder's subcommand args name, or an error naming it when the holder has none such; nil with no args, +// which is the bundle. +func command(own holderOwn, args []string) (func(args []string) error, error) { + if len(args) == 0 { + return nil, nil } + if run, ok := own.Commands[args[0]]; ok { + return run, nil + } + return nil, fmt.Errorf("uplink-tools has no subcommand %q", args[0]) } func main() { m := Machine{ResolvPath: ResolvConf, Run: execRunner, Running: running, LinkDNS: managerDNS} - if err := stdio.Serve("", tools(m)); err != nil { + own := holder(m) + run, err := command(own, os.Args[1:]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + if run != nil { + if err := run(os.Args[2:]); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + return + } + if own.Start != nil { + go own.Start() + } + if err := stdio.Serve("", tools(m, own)); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } diff --git a/modules/systemd-networkd/cmd/uplink-tools/manager.go b/modules/systemd-networkd/cmd/uplink-tools/manager.go index 2710ecbe8..156c9aaa5 100644 --- a/modules/systemd-networkd/cmd/uplink-tools/manager.go +++ b/modules/systemd-networkd/cmd/uplink-tools/manager.go @@ -65,3 +65,7 @@ func parseNetworkctl(out string) map[string]LinkDNS { flush() return links } + +// holder is what systemd-networkd adds to the seat's verbs: nothing, for it cannot tell a sign-in page (design 08 +// sign-in-page §11, §28). +func holder(Machine) holderOwn { return holderOwn{} } diff --git a/modules/systemd-networkd/cmd/uplink-tools/manager_test.go b/modules/systemd-networkd/cmd/uplink-tools/manager_test.go index c41fa0a6e..4788744cf 100644 --- a/modules/systemd-networkd/cmd/uplink-tools/manager_test.go +++ b/modules/systemd-networkd/cmd/uplink-tools/manager_test.go @@ -27,3 +27,16 @@ func TestNetworkctlIsReadPerLink(t *testing.T) { t.Fatalf("read %+v", links) } } + +// systemd-networkd serves the seat's two verbs and nothing of its own: no subcommand, no tool, no work at its start +// (design 08 sign-in-page §11, §28). +func TestNetworkdServesTheSeatsVerbsAlone(t *testing.T) { + own := holder(Machine{}) + if len(own.Commands) != 0 || len(own.Tools) != 0 || own.Start != nil { + t.Fatalf("systemd-networkd adds %+v", own) + } + served := tools(Machine{}, own) + if len(served) != 2 || served[0].Name != "node-uplink.resolvers" || served[1].Name != "node-uplink.links" { + t.Fatalf("served %v", served) + } +} diff --git a/modules/systemd-networkd/cmd/uplink-tools/uplink_test.go b/modules/systemd-networkd/cmd/uplink-tools/uplink_test.go index 1f0449800..f6272f3e3 100644 --- a/modules/systemd-networkd/cmd/uplink-tools/uplink_test.go +++ b/modules/systemd-networkd/cmd/uplink-tools/uplink_test.go @@ -7,6 +7,8 @@ import ( "path/filepath" "strings" "testing" + + stdio "git.novox.be/novox/mesh-sdk/go" ) // The node-uplink seat's verbs (novox/hq ADR 0241), every holder's: the resolver file as it is, the mesh's @@ -148,3 +150,30 @@ func TestTheLinksCarryTheirDefaultRouteAndTheirNames(t *testing.T) { t.Fatalf("a manager that does not answer is not said: %+v", answer) } } + +// main.go serves the seat's two verbs first and the holder's own tools after them, and runs a subcommand only when +// the holder's manager.go gives it (design 08 sign-in-page §11). +func TestTheHoldersOwnPartComesBesideTheSeatsVerbs(t *testing.T) { + ran := "" + own := holderOwn{ + Commands: map[string]func([]string) error{"watch": func(args []string) error { ran = strings.Join(args, " "); return nil }}, + Tools: []stdio.Tool{{Name: "own_tool"}}, + } + var names []string + for _, tool := range tools(Machine{}, own) { + names = append(names, tool.Name) + } + if strings.Join(names, " ") != "node-uplink.resolvers node-uplink.links own_tool" { + t.Fatalf("served %v", names) + } + if run, err := command(own, nil); run != nil || err != nil { + t.Fatalf("no arguments is the bundle, got a subcommand or %v", err) + } + if _, err := command(own, []string{"nothing"}); err == nil || !strings.Contains(err.Error(), `"nothing"`) { + t.Fatalf("a subcommand the holder has not was taken: %v", err) + } + run, err := command(own, []string{"watch", "a"}) + if err != nil || run([]string{"a"}) != nil || ran != "a" { + t.Fatalf("the holder's subcommand did not run: %v %q", err, ran) + } +}