- 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
738 lines
19 KiB
Go
738 lines
19 KiB
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// --- list ---------------------------------------------------------------------
|
|
|
|
func (c *cmd) listLs(args []string) int {
|
|
if _, err := parseArgs(newFlagSet("list ls"), args); err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
cl, err := c.client()
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
raw, err := cl.do("GET", "/lists", nil)
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
return c.printJSON(raw)
|
|
}
|
|
var lists []list
|
|
if err := json.Unmarshal(raw, &lists); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if len(lists) == 0 {
|
|
fmt.Fprintln(c.io.Out, "No lists yet.")
|
|
return 0
|
|
}
|
|
rows := [][]string{{"ID", "ADDRESS", "TITLE", "POST", "JOIN", "ARCHIVE"}}
|
|
for _, l := range lists {
|
|
rows = append(rows, []string{strconv.FormatInt(l.ID, 10), l.Address, l.Title, l.PostPolicy, l.JoinPolicy, l.ArchivePolicy})
|
|
}
|
|
c.table(rows)
|
|
return 0
|
|
}
|
|
|
|
func (c *cmd) listAdd(args []string) int {
|
|
fs := newFlagSet("list add")
|
|
title := fs.String("title", "", "")
|
|
pos, err := parseArgs(fs, args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 1 {
|
|
return c.usageError("usage: parsimony list add <name@domain> [--title TITLE]")
|
|
}
|
|
name, domain, ok := strings.Cut(pos[0], "@")
|
|
if !ok || name == "" || domain == "" {
|
|
return c.usageError("a list address must look like name@domain")
|
|
}
|
|
cl, err := c.client()
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
raw, err := cl.do("POST", "/lists", map[string]string{"name": name, "domain": domain, "title": *title})
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
return c.printJSON(raw)
|
|
}
|
|
var l list
|
|
json.Unmarshal(raw, &l)
|
|
fmt.Fprintf(c.io.Out, "Created %s (id %d)\n", l.Address, l.ID)
|
|
return 0
|
|
}
|
|
|
|
func (c *cmd) listShow(args []string) int {
|
|
pos, err := parseArgs(newFlagSet("list show"), args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 1 {
|
|
return c.usageError("usage: parsimony list show <list>")
|
|
}
|
|
l, err := c.resolveList(pos[0])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
raw, _ := json.Marshal(l)
|
|
return c.printJSON(raw)
|
|
}
|
|
c.table([][]string{
|
|
{"id:", strconv.FormatInt(l.ID, 10)},
|
|
{"address:", l.Address},
|
|
{"title:", l.Title},
|
|
{"post policy:", l.PostPolicy},
|
|
{"join policy:", l.JoinPolicy},
|
|
{"archive policy:", l.ArchivePolicy},
|
|
})
|
|
return 0
|
|
}
|
|
|
|
func (c *cmd) listSet(args []string) int {
|
|
fs := newFlagSet("list set")
|
|
fs.String("title", "", "")
|
|
fs.String("post", "", "")
|
|
fs.String("join", "", "")
|
|
fs.String("archive", "", "")
|
|
pos, err := parseArgs(fs, args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 1 {
|
|
return c.usageError("usage: parsimony list set <list> [--title T] [--post P] [--join J] [--archive A]")
|
|
}
|
|
fields := map[string]string{"title": "title", "post": "post_policy", "join": "join_policy", "archive": "archive_policy"}
|
|
patch := map[string]string{}
|
|
fs.Visit(func(f *flag.Flag) { patch[fields[f.Name]] = f.Value.String() })
|
|
if len(patch) == 0 {
|
|
return c.usageError("nothing to change: give at least one of --title, --post, --join, --archive")
|
|
}
|
|
l, err := c.resolveList(pos[0])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
cl, _ := c.client()
|
|
raw, err := cl.do("PATCH", fmt.Sprintf("/lists/%d", l.ID), patch)
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
return c.printJSON(raw)
|
|
}
|
|
fmt.Fprintf(c.io.Out, "Updated %s\n", printable(l.Address))
|
|
return 0
|
|
}
|
|
|
|
func (c *cmd) listRm(args []string) int {
|
|
fs := newFlagSet("list rm")
|
|
yes := fs.Bool("yes", false, "")
|
|
pos, err := parseArgs(fs, args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 1 {
|
|
return c.usageError("usage: parsimony list rm <list> --yes")
|
|
}
|
|
if !*yes {
|
|
return c.usageError("refusing to delete %s with its members and archive without --yes", pos[0])
|
|
}
|
|
l, err := c.resolveList(pos[0])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
cl, _ := c.client()
|
|
if _, err := cl.do("DELETE", fmt.Sprintf("/lists/%d", l.ID), nil); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
return c.done("Deleted " + l.Address)
|
|
}
|
|
|
|
// done prints a success message (or a JSON ack) and returns 0.
|
|
func (c *cmd) done(msg string) int {
|
|
if c.json {
|
|
return c.printJSON(nil)
|
|
}
|
|
fmt.Fprintln(c.io.Out, printable(msg))
|
|
return 0
|
|
}
|
|
|
|
// --- members --------------------------------------------------------------------
|
|
|
|
type member struct {
|
|
Address string `json:"address"`
|
|
DisplayName string `json:"display_name"`
|
|
Disabled bool `json:"disabled"`
|
|
BounceCount int `json:"bounce_count"`
|
|
}
|
|
|
|
func (m member) status() string {
|
|
state := "active"
|
|
if m.Disabled {
|
|
state = "disabled"
|
|
}
|
|
if m.BounceCount > 0 {
|
|
return fmt.Sprintf("%s (%s)", state, plural(m.BounceCount, "bounce", "bounces"))
|
|
}
|
|
return state
|
|
}
|
|
|
|
func (c *cmd) memberLs(args []string) int {
|
|
fs := newFlagSet("member ls")
|
|
emails := fs.Bool("emails", false, "")
|
|
pos, err := parseArgs(fs, args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 1 {
|
|
return c.usageError("usage: parsimony member ls <list> [--emails]")
|
|
}
|
|
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/members", l.ID), nil)
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
return c.printJSON(raw)
|
|
}
|
|
var ms []member
|
|
if err := json.Unmarshal(raw, &ms); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if *emails {
|
|
for _, m := range ms {
|
|
fmt.Fprintln(c.io.Out, printable(m.Address))
|
|
}
|
|
return 0
|
|
}
|
|
if len(ms) == 0 {
|
|
fmt.Fprintln(c.io.Out, "No members yet.")
|
|
return 0
|
|
}
|
|
rows := [][]string{{"ADDRESS", "NAME", "STATUS"}}
|
|
for _, m := range ms {
|
|
rows = append(rows, []string{m.Address, m.DisplayName, m.status()})
|
|
}
|
|
c.table(rows)
|
|
return 0
|
|
}
|
|
|
|
func (c *cmd) memberAdd(args []string) int {
|
|
fs := newFlagSet("member add")
|
|
name := fs.String("name", "", "")
|
|
pos, err := parseArgs(fs, args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 2 {
|
|
return c.usageError("usage: parsimony member add <list> <email> [--name NAME]")
|
|
}
|
|
l, err := c.resolveList(pos[0])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
raw, err := c.addMember(l.ID, pos[1], *name)
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
return c.printJSON(raw)
|
|
}
|
|
var m member
|
|
json.Unmarshal(raw, &m)
|
|
fmt.Fprintf(c.io.Out, "Added %s to %s\n", printable(m.Address), printable(l.Address))
|
|
return 0
|
|
}
|
|
|
|
func (c *cmd) addMember(listID int64, address, name string) (json.RawMessage, error) {
|
|
cl, err := c.client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cl.do("POST", fmt.Sprintf("/lists/%d/members", listID), map[string]string{"address": address, "display_name": name})
|
|
}
|
|
|
|
func (c *cmd) memberRm(args []string) int {
|
|
pos, err := parseArgs(newFlagSet("member rm"), args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 2 {
|
|
return c.usageError("usage: parsimony member rm <list> <email>")
|
|
}
|
|
l, err := c.resolveList(pos[0])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
cl, _ := c.client()
|
|
if _, err := cl.do("DELETE", fmt.Sprintf("/lists/%d/members/%s", l.ID, url.PathEscape(pos[1])), nil); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
return c.done(fmt.Sprintf("Removed %s from %s", strings.ToLower(pos[1]), l.Address))
|
|
}
|
|
|
|
func (c *cmd) memberImport(args []string) int {
|
|
pos, err := parseArgs(newFlagSet("member import"), args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 2 {
|
|
return c.usageError("usage: parsimony member import <list> <file|->")
|
|
}
|
|
l, err := c.resolveList(pos[0])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
var in io.Reader = c.io.In
|
|
if pos[1] != "-" {
|
|
f, err := os.Open(pos[1])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
defer f.Close()
|
|
in = f
|
|
}
|
|
|
|
imported, failed := 0, 0
|
|
sc := bufio.NewScanner(in)
|
|
sc.Buffer(make([]byte, 64*1024), 1024*1024)
|
|
for n := 1; sc.Scan(); n++ {
|
|
line := strings.TrimSpace(sc.Text())
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
parts := strings.SplitN(line, ",", 3)
|
|
address, name := strings.TrimSpace(parts[0]), ""
|
|
if len(parts) > 1 {
|
|
name = strings.TrimSpace(parts[1])
|
|
}
|
|
if _, err := c.addMember(l.ID, address, name); err != nil {
|
|
failed++
|
|
fmt.Fprintf(c.io.Err, "line %d: %s\n", n, printable(fmt.Sprintf("%s: %v", line, err)))
|
|
continue
|
|
}
|
|
imported++
|
|
}
|
|
if err := sc.Err(); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
msg := fmt.Sprintf("Imported %d members into %s", imported, l.Address)
|
|
if failed > 0 {
|
|
msg += fmt.Sprintf(", %d failed", failed)
|
|
}
|
|
fmt.Fprintln(c.io.Out, printable(msg))
|
|
if failed > 0 {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// --- held messages ----------------------------------------------------------------
|
|
|
|
func (c *cmd) heldLs(args []string) int {
|
|
pos, err := parseArgs(newFlagSet("held ls"), args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 1 {
|
|
return c.usageError("usage: parsimony held ls <list>")
|
|
}
|
|
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/held", l.ID), nil)
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
return c.printJSON(raw)
|
|
}
|
|
var hs []struct {
|
|
ID int64 `json:"id"`
|
|
Sender string `json:"sender"`
|
|
Subject string `json:"subject"`
|
|
Size int `json:"size"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
Reason string `json:"reason"`
|
|
From string `json:"from"`
|
|
Mismatch bool `json:"mismatch"`
|
|
}
|
|
if err := json.Unmarshal(raw, &hs); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if len(hs) == 0 {
|
|
fmt.Fprintln(c.io.Out, "Nothing is held for moderation.")
|
|
return 0
|
|
}
|
|
rows := [][]string{{"ID", "RECEIVED", "SENDER", "FROM", "SUBJECT", "SIZE", "REASON"}}
|
|
for _, h := range hs {
|
|
subject := h.Subject
|
|
if subject == "" {
|
|
subject = "(no subject)"
|
|
}
|
|
from := h.From
|
|
if from == "" {
|
|
from = "-"
|
|
}
|
|
if h.Mismatch {
|
|
from += " (differs)" // readers would see someone other than the sender
|
|
}
|
|
rows = append(rows, []string{strconv.FormatInt(h.ID, 10), formatDate(h.CreatedAt), h.Sender, from, subject, formatBytes(h.Size), h.Reason})
|
|
}
|
|
c.table(rows)
|
|
return 0
|
|
}
|
|
|
|
// byID runs an action on the numeric id given as the only argument. With needYes
|
|
// the command refuses to run without --yes.
|
|
func (c *cmd) byID(name string, args []string, needYes bool, act func(cl *client, id int64) error, done string) int {
|
|
fs := newFlagSet(name)
|
|
var yes *bool
|
|
if needYes {
|
|
yes = fs.Bool("yes", false, "")
|
|
}
|
|
pos, err := parseArgs(fs, args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 1 {
|
|
suffix := ""
|
|
if needYes {
|
|
suffix = " --yes"
|
|
}
|
|
return c.usageError("usage: parsimony %s <id>%s", name, suffix)
|
|
}
|
|
id, ok := parseID(pos[0])
|
|
if !ok {
|
|
return c.usageError("%q is not a valid id", pos[0])
|
|
}
|
|
if needYes && !*yes {
|
|
return c.usageError("refusing to run %s %d without --yes", name, id)
|
|
}
|
|
cl, err := c.client()
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if err := act(cl, id); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if done == "" {
|
|
return 0 // the action printed its own output
|
|
}
|
|
return c.done(fmt.Sprintf(done, id))
|
|
}
|
|
|
|
func (c *cmd) heldApprove(args []string) int {
|
|
return c.byID("held approve", args, false, func(cl *client, id int64) error {
|
|
_, err := cl.do("POST", fmt.Sprintf("/held/%d/approve", id), nil)
|
|
return err
|
|
}, "Approved held message %d: queued for delivery")
|
|
}
|
|
|
|
func (c *cmd) heldDiscard(args []string) int {
|
|
return c.byID("held discard", args, false, func(cl *client, id int64) error {
|
|
_, err := cl.do("DELETE", fmt.Sprintf("/held/%d", id), nil)
|
|
return err
|
|
}, "Discarded held message %d")
|
|
}
|
|
|
|
// --- archive ------------------------------------------------------------------------
|
|
|
|
type message struct {
|
|
ID int64 `json:"id"`
|
|
Subject string `json:"subject"`
|
|
Sender string `json:"sender"`
|
|
Date time.Time `json:"date"`
|
|
Body string `json:"body"`
|
|
}
|
|
|
|
func plural(n int, one, many string) string {
|
|
if n == 1 {
|
|
return fmt.Sprintf("%d %s", n, one)
|
|
}
|
|
return fmt.Sprintf("%d %s", n, many)
|
|
}
|
|
|
|
func (c *cmd) archiveLs(args []string) int {
|
|
fs := newFlagSet("archive ls")
|
|
q := fs.String("q", "", "")
|
|
limit := fs.Int("limit", 50, "")
|
|
offset := fs.Int("offset", 0, "")
|
|
pos, err := parseArgs(fs, args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 1 {
|
|
return c.usageError("usage: parsimony archive ls <list> [--q TEXT] [--limit N] [--offset N]")
|
|
}
|
|
l, err := c.resolveList(pos[0])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
cl, _ := c.client()
|
|
query := url.Values{"limit": {strconv.Itoa(*limit)}, "offset": {strconv.Itoa(*offset)}}
|
|
if *q != "" {
|
|
query.Set("q", *q)
|
|
}
|
|
raw, err := cl.do("GET", fmt.Sprintf("/lists/%d/archive?%s", l.ID, query.Encode()), nil)
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
return c.printJSON(raw)
|
|
}
|
|
var page struct {
|
|
Total int `json:"total"`
|
|
Messages []message `json:"messages"`
|
|
}
|
|
if err := json.Unmarshal(raw, &page); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if page.Total == 0 {
|
|
if *q != "" {
|
|
fmt.Fprintln(c.io.Out, "No messages found.")
|
|
} else {
|
|
fmt.Fprintln(c.io.Out, "The archive is empty.")
|
|
}
|
|
return 0
|
|
}
|
|
rows := [][]string{{"ID", "DATE", "FROM", "SUBJECT"}}
|
|
for _, m := range page.Messages {
|
|
rows = append(rows, []string{strconv.FormatInt(m.ID, 10), formatDate(m.Date), m.Sender, m.Subject})
|
|
}
|
|
c.table(rows)
|
|
if len(page.Messages) < page.Total {
|
|
fmt.Fprintf(c.io.Out, "\nShowing %d of %s\n", len(page.Messages), plural(page.Total, "message", "messages"))
|
|
} else {
|
|
fmt.Fprintf(c.io.Out, "\n%s\n", plural(page.Total, "message", "messages"))
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (c *cmd) archiveShow(args []string) int {
|
|
return c.byID("archive show", args, false, func(cl *client, id int64) error {
|
|
raw, err := cl.do("GET", fmt.Sprintf("/archive/%d", id), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if c.json {
|
|
c.printJSON(raw)
|
|
return nil
|
|
}
|
|
var m message
|
|
if err := json.Unmarshal(raw, &m); err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(c.io.Out, "Subject: %s\nFrom: %s\nDate: %s\n\n%s\n", printable(m.Subject), printable(m.Sender), formatDate(m.Date), printable(m.Body))
|
|
return nil
|
|
}, "")
|
|
}
|
|
|
|
func (c *cmd) archiveRm(args []string) int {
|
|
return c.byID("archive rm", args, true, func(cl *client, id int64) error {
|
|
_, err := cl.do("DELETE", fmt.Sprintf("/archive/%d", id), nil)
|
|
return err
|
|
}, "Deleted archived message %d")
|
|
}
|
|
|
|
func (c *cmd) archiveImport(args []string) int {
|
|
pos, err := parseArgs(newFlagSet("archive import"), args)
|
|
if err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
if len(pos) != 2 {
|
|
return c.usageError("usage: parsimony archive import <list> <file|->")
|
|
}
|
|
l, err := c.resolveList(pos[0])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
var in io.Reader = c.io.In
|
|
if pos[1] != "-" {
|
|
f, err := os.Open(pos[1])
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
defer f.Close()
|
|
in = f
|
|
}
|
|
cl, _ := c.client()
|
|
raw, err := cl.uploadTo(fmt.Sprintf("/lists/%d/archive/import", l.ID), in, "application/mbox")
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
var sum struct {
|
|
Imported int `json:"imported"`
|
|
Duplicates int `json:"duplicates"`
|
|
Failed int `json:"failed"`
|
|
Errors []string `json:"errors"`
|
|
}
|
|
if err := json.Unmarshal(raw, &sum); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
c.printJSON(raw)
|
|
} else {
|
|
msg := fmt.Sprintf("Imported %s into %s", plural(sum.Imported, "message", "messages"), l.Address)
|
|
if sum.Duplicates > 0 {
|
|
msg += fmt.Sprintf(", %d already present", sum.Duplicates)
|
|
}
|
|
if sum.Failed > 0 {
|
|
msg += fmt.Sprintf(", %d failed", sum.Failed)
|
|
}
|
|
fmt.Fprintln(c.io.Out, printable(msg))
|
|
}
|
|
for _, e := range sum.Errors {
|
|
fmt.Fprintln(c.io.Err, printable(e))
|
|
}
|
|
if sum.Failed > len(sum.Errors) {
|
|
fmt.Fprintf(c.io.Err, "... and %d more\n", sum.Failed-len(sum.Errors))
|
|
}
|
|
if sum.Failed > 0 {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// --- queue --------------------------------------------------------------------------
|
|
|
|
func (c *cmd) queueLs(args []string) int {
|
|
if _, err := parseArgs(newFlagSet("queue ls"), args); err != nil {
|
|
return c.usageError("%v", err)
|
|
}
|
|
cl, err := c.client()
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
raw, err := cl.do("GET", "/queue", nil)
|
|
if err != nil {
|
|
return c.fail(err)
|
|
}
|
|
if c.json {
|
|
return c.printJSON(raw)
|
|
}
|
|
var q struct {
|
|
Pending int `json:"pending"`
|
|
Retrying int `json:"retrying"`
|
|
Failed int `json:"failed"`
|
|
Oldest *time.Time `json:"oldest"`
|
|
Items []struct {
|
|
ID int64 `json:"id"`
|
|
Recipient string `json:"recipient"`
|
|
Attempts int `json:"attempts"`
|
|
LastError string `json:"last_error"`
|
|
NextAttempt time.Time `json:"next_attempt"`
|
|
Failed bool `json:"failed"`
|
|
} `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(raw, &q); err != nil {
|
|
return c.fail(err)
|
|
}
|
|
oldest := "-"
|
|
if q.Oldest != nil {
|
|
oldest = formatDate(*q.Oldest)
|
|
}
|
|
fmt.Fprintf(c.io.Out, "Pending: %d\nRetrying: %d\nFailed: %d\nOldest: %s\n", q.Pending, q.Retrying, q.Failed, oldest)
|
|
if len(q.Items) == 0 {
|
|
fmt.Fprintln(c.io.Out, "\nThe queue is empty.")
|
|
return 0
|
|
}
|
|
fmt.Fprintln(c.io.Out)
|
|
rows := [][]string{{"ID", "RECIPIENT", "STATE", "ATTEMPTS", "NEXT ATTEMPT", "LAST ERROR"}}
|
|
for _, i := range q.Items {
|
|
state, next := "waiting", formatDate(i.NextAttempt)
|
|
switch {
|
|
case i.Failed:
|
|
state, next = "failed", "-" // never retried
|
|
case i.Attempts > 0:
|
|
state = "retrying"
|
|
}
|
|
rows = append(rows, []string{strconv.FormatInt(i.ID, 10), i.Recipient, state, strconv.Itoa(i.Attempts), next, i.LastError})
|
|
}
|
|
c.table(rows)
|
|
if q.Pending > len(q.Items) {
|
|
fmt.Fprintf(c.io.Out, "\nShowing %d of %d deliveries\n", len(q.Items), q.Pending)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (c *cmd) queueRm(args []string) int {
|
|
return c.byID("queue rm", args, false, func(cl *client, id int64) error {
|
|
_, err := cl.do("DELETE", fmt.Sprintf("/queue/%d", id), nil)
|
|
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
|
|
}
|