backups: one Show for page and CLI; no dash; db pull's --instance help
Fix round 1 for Task 5 (#898). - backupswords.Show decides, from the door's answer, the chips, the tile values, the reason line and the db pull commands; Date and AtExample move there too. backupsSectionFor and printBackups now only render. The console's class, reason and files constants are the leaf's. - The CLI prints no dash: Restore test shows "not shown yet", and an unknown history prints its label alone. The page keeps its tile dash. - --instance help is db pull's, shared as dbInstanceUsage.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+398
−218
db.go+5 −1instances_backups.go+28 −98instances_backups_test.go+19 −4internal/backupswords/display.go+172 −0internal/backupswords/display_test.go+121 −0internal/backupswords/words.go+6 −5internal/console/backups.go+18 −16internal/console/instancepage.go+29 −94
CI cancelled — superseded by a newer push — run details
Intent
Attributed to this commit: 18.1 g CO₂e (2,338,504 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on member-backups-1, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/db.go b/db.go| index df35e7db8..8aeb98ea0 100644 |
| --- a/db.go |
| +++ b/db.go |
| @@ -101,12 +101,16 @@ type dbTarget struct { |
| at time.Time |
| } |
| +// dbInstanceUsage is --instance's help, shared by db pull, db query and |
| +// instances backups, so the one flag reads one way. |
| +const dbInstanceUsage = "the instance whose database to read (may be left out when the app has one)" |
| + |
| // dbFlags registers the flags both verbs share and returns the resolver. |
| // Status lines go to stderr: query's stdout is its rows, and must pipe |
| // cleanly into whatever reads them. |
| func dbFlags(fs *flag.FlagSet, verb string) func() dbTarget { |
| app := appFlag(fs, "app whose database to read") |
| - instance := fs.String("instance", "", "the instance whose database to read (may be left out when the app has one)") |
| + instance := fs.String("instance", "", dbInstanceUsage) |
| acctRaw, acctIDRaw, _ := accountFlags(fs) |
| consoleFlag := consoleOption(fs, "console to read through (default: the only logged-in one)") |
| atRaw := fs.String("at", "", "read the database as it was at this time (RFC 3339, for example 2026-10-07T14:00:00Z)") |
diff --git a/instances_backups.go b/instances_backups.go| index 68b62c59c..dcfdabe15 100644 |
| --- a/instances_backups.go |
| +++ b/instances_backups.go |
| @@ -57,7 +57,7 @@ func instancesBackups(ctx context.Context, args []string, stdout, stderr io.Writ |
| fs := flag.NewFlagSet("instances backups", flag.ContinueOnError) |
| fs.SetOutput(stderr) |
| app := appFlag(fs, "app the instance belongs to") |
| - instance := fs.String("instance", "", "the instance (may be left out when the app has one)") |
| + instance := fs.String("instance", "", dbInstanceUsage) |
| asJSON := fs.Bool("json", false, "print the console's answer as JSON") |
| acctRaw, acctIDRaw, _ := accountFlags(fs) |
| consoleFlag := consoleOption(fs, "console to read through (default: the only logged-in one)") |
| @@ -109,75 +109,33 @@ func backupsShow(ctx context.Context, sess *apiSession, acct apiAcct, app, insta |
| return 0 |
| } |
| -// printBackups writes v as the instance page lays it out |
| -// (backupsSectionFor, internal/console/instancepage.go): the three tile |
| -// rows when the copy's newest point is known, the two "What we keep" rows |
| -// with their chips, the one reason line when a row needs one, and the two |
| -// db pull commands when db pull can serve the instance. Times are UTC, as |
| -// on the page; now decides "today" and the --at example. |
| +// printBackups writes v as the instance page lays it out, through the |
| +// same decisions (backupswords.Show): the three tile rows when the copy's |
| +// newest point is known, the two "What we keep" rows with their chips, the |
| +// one reason line when a row needs one, and the two db pull commands when |
| +// db pull can serve the instance. A value that isn't known prints its label |
| +// alone; now decides "today" and the --at example. |
| func printBackups(w io.Writer, app, host string, v backupsJSON, now time.Time) { |
| - now = now.UTC() |
| - files := backupswords.ChipNotShown |
| - if v.Files == "not_copied" { |
| - files = backupswords.ChipNotCopied |
| - } |
| - var db, why string |
| - pull := false |
| - switch v.Class { |
| - case "replicated": |
| - db, pull = backupswords.ChipCopied, true |
| - case "no_copy": |
| - db = backupswords.ChipNotCopied |
| - default: |
| - db = backupswords.ChipNotShown |
| - } |
| - switch v.Reason { |
| - case "no_sleep": |
| - why = backupswords.WhyNoSleep |
| - case "own_address": |
| - why = backupswords.WhyOwnAddress |
| - case "not_instance": |
| - why = strings.ReplaceAll(backupswords.WhyNotInstance, "{host}", host) |
| - case "demo": |
| - why = backupswords.WhyDemo |
| - case "pooled": |
| - why = backupswords.WhyPooled |
| - case "fleet": |
| - why = backupswords.WhyFleet |
| - case "other_region": |
| - // db pull forwards to the instance's region. |
| - why = strings.ReplaceAll(backupswords.WhyOtherRegion, "{region}", v.Region) |
| - pull = true |
| - case "nothing_replicated": |
| - db, why = backupswords.ChipNothingYet, backupswords.WhyNothingYet |
| - case "too_large_to_plan": |
| - // Too large for the console to plan; db pull plans it here. |
| - db, pull = backupswords.ChipUnavailable, true |
| - case "unavailable": |
| - db = backupswords.ChipUnavailable |
| + in := backupswords.Input{Class: v.Class, Reason: v.Reason, RegionLabel: v.Region, |
| + HistoryFrom: v.HistoryFrom, Files: v.Files, Host: host, App: app, Now: now} |
| + if v.CopiedTo != nil { |
| + in.SavedAt = &v.CopiedTo.WrittenAt |
| } |
| + d := backupswords.Show(in) |
| type row struct{ label, value string } |
| var rows []row |
| - var from *time.Time |
| - if v.Class == "replicated" && v.CopiedTo != nil { |
| - saved := v.CopiedTo.WrittenAt.UTC() |
| - day := backupsDate(saved, now, "Mon 2 Jan") |
| - if saved.Year() == now.Year() && saved.YearDay() == now.YearDay() { |
| - day = backupswords.SavedToday |
| - } |
| - history := "—" |
| - if v.HistoryFrom != nil { |
| - h := v.HistoryFrom.UTC() |
| - from = &h |
| - history = backupsDate(h, now, "2 Jan") + " (" + backupswords.HistoryDetail + ")" |
| + if d.Tiles { |
| + history := "" |
| + if d.HistoryDay != "" { |
| + history = d.HistoryDay + " (" + backupswords.HistoryDetail + ")" |
| } |
| rows = append(rows, |
| - row{backupswords.SavedLabel, saved.Format("15:04") + " UTC " + day}, |
| + row{backupswords.SavedLabel, d.SavedTime + " " + d.SavedDay}, |
| row{backupswords.HistoryLabel, history}, |
| - row{backupswords.TestLabel, "— (" + backupswords.TestNone + ")"}) |
| + row{backupswords.TestLabel, backupswords.TestNone}) |
| } |
| - rows = append(rows, row{backupswords.KeepDB, db}, row{backupswords.KeepFiles, files}) |
| + rows = append(rows, row{backupswords.KeepDB, d.DBChip}, row{backupswords.KeepFiles, d.FilesChip}) |
| width := 0 |
| for _, r := range []string{backupswords.SavedLabel, backupswords.HistoryLabel, backupswords.TestLabel, backupswords.KeepDB, backupswords.KeepFiles} { |
| @@ -185,45 +143,17 @@ func printBackups(w io.Writer, app, host string, v backupsJSON, now time.Time) { |
| } |
| fmt.Fprintln(w, strings.ReplaceAll(backupsHeading, "{host}", host)) |
| for _, r := range rows { |
| + if r.value == "" { |
| + fmt.Fprintln(w, r.label) |
| + continue |
| + } |
| fmt.Fprintf(w, "%-*s %s\n", width, r.label, r.value) |
| } |
| - if why != "" { |
| - fmt.Fprintf(w, "\n%s\n", why) |
| - } |
| - if pull { |
| - // db pull needs --out, a path that doesn't exist yet: the host's |
| - // first label, and for --at the time too, so the two don't clash. |
| - file, _, _ := strings.Cut(host, ".") |
| - at := backupsAtExample(from, now) |
| - fmt.Fprintf(w, "\n%s\n carlos db pull --app %s --instance %s --out %s.db\n", backupswords.CLILatest, app, host, file) |
| - fmt.Fprintf(w, "\n%s\n carlos db pull --app %s --instance %s --at %s --out %s-%s.db\n", |
| - backupswords.CLIAt, app, host, at.Format(time.RFC3339), file, at.Format("20060102T1504Z")) |
| - } |
| -} |
| - |
| -// backupsDate is t in layout, with the year when it isn't now's. |
| -func backupsDate(t, now time.Time, layout string) string { |
| - if t.Year() != now.Year() { |
| - layout += " 2006" |
| - } |
| - return t.Format(layout) |
| -} |
| - |
| -// backupsAtExample is the --at in the second command, as the page picks |
| -// it: a day before now, on the hour, unless the kept history starts later, |
| -// and then the first whole minute inside it. db pull refuses a time after |
| -// now and one before the history. |
| -func backupsAtExample(from *time.Time, now time.Time) time.Time { |
| - at := now.UTC().Add(-24 * time.Hour).Truncate(time.Hour) |
| - if from == nil || !at.Before(*from) { |
| - return at |
| - } |
| - at = from.Truncate(time.Minute) |
| - if at.Before(*from) { |
| - at = at.Add(time.Minute) |
| + if d.Why != "" { |
| + fmt.Fprintf(w, "\n%s\n", d.Why) |
| } |
| - if at.After(now) { |
| - at = *from |
| + if d.PullLatest != "" { |
| + fmt.Fprintf(w, "\n%s\n %s\n", backupswords.CLILatest, d.PullLatest) |
| + fmt.Fprintf(w, "\n%s\n %s\n", backupswords.CLIAt, d.PullAt) |
| } |
| - return at |
| } |
diff --git a/instances_backups_test.go b/instances_backups_test.go| index 1336a813e..c55402503 100644 |
| --- a/instances_backups_test.go |
| +++ b/instances_backups_test.go |
| @@ -4,6 +4,7 @@ import ( |
| "bytes" |
| "context" |
| "encoding/json" |
| + "flag" |
| "net/http" |
| "net/http/httptest" |
| "strings" |
| @@ -43,7 +44,7 @@ func TestInstancesBackupsPrintsLines(t *testing.T) { |
| want := "Backups for shop-1.shop.example.com\n" + |
| "Last change saved 14:02 UTC today\n" + |
| "Go back as far as 4 Nov (about 7 days of history)\n" + |
| - "Restore test — (not shown yet)\n" + |
| + "Restore test not shown yet\n" + |
| "Database Copied\n" + |
| "Files on disk Not copied\n" + |
| "\n" + |
| @@ -60,7 +61,7 @@ func TestInstancesBackupsPrintsLines(t *testing.T) { |
| // Every class and reason the door answers prints the page's chips and its |
| // one reason line, tiles only for a read copy, and the commands only where |
| // db pull can serve the instance. No state says "backed up", "up to date", |
| -// "current", or wears a tick. |
| +// "current", or wears a tick or a dash. |
| func TestInstancesBackupsPrintsEachClass(t *testing.T) { |
| cases := []struct { |
| name, body string |
| @@ -73,7 +74,7 @@ func TestInstancesBackupsPrintsEachClass(t *testing.T) { |
| {name: "replicated, history unknown, born HA", |
| body: `{"class":"replicated","copied_to":{"transaction":42,"written_at":"2026-11-09T08:30:00Z"},"files":"not_shown"}`, |
| db: backupswords.ChipCopied, files: backupswords.ChipNotShown, tiles: true, pull: true, |
| - lines: []string{"Last change saved 08:30 UTC Mon 9 Nov", "Go back as far as —"}}, |
| + lines: []string{"Last change saved 08:30 UTC Mon 9 Nov", "Go back as far as"}}, |
| {name: "last year", |
| body: `{"class":"replicated","copied_to":{"transaction":42,"written_at":"2025-12-31T23:00:00Z"},"history_from":"2025-12-30T10:00:00Z","files":"not_copied"}`, |
| db: backupswords.ChipCopied, files: backupswords.ChipNotCopied, tiles: true, pull: true, |
| @@ -144,7 +145,7 @@ func TestInstancesBackupsPrintsEachClass(t *testing.T) { |
| t.Errorf("db pull shown = %v, want %v:\n%s", !c.pull, c.pull, got) |
| } |
| lower := strings.ToLower(got) |
| - for _, banned := range []string{"backed up", "up to date", "current", "✓", "✔"} { |
| + for _, banned := range []string{"backed up", "up to date", "current", "✓", "✔", "—"} { |
| if strings.Contains(lower, banned) { |
| t.Errorf("output says %q:\n%s", banned, got) |
| } |
| @@ -306,3 +307,17 @@ func TestInstancesBackupsBadArgs(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// --instance's help is db pull's, byte for byte: no new copy. |
| +func TestInstancesBackupsInstanceFlagIsDBPulls(t *testing.T) { |
| + db := flag.NewFlagSet("db pull", flag.ContinueOnError) |
| + dbFlags(db, "pull") |
| + want := db.Lookup("instance").Usage |
| + var out, errb bytes.Buffer |
| + if code := instancesBackups(context.Background(), []string{"-h"}, &out, &errb); code != 0 { |
| + t.Fatalf("-h: exit %d", code) |
| + } |
| + if !strings.Contains(errb.String(), "--instance <string>") || !strings.Contains(errb.String(), want) { |
| + t.Fatalf("--instance help is not db pull's %q:\n%s", want, errb.String()) |
| + } |
| +} |
diff --git a/internal/backupswords/display.go b/internal/backupswords/display.go| new file mode 100644 |
| index 000000000..beaa0e0c4 |
| --- /dev/null |
| +++ b/internal/backupswords/display.go |
| @@ -0,0 +1,172 @@ |
| +package backupswords |
| + |
| +import ( |
| + "fmt" |
| + "strings" |
| + "time" |
| +) |
| + |
| +// The backups door's API values (the console's backupsView): its classes, |
| +// its reasons and its files values. The console's own constants are these, |
| +// so the door and Show can't name a value two ways. |
| +const ( |
| + ClassReplicated = "replicated" |
| + ClassNoCopy = "no_copy" |
| + ClassNotShown = "not_shown" |
| + |
| + ReasonNotInstance = "not_instance" |
| + ReasonOwnAddress = "own_address" |
| + ReasonNoSleep = "no_sleep" |
| + ReasonDemo = "demo" |
| + ReasonPooled = "pooled" |
| + ReasonFleet = "fleet" |
| + ReasonOtherRegion = "other_region" |
| + ReasonNothing = "nothing_replicated" |
| + ReasonTooLarge = "too_large_to_plan" |
| + ReasonUnavailable = "unavailable" |
| + |
| + FilesNotCopied = "not_copied" |
| + FilesNotShown = "not_shown" |
| +) |
| + |
| +// Input is what Show needs about one instance's copy: the door's answer |
| +// (Class, Reason, RegionLabel, SavedAt from copied_to.written_at, |
| +// HistoryFrom, Files), whether the instance was born HA, the instance and |
| +// its app for the db pull commands, and the clock. |
| +type Input struct { |
| + Class, Reason string |
| + // RegionLabel fills {region} in the other-region line. |
| + RegionLabel string |
| + // SavedAt is when the copy's newest change was written, nil when the |
| + // copy wasn't read. HistoryFrom is the oldest point it reaches, nil |
| + // when not known. |
| + SavedAt, HistoryFrom *time.Time |
| + Files string |
| + HABorn bool |
| + Host, App string |
| + Now time.Time |
| +} |
| + |
| +// Display is what the page and the CLI show, before either lays it out. |
| +// Every time is UTC. |
| +type Display struct { |
| + // Tiles is whether the three tiles (or the CLI's three rows) show: the |
| + // copy was read and its newest point is known. |
| + Tiles bool |
| + // SavedAt, SavedTime ("14:02 UTC") and SavedDay (SavedToday, or the |
| + // weekday and date, with the year when it isn't now's) are the first |
| + // tile. |
| + SavedAt time.Time |
| + SavedTime, SavedDay string |
| + // HistoryAt and HistoryDay ("4 Nov") are the second tile; HistoryAt is |
| + // nil and HistoryDay "" when the history's start isn't known. |
| + HistoryAt *time.Time |
| + HistoryDay string |
| + // DBChip and FilesChip are the "What we keep" rows; a Gap chip is a |
| + // known gap (Not copied), the rest are neutral. |
| + DBChip, FilesChip string |
| + DBGap, FilesGap bool |
| + // Why is the one reason line, "" for none. |
| + Why string |
| + // PullLatest and PullAt are the two db pull commands, "" when db pull |
| + // can't serve the instance. |
| + PullLatest, PullAt string |
| +} |
| + |
| +// Show decides what the Backups section says about in. The reasons map to |
| +// the approved lines; too_large_to_plan and unavailable read "Can't read |
| +// now" with no line. db pull serves a replicated instance, one in another |
| +// region (it forwards there) and one too large for the console to plan |
| +// (it plans it locally). A class or files value this binary doesn't know |
| +// reads "Not shown here"; a reason it doesn't know gives no line. |
| +func Show(in Input) Display { |
| + now := in.Now.UTC() |
| + d := Display{FilesChip: ChipNotCopied, FilesGap: true} |
| + if in.HABorn || (in.Files != "" && in.Files != FilesNotCopied) { |
| + d.FilesChip, d.FilesGap = ChipNotShown, false |
| + } |
| + pull := false |
| + switch in.Class { |
| + case ClassReplicated: |
| + d.DBChip, pull = ChipCopied, true |
| + case ClassNoCopy: |
| + d.DBChip, d.DBGap = ChipNotCopied, true |
| + default: |
| + d.DBChip = ChipNotShown |
| + } |
| + switch in.Reason { |
| + case ReasonNoSleep: |
| + d.Why = WhyNoSleep |
| + case ReasonOwnAddress: |
| + d.Why = WhyOwnAddress |
| + case ReasonNotInstance: |
| + d.Why = strings.ReplaceAll(WhyNotInstance, "{host}", in.Host) |
| + case ReasonDemo: |
| + d.Why = WhyDemo |
| + case ReasonPooled: |
| + d.Why = WhyPooled |
| + case ReasonFleet: |
| + d.Why = WhyFleet |
| + case ReasonOtherRegion: |
| + d.Why = strings.ReplaceAll(WhyOtherRegion, "{region}", in.RegionLabel) |
| + pull = true |
| + case ReasonNothing: |
| + d.DBChip, d.Why = ChipNothingYet, WhyNothingYet |
| + case ReasonTooLarge: |
| + d.DBChip = ChipUnavailable |
| + pull = true |
| + case ReasonUnavailable: |
| + d.DBChip = ChipUnavailable |
| + } |
| + var from *time.Time |
| + if in.Class == ClassReplicated && in.SavedAt != nil { |
| + saved := in.SavedAt.UTC() |
| + d.Tiles, d.SavedAt, d.SavedTime = true, saved, saved.Format("15:04")+" UTC" |
| + d.SavedDay = Date(saved, now, "Mon 2 Jan") |
| + if saved.Year() == now.Year() && saved.YearDay() == now.YearDay() { |
| + d.SavedDay = SavedToday |
| + } |
| + if in.HistoryFrom != nil { |
| + h := in.HistoryFrom.UTC() |
| + from = &h |
| + d.HistoryAt, d.HistoryDay = &h, Date(h, now, "2 Jan") |
| + } |
| + } |
| + if pull { |
| + // db pull needs --out, a path that doesn't exist yet: the host's |
| + // first label, and for --at the time too, so the two don't clash. |
| + file, _, _ := strings.Cut(in.Host, ".") |
| + at := AtExample(from, now) |
| + d.PullLatest = fmt.Sprintf("carlos db pull --app %s --instance %s --out %s.db", in.App, in.Host, file) |
| + d.PullAt = fmt.Sprintf("carlos db pull --app %s --instance %s --at %s --out %s-%s.db", |
| + in.App, in.Host, at.Format(time.RFC3339), file, at.Format("20060102T1504Z")) |
| + } |
| + return d |
| +} |
| + |
| +// Date is t in layout, with the year when it isn't now's. |
| +func Date(t, now time.Time, layout string) string { |
| + if t.Year() != now.Year() { |
| + layout += " 2006" |
| + } |
| + return t.Format(layout) |
| +} |
| + |
| +// AtExample is the --at in the second command: a day before now, on the |
| +// hour, unless the kept history starts later, and then the first whole |
| +// minute inside it. db pull refuses a time after now and one before the |
| +// history. |
| +func AtExample(from *time.Time, now time.Time) time.Time { |
| + at := now.UTC().Add(-24 * time.Hour).Truncate(time.Hour) |
| + if from == nil || !at.Before(*from) { |
| + return at |
| + } |
| + at = from.Truncate(time.Minute) |
| + if at.Before(*from) { |
| + at = at.Add(time.Minute) |
| + } |
| + if at.After(now) { |
| + at = *from |
| + } |
| + return at |
| +} |
diff --git a/internal/backupswords/display_test.go b/internal/backupswords/display_test.go| new file mode 100644 |
| index 000000000..0ccf3e402 |
| --- /dev/null |
| +++ b/internal/backupswords/display_test.go |
| @@ -0,0 +1,121 @@ |
| +package backupswords |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// Every class and reason the door answers, and the unknowns: the chips, |
| +// the one reason line, whether the tiles and the db pull commands show. |
| +func TestShowEveryClassAndReason(t *testing.T) { |
| + now := time.Date(2026, 11, 11, 15, 0, 0, 0, time.UTC) |
| + saved := time.Date(2026, 11, 11, 14, 2, 31, 0, time.UTC) |
| + from := time.Date(2026, 11, 4, 9, 12, 0, 0, time.UTC) |
| + cases := []struct { |
| + name string |
| + in Input |
| + db, files string |
| + dbGap, filGap bool |
| + why string |
| + tiles, pull bool |
| + }{ |
| + {name: "replicated", in: Input{Class: ClassReplicated, SavedAt: &saved, HistoryFrom: &from, Files: FilesNotCopied}, |
| + db: ChipCopied, files: ChipNotCopied, filGap: true, tiles: true, pull: true}, |
| + {name: "replicated born HA", in: Input{Class: ClassReplicated, SavedAt: &saved, Files: FilesNotShown, HABorn: true}, |
| + db: ChipCopied, files: ChipNotShown, tiles: true, pull: true}, |
| + {name: "replicated, HA, files unset", in: Input{Class: ClassReplicated, SavedAt: &saved, HABorn: true}, |
| + db: ChipCopied, files: ChipNotShown, tiles: true, pull: true}, |
| + {name: "replicated, position unknown", in: Input{Class: ClassReplicated, Files: FilesNotCopied}, |
| + db: ChipCopied, files: ChipNotCopied, filGap: true, pull: true}, |
| + {name: "never sleeps", in: Input{Class: ClassNoCopy, Reason: ReasonNoSleep, Files: FilesNotCopied}, |
| + db: ChipNotCopied, dbGap: true, files: ChipNotCopied, filGap: true, why: WhyNoSleep}, |
| + {name: "own address", in: Input{Class: ClassNoCopy, Reason: ReasonOwnAddress, Files: FilesNotCopied}, |
| + db: ChipNotCopied, dbGap: true, files: ChipNotCopied, filGap: true, why: WhyOwnAddress}, |
| + {name: "not an instance", in: Input{Class: ClassNoCopy, Reason: ReasonNotInstance, Files: FilesNotCopied}, |
| + db: ChipNotCopied, dbGap: true, files: ChipNotCopied, filGap: true, why: "shop-1.shop.example.com is a redirect or site, with no database."}, |
| + {name: "demo", in: Input{Class: ClassNoCopy, Reason: ReasonDemo, Files: FilesNotCopied}, |
| + db: ChipNotCopied, dbGap: true, files: ChipNotCopied, filGap: true, why: WhyDemo}, |
| + {name: "pooled", in: Input{Class: ClassNotShown, Reason: ReasonPooled, Files: FilesNotCopied}, |
| + db: ChipNotShown, files: ChipNotCopied, filGap: true, why: WhyPooled}, |
| + {name: "fleet", in: Input{Class: ClassNotShown, Reason: ReasonFleet, Files: FilesNotShown}, |
| + db: ChipNotShown, files: ChipNotShown, why: WhyFleet}, |
| + {name: "other region", in: Input{Class: ClassNotShown, Reason: ReasonOtherRegion, RegionLabel: "London", Files: FilesNotCopied}, |
| + db: ChipNotShown, files: ChipNotCopied, filGap: true, pull: true, |
| + why: "Copies in London aren't shown here yet. Use the command line below to get one."}, |
| + {name: "nothing yet", in: Input{Class: ClassNotShown, Reason: ReasonNothing, Files: FilesNotCopied}, |
| + db: ChipNothingYet, files: ChipNotCopied, filGap: true, why: WhyNothingYet}, |
| + {name: "too large", in: Input{Class: ClassNotShown, Reason: ReasonTooLarge, Files: FilesNotCopied}, |
| + db: ChipUnavailable, files: ChipNotCopied, filGap: true, pull: true}, |
| + {name: "unavailable", in: Input{Class: ClassNotShown, Reason: ReasonUnavailable, Files: FilesNotCopied}, |
| + db: ChipUnavailable, files: ChipNotCopied, filGap: true}, |
| + {name: "unknown class, reason and files", in: Input{Class: "later", Reason: "later", Files: "later"}, |
| + db: ChipNotShown, files: ChipNotShown}, |
| + } |
| + for _, c := range cases { |
| + t.Run(c.name, func(t *testing.T) { |
| + c.in.Host, c.in.App, c.in.Now = "shop-1.shop.example.com", "shop", now |
| + d := Show(c.in) |
| + if d.DBChip != c.db || d.DBGap != c.dbGap || d.FilesChip != c.files || d.FilesGap != c.filGap { |
| + t.Errorf("chips %q/%v %q/%v, want %q/%v %q/%v", d.DBChip, d.DBGap, d.FilesChip, d.FilesGap, c.db, c.dbGap, c.files, c.filGap) |
| + } |
| + if d.Why != c.why { |
| + t.Errorf("why %q, want %q", d.Why, c.why) |
| + } |
| + if d.Tiles != c.tiles { |
| + t.Errorf("tiles %v, want %v", d.Tiles, c.tiles) |
| + } |
| + if (d.PullLatest != "") != c.pull || (d.PullAt != "") != c.pull { |
| + t.Errorf("pull %q %q, want shown %v", d.PullLatest, d.PullAt, c.pull) |
| + } |
| + for _, s := range []string{d.DBChip, d.FilesChip, d.Why, d.SavedDay, d.HistoryDay} { |
| + for _, banned := range []string{"backed up", "up to date", "current", "✓", "✔", "—"} { |
| + if strings.Contains(strings.ToLower(s), banned) { |
| + t.Errorf("%q says %q", s, banned) |
| + } |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The tiles' clock and the db pull commands: UTC, today or the weekday and |
| +// date, the year only when it isn't now's, and --at a day ago on the hour |
| +// unless the history starts later. |
| +func TestShowTimesAndCommands(t *testing.T) { |
| + now := time.Date(2026, 11, 11, 15, 30, 0, 0, time.UTC) |
| + from := time.Date(2026, 11, 4, 9, 0, 1, 0, time.UTC) |
| + in := func(saved time.Time, from *time.Time) Input { |
| + return Input{Class: ClassReplicated, SavedAt: &saved, HistoryFrom: from, Files: FilesNotCopied, Host: "shop-1.shop.example.com", App: "shop", Now: now} |
| + } |
| + for _, c := range []struct { |
| + saved time.Time |
| + time, day string |
| + }{ |
| + {time.Date(2026, 11, 11, 14, 2, 0, 0, time.FixedZone("CET", 3600)), "13:02 UTC", "today"}, |
| + {time.Date(2026, 11, 10, 23, 59, 0, 0, time.UTC), "23:59 UTC", "Tue 10 Nov"}, |
| + {time.Date(2025, 12, 31, 8, 0, 0, 0, time.UTC), "08:00 UTC", "Wed 31 Dec 2025"}, |
| + } { |
| + d := Show(in(c.saved, &from)) |
| + if d.SavedTime != c.time || d.SavedDay != c.day || d.HistoryDay != "4 Nov" || d.HistoryAt == nil || !d.HistoryAt.Equal(from) { |
| + t.Errorf("saved %s: %q %q %q, want %q %q 4 Nov", c.saved, d.SavedTime, d.SavedDay, d.HistoryDay, c.time, c.day) |
| + } |
| + } |
| + d := Show(in(now.Add(-time.Minute), &from)) |
| + if d.PullLatest != "carlos db pull --app shop --instance shop-1.shop.example.com --out shop-1.db" || |
| + d.PullAt != "carlos db pull --app shop --instance shop-1.shop.example.com --at 2026-11-10T15:00:00Z --out shop-1-20261110T1500Z.db" { |
| + t.Errorf("commands %q %q", d.PullLatest, d.PullAt) |
| + } |
| + short := time.Date(2026, 11, 11, 10, 0, 1, 0, time.UTC) |
| + if d := Show(in(now.Add(-time.Minute), &short)); !strings.Contains(d.PullAt, "--at 2026-11-11T10:01:00Z --out shop-1-20261111T1001Z.db") { |
| + t.Errorf("short history: %q", d.PullAt) |
| + } |
| + if d := Show(in(now.Add(-time.Minute), nil)); !strings.Contains(d.PullAt, "--at 2026-11-10T15:00:00Z ") || d.HistoryDay != "" || d.HistoryAt != nil { |
| + t.Errorf("unknown history: %q %q", d.PullAt, d.HistoryDay) |
| + } |
| + // A history that starts after now (a clock skew) uses its own start. |
| + late := now.Add(30 * time.Second) |
| + if at := AtExample(&late, now); !at.Equal(late) { |
| + t.Errorf("late history: %s", at) |
| + } |
| +} |
diff --git a/internal/backupswords/words.go b/internal/backupswords/words.go| index 5e90be515..69cd3b689 100644 |
| --- a/internal/backupswords/words.go |
| +++ b/internal/backupswords/words.go |
| @@ -1,13 +1,14 @@ |
| // Package backupswords holds the member backups phrases that both the |
| -// console's instance page and `carlos instances backups` show: the labels, |
| -// the chips, the reason lines and the db pull intros. One home, so the page |
| -// and the terminal never word one fact two ways. Every string here is |
| +// console's instance page and `carlos instances backups` show (the labels, |
| +// the chips, the reason lines and the db pull intros) and Show, the one |
| +// set of decisions about which of them an instance gets. One home, so the |
| +// page and the terminal never word or decide one fact two ways. Every string here is |
| // approved copy, pinned by the member backups copy record |
| // (docs/superpowers/plans/member-backups-slice1-copy.json, ids backups.*). |
| // {host} and {region} are filled in when the line is shown. |
| // |
| -// A leaf: it imports nothing, so the root CLI can use it without taking on |
| -// the console. |
| +// A leaf: it imports nothing of this repo's, so the root CLI can use it |
| +// without taking on the console. |
| package backupswords |
| const ( |
diff --git a/internal/console/backups.go b/internal/console/backups.go| index 8a896796c..dc36f19b3 100644 |
| --- a/internal/console/backups.go |
| +++ b/internal/console/backups.go |
| @@ -11,6 +11,7 @@ import ( |
| "sync" |
| "time" |
| + "github.com/carlosframework/platform/internal/backupswords" |
| "github.com/carlosframework/platform/internal/dbdoor" |
| "github.com/carlosframework/platform/internal/dbreads" |
| "github.com/carlosframework/platform/internal/provision" |
| @@ -23,29 +24,30 @@ type backupClass string |
| const ( |
| // backupReplicated: the platform keeps a copy and this console reads it. |
| - backupReplicated backupClass = "replicated" |
| + backupReplicated backupClass = backupswords.ClassReplicated |
| // backupNoCopy: the platform keeps no copy of this database at all. |
| - backupNoCopy backupClass = "no_copy" |
| + backupNoCopy backupClass = backupswords.ClassNoCopy |
| // backupNotShown: a copy may exist, but this console does not show it. |
| - backupNotShown backupClass = "not_shown" |
| + backupNotShown backupClass = backupswords.ClassNotShown |
| ) |
| // The reasons a backupsView gives, and its files values: API values |
| -// rather than copy. The page and the CLI turn them into sentences. |
| +// rather than copy, defined once in backupswords, whose Show turns them |
| +// into the page's and the CLI's words. |
| const ( |
| - backupReasonNotInstance = "not_instance" |
| - backupReasonOwnAddress = "own_address" |
| - backupReasonNoSleep = "no_sleep" |
| - backupReasonDemo = "demo" |
| - backupReasonPooled = "pooled" |
| - backupReasonFleet = "fleet" |
| - backupReasonOtherRegion = "other_region" |
| - backupReasonNothing = "nothing_replicated" |
| - backupReasonTooLarge = "too_large_to_plan" |
| - backupReasonUnavailable = "unavailable" |
| + backupReasonNotInstance = backupswords.ReasonNotInstance |
| + backupReasonOwnAddress = backupswords.ReasonOwnAddress |
| + backupReasonNoSleep = backupswords.ReasonNoSleep |
| + backupReasonDemo = backupswords.ReasonDemo |
| + backupReasonPooled = backupswords.ReasonPooled |
| + backupReasonFleet = backupswords.ReasonFleet |
| + backupReasonOtherRegion = backupswords.ReasonOtherRegion |
| + backupReasonNothing = backupswords.ReasonNothing |
| + backupReasonTooLarge = backupswords.ReasonTooLarge |
| + backupReasonUnavailable = backupswords.ReasonUnavailable |
| - backupFilesNotCopied = "not_copied" |
| - backupFilesNotShown = "not_shown" |
| + backupFilesNotCopied = backupswords.FilesNotCopied |
| + backupFilesNotShown = backupswords.FilesNotShown |
| ) |
| // The read's bounds (spec §1, R1-13): files listed across every level, |
diff --git a/internal/console/instancepage.go b/internal/console/instancepage.go| index 2455976ad..982ec7ef0 100644 |
| --- a/internal/console/instancepage.go |
| +++ b/internal/console/instancepage.go |
| @@ -11,9 +11,9 @@ import ( |
| "net/url" |
| "os" |
| "sort" |
| - "strings" |
| "time" |
| + "github.com/carlosframework/platform/internal/backupswords" |
| "github.com/carlosframework/platform/internal/provision" |
| "github.com/carlosframework/platform/internal/replicas" |
| "github.com/carlosframework/platform/internal/rolloutwords" |
| @@ -363,104 +363,39 @@ const ( |
| ) |
| // backupsSectionFor turns the door's view of host's copy into the section, |
| -// at now. The reasons map to the approved lines; too_large_to_plan and |
| -// unavailable read "Can't read now" with no line (1b says more for a copy |
| -// too large to plan). |
| +// at now. The decisions are backupswords.Show's, the ones `carlos instances |
| +// backups` prints too; this keeps only the page's own rendering: RFC 3339 |
| +// times for <time datetime> and the chips' tones. |
| func backupsSectionFor(v backupsView, app, host string, now time.Time) *backupsSection { |
| - c := backupsCopy |
| - s := &backupsSection{FilesChip: c.ChipNotCopied, FilesTone: backupsToneGap} |
| - if v.HABorn { |
| - s.FilesChip, s.FilesTone = c.ChipNotShown, backupsToneNeutral |
| - } |
| - pull := false |
| - switch v.Class { |
| - case backupReplicated: |
| - s.DBChip, s.DBTone = c.ChipCopied, backupsToneNeutral |
| - pull = true |
| - case backupNoCopy: |
| - s.DBChip, s.DBTone = c.ChipNotCopied, backupsToneGap |
| - default: |
| - s.DBChip, s.DBTone = c.ChipNotShown, backupsToneNeutral |
| - } |
| - switch v.Reason { |
| - case backupReasonNoSleep: |
| - s.Why = c.WhyNoSleep |
| - case backupReasonOwnAddress: |
| - s.Why = c.WhyOwnAddress |
| - case backupReasonNotInstance: |
| - s.Why = strings.ReplaceAll(c.WhyNotInstance, "{host}", host) |
| - case backupReasonDemo: |
| - s.Why = c.WhyDemo |
| - case backupReasonPooled: |
| - s.Why = c.WhyPooled |
| - case backupReasonFleet: |
| - s.Why = c.WhyFleet |
| - case backupReasonOtherRegion: |
| - // db pull forwards to the instance's region (spec §1, R2-2). |
| - s.Why = strings.ReplaceAll(c.WhyOtherRegion, "{region}", v.Region) |
| - pull = true |
| - case backupReasonNothing: |
| - s.DBChip, s.Why = c.ChipNothingYet, c.WhyNothingYet |
| - case backupReasonTooLarge: |
| - // Too large for this console to plan within its bounds; the CLI |
| - // plans it without them. |
| - s.DBChip = c.ChipUnavailable |
| - pull = true |
| - case backupReasonUnavailable: |
| - s.DBChip = c.ChipUnavailable |
| - } |
| - var from *time.Time |
| - if v.Class == backupReplicated && v.CopiedTo != nil { |
| - saved := v.CopiedTo.WrittenAt.UTC() |
| - s.Tiles = true |
| - s.SavedAt, s.SavedTime = saved.Format(time.RFC3339), saved.Format("15:04")+" UTC" |
| - s.SavedDay = backupsDate(saved, now.UTC(), "Mon 2 Jan") |
| - if n := now.UTC(); saved.Year() == n.Year() && saved.YearDay() == n.YearDay() { |
| - s.SavedDay = c.SavedToday |
| - } |
| - if v.HistoryFrom != nil { |
| - h := v.HistoryFrom.UTC() |
| - from = &h |
| - s.HistoryAt, s.HistoryDay = h.Format(time.RFC3339), backupsDate(h, now.UTC(), "2 Jan") |
| + in := backupswords.Input{Class: string(v.Class), Reason: v.Reason, RegionLabel: v.Region, |
| + HistoryFrom: v.HistoryFrom, Files: v.Files, HABorn: v.HABorn, Host: host, App: app, Now: now} |
| + if v.CopiedTo != nil { |
| + in.SavedAt = &v.CopiedTo.WrittenAt |
| + } |
| + d := backupswords.Show(in) |
| + tone := func(gap bool) string { |
| + if gap { |
| + return backupsToneGap |
| } |
| + return backupsToneNeutral |
| } |
| - if pull { |
| - // db pull needs --out, a path that doesn't exist yet: the host's |
| - // first label, and for --at the time too, so the two don't clash. |
| - file, _, _ := strings.Cut(host, ".") |
| - at := backupsAtExample(from, now) |
| - s.PullLatest = fmt.Sprintf("carlos db pull --app %s --instance %s --out %s.db", app, host, file) |
| - s.PullAt = fmt.Sprintf("carlos db pull --app %s --instance %s --at %s --out %s-%s.db", |
| - app, host, at.Format(time.RFC3339), file, at.Format("20060102T1504Z")) |
| + s := &backupsSection{ |
| + Tiles: d.Tiles, |
| + SavedTime: d.SavedTime, |
| + SavedDay: d.SavedDay, |
| + HistoryDay: d.HistoryDay, |
| + DBChip: d.DBChip, DBTone: tone(d.DBGap), |
| + FilesChip: d.FilesChip, FilesTone: tone(d.FilesGap), |
| + Why: d.Why, |
| + PullLatest: d.PullLatest, PullAt: d.PullAt, |
| } |
| - return s |
| -} |
| - |
| -// backupsDate is t in layout, with the year when it isn't now's. |
| -func backupsDate(t, now time.Time, layout string) string { |
| - if t.Year() != now.Year() { |
| - layout += " 2006" |
| + if d.Tiles { |
| + s.SavedAt = d.SavedAt.Format(time.RFC3339) |
| } |
| - return t.Format(layout) |
| -} |
| - |
| -// backupsAtExample is the --at in the second command: a day before now, |
| -// on the hour, unless the kept history starts later, and then the first |
| -// whole minute inside it. db pull refuses a time after now and one before |
| -// the history. |
| -func backupsAtExample(from *time.Time, now time.Time) time.Time { |
| - at := now.UTC().Add(-24 * time.Hour).Truncate(time.Hour) |
| - if from == nil || !at.Before(*from) { |
| - return at |
| - } |
| - at = from.Truncate(time.Minute) |
| - if at.Before(*from) { |
| - at = at.Add(time.Minute) |
| - } |
| - if at.After(now) { |
| - at = *from |
| - } |
| - return at |
| + if d.HistoryAt != nil { |
| + s.HistoryAt = d.HistoryAt.Format(time.RFC3339) |
| + } |
| + return s |
| } |
| // instanceBackups is the Backups section for rt, read in-process through |