feat(cli): parsimony posts ls
- posts ls <list> [--limit N] prints ID, DATE, SUBJECT, RECIPIENTS, SENT, PENDING, FAILED and DROPPED, newest first, "No posts yet." when empty, and the API's JSON with --json - cells go through the same control-character escaping as the other tables; tests for the table, the limit, JSON, errors and escaping
This commit is contained in:
parent
2f2ee71f92
commit
7e43117c74
3 changed files with 147 additions and 0 deletions
|
|
@ -49,6 +49,8 @@ Administration (talks to the REST API):
|
|||
archive import <list> <file|-> add the messages of an mbox file (Mailman export);
|
||||
safe to repeat, threads are rebuilt
|
||||
|
||||
posts ls <list> [--limit N] posts sent to a list with their delivery counts
|
||||
|
||||
queue ls outbound queue
|
||||
queue rm <id> discard one queued delivery
|
||||
|
||||
|
|
@ -105,6 +107,7 @@ func Run(args []string, io IO, getenv func(string) string) int {
|
|||
"held": {"ls": c.heldLs, "approve": c.heldApprove, "discard": c.heldDiscard},
|
||||
"archive": {"ls": c.archiveLs, "show": c.archiveShow, "rm": c.archiveRm, "import": c.archiveImport},
|
||||
"queue": {"ls": c.queueLs, "rm": c.queueRm},
|
||||
"posts": {"ls": c.postsLs},
|
||||
"dkim": {"keygen": c.dkimKeygen, "record": c.dkimRecord, "check": c.dkimCheck},
|
||||
}
|
||||
group, ok := commands[rest[0]]
|
||||
|
|
|
|||
|
|
@ -683,3 +683,56 @@ func (c *cmd) queueRm(args []string) int {
|
|||
return err
|
||||
}, "Discarded queued delivery %d")
|
||||
}
|
||||
|
||||
// postsLs shows the posts queued to a list, newest first, with how far their delivery got.
|
||||
func (c *cmd) postsLs(args []string) int {
|
||||
fs := newFlagSet("posts ls")
|
||||
limit := fs.Int("limit", 50, "")
|
||||
pos, err := parseArgs(fs, args)
|
||||
if err != nil {
|
||||
return c.usageError("%v", err)
|
||||
}
|
||||
if len(pos) != 1 {
|
||||
return c.usageError("usage: parsimony posts ls <list> [--limit N]")
|
||||
}
|
||||
l, err := c.resolveList(pos[0])
|
||||
if err != nil {
|
||||
return c.fail(err)
|
||||
}
|
||||
cl, _ := c.client()
|
||||
raw, err := cl.do("GET", fmt.Sprintf("/lists/%d/posts?limit=%d", l.ID, *limit), nil)
|
||||
if err != nil {
|
||||
return c.fail(err)
|
||||
}
|
||||
if c.json {
|
||||
return c.printJSON(raw)
|
||||
}
|
||||
var ps []struct {
|
||||
ID int64 `json:"id"`
|
||||
Subject string `json:"subject"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Recipients int `json:"recipients"`
|
||||
Sent int `json:"sent"`
|
||||
Failed int `json:"failed"`
|
||||
Dropped int `json:"dropped"`
|
||||
Pending int `json:"pending"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &ps); err != nil {
|
||||
return c.fail(err)
|
||||
}
|
||||
if len(ps) == 0 {
|
||||
fmt.Fprintln(c.io.Out, "No posts yet.")
|
||||
return 0
|
||||
}
|
||||
rows := [][]string{{"ID", "DATE", "SUBJECT", "RECIPIENTS", "SENT", "PENDING", "FAILED", "DROPPED"}}
|
||||
for _, p := range ps {
|
||||
subject := p.Subject
|
||||
if subject == "" {
|
||||
subject = "(no subject)"
|
||||
}
|
||||
rows = append(rows, []string{strconv.FormatInt(p.ID, 10), formatDate(p.CreatedAt), subject, strconv.Itoa(p.Recipients),
|
||||
strconv.Itoa(p.Sent), strconv.Itoa(p.Pending), strconv.Itoa(p.Failed), strconv.Itoa(p.Dropped)})
|
||||
}
|
||||
c.table(rows)
|
||||
return 0
|
||||
}
|
||||
|
|
|
|||
91
internal/cli/posts_test.go
Normal file
91
internal/cli/posts_test.go
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"codeberg.org/squidd/parsimony/internal/store"
|
||||
)
|
||||
|
||||
func TestPostsLs(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
e.ok(t, "list", "add", "dev@example.org")
|
||||
l, _ := e.s.FindList("dev@example.org")
|
||||
if out := e.ok(t, "posts", "ls", "dev@example.org"); !strings.Contains(out, "No posts yet.") {
|
||||
t.Fatalf("empty: %q", out)
|
||||
}
|
||||
now := time.Date(2026, 3, 1, 9, 30, 0, 0, time.UTC)
|
||||
e.s.Now = func() time.Time { return now }
|
||||
rcpts := []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}, {From: "b@x.org", Rcpt: "b@x.com"}, {From: "b@x.org", Rcpt: "c@x.com"}, {From: "b@x.org", Rcpt: "d@x.com"}}
|
||||
e.s.EnqueuePost(l.ID, "mid:<1@x>", []byte("Message-ID: <1@x>\r\nSubject: March issue\r\n\r\nb"), rcpts)
|
||||
due, _ := e.s.DueOutbox(10)
|
||||
e.s.CompleteOutbox(due[0].ID)
|
||||
e.s.CompleteOutbox(due[1].ID)
|
||||
e.s.FailOutbox(due[2].ID, "no")
|
||||
now = now.Add(time.Hour)
|
||||
e.s.EnqueuePost(l.ID, "mid:<2@x>", []byte("Message-ID: <2@x>\r\n\r\nb"), nil)
|
||||
|
||||
out := e.ok(t, "posts", "ls", fmt.Sprint(l.ID))
|
||||
lines := strings.Split(strings.TrimSpace(out), "\n")
|
||||
if len(lines) != 3 {
|
||||
t.Fatalf("posts ls:\n%s", out)
|
||||
}
|
||||
if f := strings.Fields(lines[0]); strings.Join(f, " ") != "ID DATE SUBJECT RECIPIENTS SENT PENDING FAILED DROPPED" {
|
||||
t.Fatalf("header: %q", lines[0])
|
||||
}
|
||||
if f := strings.Fields(lines[1]); strings.Join(f, " ") != "2 2026-03-01 10:30 (no subject) 0 0 0 0 0" {
|
||||
t.Fatalf("newest first: %q", lines[1])
|
||||
}
|
||||
if f := strings.Fields(lines[2]); strings.Join(f, " ") != "1 2026-03-01 09:30 March issue 4 2 1 1 0" {
|
||||
t.Fatalf("row: %q", lines[2])
|
||||
}
|
||||
|
||||
if out := e.ok(t, "posts", "ls", "dev@example.org", "--limit", "1"); strings.Contains(out, "March issue") || !strings.Contains(out, "(no subject)") {
|
||||
t.Fatalf("--limit 1:\n%s", out)
|
||||
}
|
||||
var js []map[string]any
|
||||
if err := json.Unmarshal([]byte(e.ok(t, "--json", "posts", "ls", "dev@example.org")), &js); err != nil || len(js) != 2 ||
|
||||
js[1]["subject"] != "March issue" || js[1]["pending"] != float64(1) || js[1]["message_id"] != "<1@x>" {
|
||||
t.Fatalf("json: %v %v", js, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostsLsErrors(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
e.fails(t, 2, "usage: parsimony posts ls", "posts", "ls")
|
||||
e.fails(t, 2, "usage: parsimony posts ls", "posts", "ls", "a@x", "b@x")
|
||||
e.fails(t, 2, "flag provided but not defined", "posts", "ls", "a@x", "--nope")
|
||||
e.fails(t, 1, "no such list", "posts", "ls", "nope@example.org")
|
||||
e.ok(t, "list", "add", "dev@example.org")
|
||||
e.fails(t, 1, "limit must be", "posts", "ls", "dev@example.org", "--limit", "-1")
|
||||
if out := e.ok(t, "help"); !strings.Contains(out, "posts ls <list> [--limit N]") {
|
||||
t.Fatalf("help lacks posts ls:\n%s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostsLsEscapesControlCharacters(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/api/v1/lists/1":
|
||||
fmt.Fprint(w, `{"id":1,"address":"dev@example.org"}`)
|
||||
case "/api/v1/lists/1/posts":
|
||||
b, _ := json.Marshal([]map[string]any{{"id": 1, "subject": "s " + esc, "created_at": time.Unix(0, 0), "recipients": 1}})
|
||||
w.Write(b)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
e := &env{srv: srv}
|
||||
out := e.ok(t, "posts", "ls", "1")
|
||||
noControl(t, "posts ls", out)
|
||||
if !strings.Contains(out, `\x1b[31mRED`) {
|
||||
t.Fatalf("the escape sequence should stay visible as text:\n%s", out)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue