refactor(maglevc): build the CLI on the golang-cli library

Replace maglevc's hand-rolled command-tree CLI with
git.ipng.ch/ipng/golang-cli v1.3.0, mirroring the evpnc refactor. The
tree (commands.go) and the gRPC-status error unwrap (color.go) stay
app-specific; the generic parts — parse tree, completion, '?'-help, the
readline shell, one-shot dispatch, color helpers, and the watch keypress
handler — now come from the library.

- main.go: a single cli.App[grpcapi.MaglevClient] with a Connect
  callback; drops the flag/color-default/dispatch boilerplate.
- commands.go: `type node = cli.Node[grpcapi.MaglevClient]`; label() ->
  cli.Label(); dyn* gain the captured-args parameter the library's
  Dynamic signature carries; runQuit returns cli.ErrQuit.
- watch.go: keypress.WaitForKey replaces the inline cbreak helper.
- color.go: only formatError remains, reading cli.ColorEnabled().
- delete tree.go, complete.go, shell.go.
- tests use the library API; add TestTreeValid (cli.Validate).

Behavior is unchanged except labels/errors now use the library's bright
ANSI palette (was dark); escape lengths are identical so tabwriter
alignment is unaffected. maglevc additionally gains the OpenBSD readline
fix and BSD-correct watch keypress it previously lacked. Builds on linux
and openbsd.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-05 22:38:34 +02:00
parent d2ee6d009e
commit 76fbe2eee0
10 changed files with 227 additions and 788 deletions
+31 -21
View File
@@ -5,11 +5,13 @@ package main
import (
"strings"
"testing"
cli "git.ipng.ch/ipng/golang-cli"
)
func TestExpandPathsRoot(t *testing.T) {
root := buildTree()
lines := expandPaths(root, "", make(map[*Node]bool))
lines := cli.ExpandPaths(root, "")
// Should include well-known leaf paths.
want := []string{
@@ -41,7 +43,7 @@ func TestExpandPathsRoot(t *testing.T) {
paths := make(map[string]bool, len(lines))
for _, l := range lines {
paths[l.path] = true
paths[l.Path] = true
}
for _, w := range want {
@@ -53,15 +55,15 @@ func TestExpandPathsRoot(t *testing.T) {
func TestExpandPathsShow(t *testing.T) {
root := buildTree()
showNode, _, _ := Walk(root, []string{"show"})
lines := expandPaths(showNode, "show", make(map[*Node]bool))
showNode, _, _ := cli.Walk(root, []string{"show"})
lines := cli.ExpandPaths(showNode, "show")
for _, l := range lines {
if !strings.HasPrefix(l.path, "show ") {
t.Errorf("unexpected path %q: should start with 'show '", l.path)
if !strings.HasPrefix(l.Path, "show ") {
t.Errorf("unexpected path %q: should start with 'show '", l.Path)
}
if l.help == "" {
t.Errorf("path %q has empty help", l.path)
if l.Help == "" {
t.Errorf("path %q has empty help", l.Path)
}
}
// version, frontends, frontends <name>, backends, backends <name>,
@@ -75,8 +77,8 @@ func TestExpandPathsShow(t *testing.T) {
func TestExpandPathsNoCycles(t *testing.T) {
root := buildTree()
// watch events has a self-referencing slot; expandPaths must terminate.
watchEvents, _, _ := Walk(root, []string{"watch", "events"})
lines := expandPaths(watchEvents, "watch events", make(map[*Node]bool))
watchEvents, _, _ := cli.Walk(root, []string{"watch", "events"})
lines := cli.ExpandPaths(watchEvents, "watch events")
// Should produce exactly 2 lines: "watch events" and "watch events <opt>".
if len(lines) != 2 {
@@ -87,8 +89,8 @@ func TestExpandPathsNoCycles(t *testing.T) {
func TestExpandPathsSetBackendName(t *testing.T) {
root := buildTree()
// Walk to the name slot so displayPrefix carries the actual arg.
node, _, _ := Walk(root, []string{"set", "backend", "mybackend"})
lines := expandPaths(node, "set backend mybackend", make(map[*Node]bool))
node, _, _ := cli.Walk(root, []string{"set", "backend", "mybackend"})
lines := cli.ExpandPaths(node, "set backend mybackend")
want := []string{
"set backend mybackend pause",
@@ -100,8 +102,8 @@ func TestExpandPathsSetBackendName(t *testing.T) {
t.Fatalf("expected %d lines, got %d: %v", len(want), len(lines), lines)
}
for i, w := range want {
if lines[i].path != w {
t.Errorf("line %d: got %q, want %q", i, lines[i].path, w)
if lines[i].Path != w {
t.Errorf("line %d: got %q, want %q", i, lines[i].Path, w)
}
}
}
@@ -110,7 +112,7 @@ func TestPrefixMatchCollapsedNouns(t *testing.T) {
root := buildTree()
// "sh ba" → show backends (list all) via prefix matching.
node, args, rem := Walk(root, []string{"sh", "ba"})
node, args, rem := cli.Walk(root, []string{"sh", "ba"})
if node.Run == nil {
t.Fatal("'sh ba' did not reach a Run node")
}
@@ -122,7 +124,7 @@ func TestPrefixMatchCollapsedNouns(t *testing.T) {
}
// "sh ba nginx0" → show backends <name> (get specific) via slot.
node, args, rem = Walk(root, []string{"sh", "ba", "nginx0"})
node, args, rem = cli.Walk(root, []string{"sh", "ba", "nginx0"})
if node.Run == nil {
t.Fatal("'sh ba nginx0' did not reach a Run node")
}
@@ -134,13 +136,13 @@ func TestPrefixMatchCollapsedNouns(t *testing.T) {
}
// "sh fr" → show frontends (list all).
node, _, _ = Walk(root, []string{"sh", "fr"})
node, _, _ = cli.Walk(root, []string{"sh", "fr"})
if node.Run == nil {
t.Fatal("'sh fr' did not reach a Run node")
}
// "sh he icmp" → show healthchecks icmp (get specific).
node, args, _ = Walk(root, []string{"sh", "he", "icmp"})
node, args, _ = cli.Walk(root, []string{"sh", "he", "icmp"})
if node.Run == nil {
t.Fatal("'sh he icmp' did not reach a Run node")
}
@@ -155,7 +157,7 @@ func TestWalkUnknownTokens(t *testing.T) {
// A bare unknown word leaves every token unconsumed and anchors
// the returned node at the root — callers must treat this as
// "unknown command" rather than silently showing the whole tree.
node, _, rem := Walk(root, []string{"foo"})
node, _, rem := cli.Walk(root, []string{"foo"})
if node != root {
t.Errorf("'foo' should leave walk at root, got %q", node.Word)
}
@@ -166,7 +168,7 @@ func TestWalkUnknownTokens(t *testing.T) {
// Partial consumption: "show" matches but "bogus" doesn't. The
// returned remaining is the first unmatched token onwards so the
// caller can point at exactly what was wrong.
node, _, rem = Walk(root, []string{"show", "bogus", "tail"})
node, _, rem = cli.Walk(root, []string{"show", "bogus", "tail"})
if node.Word != "show" {
t.Errorf("'show bogus tail' should stop at show, got %q", node.Word)
}
@@ -179,7 +181,7 @@ func TestExpandPathsWeightSlotWalk(t *testing.T) {
// Verify the weight command is fully walkable (fixes bug: setWeightValue
// and setFrontendPoolName were non-slot nodes that couldn't capture tokens).
root := buildTree()
node, args, _ := Walk(root, []string{"set", "frontend", "web", "pool", "primary", "backend", "be0", "weight", "42"})
node, args, _ := cli.Walk(root, []string{"set", "frontend", "web", "pool", "primary", "backend", "be0", "weight", "42"})
if node.Run == nil {
t.Fatal("Walk did not reach a Run node for full weight command")
}
@@ -190,3 +192,11 @@ func TestExpandPathsWeightSlotWalk(t *testing.T) {
t.Errorf("args[3] (weight): got %q, want 42", args[3])
}
}
// TestTreeValid guards the command tree against authoring faults (more than one
// slot child per node, empty words, duplicate siblings, dead ends).
func TestTreeValid(t *testing.T) {
if err := cli.Validate(buildTree()); err != nil {
t.Fatalf("buildTree() has authoring faults:\n%v", err)
}
}