Files
mesh-delivery/modules/mesh-delivery-view/cmd/mesh-delivery-cli/main.go
T
jschoubben 3c742145d7 View: the pipelines page takes a limit, the newest 20 by default
The design gives list a limit with a default of 20; the view asked 20
always and neither the web page nor the terminal view could ask another.
The web list takes a limit query parameter and the terminal view a -limit
flag; a followed branch's list is read at the default, any other limit is
read at the asking.

Issue 1983, review round 1 finding 2.
Builds: design 47 H2 H-A-467 (1)
2026-11-22 11:42:27 +01:00

203 lines
6.7 KiB
Go

// Command mesh-delivery-cli is the operator's command for the delivery view (novox/hq ADR 0429 §3). It asks the
// view's local socket for the rows and prints them; it holds no credential. `answer <ask key> <option> [words]` asks the view
// to write the operator's answer to an ask that needs no proof of who answered (the view refuses the merge's Approve). `holds`
// lists every fingerprint the assigner holds with its node, cause and answer; `hold-release <module> <fingerprint> [words]` and
// `hold-keep <module> <fingerprint> [words]` answer its hold ask, again when the operator changes their mind (issue 1906).
// `pipelines [branch]` and `pipeline <pipeline> [mover]` are the pipelines page (design 47 H2 H-A-467): a branch's newest
// pipelines, one pipeline's mover rows or one mover's steps; with -follow printed again at each change the runner's events say.
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
"mesh-delivery-view/internal/view"
)
const defaultSocket = "/var/lib/mesh-delivery-view-cli/cli.sock"
func main() {
socket := os.Getenv("MESH_DELIVERY_SOCKET")
if socket == "" {
socket = defaultSocket
}
os.Exit(run(os.Args[1:], socket, os.Stdout, os.Stderr))
}
func run(args []string, socket string, out, errOut io.Writer) int {
fs := flag.NewFlagSet("mesh-delivery-cli", flag.ContinueOnError)
fs.SetOutput(errOut)
asJSON := fs.Bool("json", false, "print the rows as JSON")
follow := fs.Bool("follow", false, "pipelines and pipeline: print again at each change the runner's events say, until interrupted")
next := fs.String("next", "", "pipeline: the cursor of the next page a pipeline's answer named")
limit := fs.Int("limit", view.DefaultPipelinesLimit, "pipelines: the newest this many pipelines of the branch")
if err := fs.Parse(args); err != nil {
return 2
}
if fs.NArg() > 0 && (fs.Arg(0) == "pipelines" || fs.Arg(0) == "pipeline") {
if *limit < 1 {
fmt.Fprintln(errOut, "mesh-delivery-cli: -limit is a number of at least one")
return 2
}
return pipelines(socket, fs.Args(), *next, *limit, *follow, *asJSON, out, errOut)
}
if fs.NArg() > 0 && fs.Arg(0) == "holds" {
return holds(socket, *asJSON, out, errOut)
}
if fs.NArg() > 0 && (fs.Arg(0) == "hold-release" || fs.Arg(0) == "hold-keep") {
if fs.NArg() < 3 {
fmt.Fprintf(errOut, "mesh-delivery-cli %s <module> <fingerprint> [words]\n", fs.Arg(0))
return 2
}
option := strings.TrimPrefix(fs.Arg(0), "hold-")
r, err := view.AnswerHoldOn(socket, fs.Arg(1), fs.Arg(2), option, strings.Join(fs.Args()[3:], " "))
if err != nil {
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
return 1
}
fmt.Fprintf(out, "answered the hold of %s at %s with %s; the teller fills the ask from it and the assigner reads it\n", r.Module, r.FP, option)
return 0
}
if fs.NArg() > 0 {
if fs.Arg(0) != "answer" || fs.NArg() < 3 {
fmt.Fprintln(errOut, "mesh-delivery-cli shows the rows, or answers: mesh-delivery-cli answer <ask key> <option> [words]; holds; hold-release|hold-keep <module> <fingerprint> [words]; [-follow] [-limit <n>] pipelines [branch]; [-follow] [-next <cursor>] pipeline <pipeline> [mover]")
return 2
}
key, option := fs.Arg(1), fs.Arg(2)
if err := view.AnswerAsk(socket, key, option, strings.Join(fs.Args()[3:], " ")); err != nil {
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
return 1
}
fmt.Fprintf(out, "answered %s with %s; the teller fills the ask from it\n", key, option)
return 0
}
s, err := view.Ask(socket)
if err != nil {
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
return 1
}
if *asJSON {
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
if err := enc.Encode(s); err != nil {
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
return 1
}
return 0
}
view.Table(out, s)
return 0
}
// pipelines prints the pipelines page (design 47 H2 H-A-467): `pipelines [branch]` the newest pipelines of a branch (main by
// default), `pipeline <pipeline> [mover]` one pipeline's mover rows or one mover's steps; with -follow again at each change.
func pipelines(socket string, args []string, next string, limit int, follow, asJSON bool, out, errOut io.Writer) int {
var branch, pipeline, mover string
switch {
case args[0] == "pipelines" && len(args) <= 2:
if len(args) == 2 {
branch = args[1]
}
case args[0] == "pipeline" && (len(args) == 2 || len(args) == 3):
pipeline = args[1]
if len(args) == 3 {
mover = args[2]
}
default:
fmt.Fprintln(errOut, "mesh-delivery-cli [-follow] [-limit <n>] pipelines [branch]; mesh-delivery-cli [-follow] [-next <cursor>] pipeline <pipeline> [mover]")
return 2
}
var failed error
err := view.AskPipelines(socket, branch, limit, pipeline, mover, next, follow, func(page view.PipelinesPage, show *seatShow) bool {
if follow {
fmt.Fprintf(out, "--- %s ---\n", time.Now().Format("15:04:05"))
}
if asJSON {
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
var v any = page
if show != nil {
v = show
}
if failed = enc.Encode(v); failed != nil {
return false
}
return true
}
if show != nil {
view.PipelineTable(out, *show, mover)
} else {
view.PipelinesTable(out, page)
}
return true
})
if err == nil {
err = failed
}
if err != nil {
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
return 1
}
return 0
}
// seatShow is the seat's answer of show, as the view hands it on.
type seatShow = seat.ShowAnswer
// holds prints every hold: module, fingerprint, node, since, answer, words.
func holds(socket string, asJSON bool, out, errOut io.Writer) int {
rows, err := view.AskHolds(socket)
if err != nil {
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
return 1
}
if asJSON {
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
if err := enc.Encode(rows); err != nil {
fmt.Fprintln(errOut, "mesh-delivery-cli:", err)
return 1
}
return 0
}
if len(rows) == 0 {
fmt.Fprintln(out, "the assigner holds nothing")
return 0
}
tw := tabwriter.NewWriter(out, 0, 2, 2, ' ', 0)
fmt.Fprintln(tw, "MODULE\tFINGERPRINT\tNODE\tSINCE\tANSWER\tWORDS")
for _, r := range rows {
answer := r.Answer
switch {
case r.AskKey == "":
answer = "no ask"
case r.Pending != "":
answer = r.Pending + " (from the view, the teller fills it)"
case answer == "":
answer = "not answered"
}
fp := strings.TrimPrefix(r.FP, "src2:")
if len(fp) > 12 {
fp = fp[:12]
}
since := ""
if !r.Since.IsZero() {
since = r.Since.Local().Format("2006-01-02 15:04")
}
words := r.Words
if r.Error != "" {
words = "unreadable: " + r.Error
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\t%s\n", r.Module, fp, r.Node, since, answer, words)
}
tw.Flush()
return 0
}