Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/cloudstic/cmd_tui_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
tuiActionNone tuiActionKind = iota
tuiActionUp
tuiActionDown
tuiActionSummaryView
tuiActionHistoryView
tuiActionRun
tuiActionCheck
tuiActionCreate
Expand All @@ -33,6 +35,9 @@ type tuiAction struct {
}

func ensureSelectedProfile(d tui.Dashboard) tui.Dashboard {
if d.SelectedView == "" {
d.SelectedView = tui.ProfileViewSummary
}
if d.SelectedProfile != "" || len(d.Profiles) == 0 {
return d
}
Expand Down Expand Up @@ -93,6 +98,10 @@ func readTUIAction(r io.ByteReader, layout tui.DashboardLayout) (tuiAction, erro
return tuiAction{Kind: tuiActionRun}, nil
case 'c', 'C':
return tuiAction{Kind: tuiActionCheck}, nil
case 's', 'S':
return tuiAction{Kind: tuiActionSummaryView}, nil
case 'h', 'H':
return tuiAction{Kind: tuiActionHistoryView}, nil
case 'n', 'N':
return tuiAction{Kind: tuiActionCreate}, nil
case 'e', 'E':
Expand Down
49 changes: 49 additions & 0 deletions cmd/cloudstic/cmd_tui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,24 @@ func TestReadTUIAction_ParsesCheckShortcut(t *testing.T) {
}
}

func TestReadTUIAction_ParsesViewShortcuts(t *testing.T) {
ev, err := readTUIAction(bufio.NewReader(bytes.NewBufferString("s")), tui.DashboardLayout{})
if err != nil {
t.Fatalf("readTUIAction summary: %v", err)
}
if ev.Kind != tuiActionSummaryView {
t.Fatalf("summary action=%v want %v", ev.Kind, tuiActionSummaryView)
}

ev, err = readTUIAction(bufio.NewReader(bytes.NewBufferString("h")), tui.DashboardLayout{})
if err != nil {
t.Fatalf("readTUIAction history: %v", err)
}
if ev.Kind != tuiActionHistoryView {
t.Fatalf("history action=%v want %v", ev.Kind, tuiActionHistoryView)
}
}

func TestReadTUIAction_ParsesManagementShortcuts(t *testing.T) {
ev, err := readTUIAction(bufio.NewReader(bytes.NewBufferString("n")), tui.DashboardLayout{})
if err != nil {
Expand Down Expand Up @@ -711,6 +729,7 @@ func TestTUISession_HandleActionRunRefreshesDashboard(t *testing.T) {
ProfileCount: 1,
StoreCount: 1,
SelectedProfile: "docs",
SelectedView: tui.ProfileViewSummary,
Profiles: []tui.ProfileCard{
{
Name: "docs",
Expand All @@ -735,6 +754,7 @@ func TestTUISession_HandleActionRunRefreshesDashboard(t *testing.T) {
var out strings.Builder
s := newTUISession(&runner{out: &out, stdoutFile: os.Stdout, stdin: os.Stdin}, "profiles.yaml", tui.Dashboard{
SelectedProfile: "docs",
SelectedView: tui.ProfileViewHistory,
Profiles: []tui.ProfileCard{
{
Name: "docs",
Expand All @@ -756,6 +776,9 @@ func TestTUISession_HandleActionRunRefreshesDashboard(t *testing.T) {
if s.dashboard.SelectedProfile != "docs" {
t.Fatalf("selected profile lost after refresh: %+v", s.dashboard)
}
if s.dashboard.SelectedView != tui.ProfileViewHistory {
t.Fatalf("selected view lost after refresh: %+v", s.dashboard)
}
if len(s.dashboard.Activity.Lines) == 0 {
t.Fatalf("expected activity lines after action")
}
Expand All @@ -767,6 +790,32 @@ func TestTUISession_HandleActionRunRefreshesDashboard(t *testing.T) {
}
}

func TestTUISession_HandleActionSwitchesSelectedView(t *testing.T) {
stubTUITestHooks(t)

s := newTUISession(&runner{out: io.Discard, stdoutFile: os.Stdout, stdin: os.Stdin}, "profiles.yaml", tui.Dashboard{
SelectedProfile: "docs",
SelectedView: tui.ProfileViewSummary,
Profiles: []tui.ProfileCard{
{Name: "docs", Enabled: true, Status: tui.ProfileStatusReady},
},
})

if _, err := s.handleAction(context.Background(), tuiAction{Kind: tuiActionHistoryView}); err != nil {
t.Fatalf("handleAction(history): %v", err)
}
if s.dashboard.SelectedView != tui.ProfileViewHistory {
t.Fatalf("selected view = %q want history", s.dashboard.SelectedView)
}

if _, err := s.handleAction(context.Background(), tuiAction{Kind: tuiActionSummaryView}); err != nil {
t.Fatalf("handleAction(summary): %v", err)
}
if s.dashboard.SelectedView != tui.ProfileViewSummary {
t.Fatalf("selected view = %q want summary", s.dashboard.SelectedView)
}
}

func TestTUISession_HandleActionRunRefreshFailureRestoresRawMode(t *testing.T) {
readEnd, writeEnd, err := os.Pipe()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/cloudstic/tui_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ func (s *tuiSession) handleAction(ctx context.Context, action tuiAction) (int, e
s.dashboard = moveTUISelection(s.dashboard, -1)
case tuiActionDown:
s.dashboard = moveTUISelection(s.dashboard, 1)
case tuiActionSummaryView:
s.dashboard.SelectedView = tui.ProfileViewSummary
case tuiActionHistoryView:
s.dashboard.SelectedView = tui.ProfileViewHistory
case tuiActionSelectProfile:
if action.Profile != "" {
s.dashboard.SelectedProfile = action.Profile
Expand Down Expand Up @@ -344,12 +348,14 @@ func (s *tuiSession) runSuspended(ctx context.Context, fn func(context.Context)

func (s *tuiSession) refresh(ctx context.Context) error {
selected := s.dashboard.SelectedProfile
selectedView := s.dashboard.SelectedView
activity := s.dashboard.Activity
dashboard, err := tuiBuildDashboard(ctx, s.profilesFile)
if err != nil {
return err
}
dashboard.SelectedProfile = selected
dashboard.SelectedView = selectedView
dashboard.Activity = activity
s.dashboard = ensureSelectedProfile(dashboard)
return nil
Expand Down
66 changes: 61 additions & 5 deletions internal/tui/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ type Dashboard struct {
StoreCount int
AuthCount int
SelectedProfile string
SelectedView ProfileView
Activity ActivityPanel
Modal *Modal
Profiles []ProfileCard
}

type ProfileView string

const (
ProfileViewSummary ProfileView = "summary"
ProfileViewHistory ProfileView = "history"
)

type ModalKind string

const (
Expand Down Expand Up @@ -161,9 +169,15 @@ type ProfileCard struct {
BackupState BackupFreshness
LastBackup string
LastRef string
History []ProfileSnapshot
Actions []ProfileAction
}

type ProfileSnapshot struct {
Created string
Ref string
}

type StoreProbe struct {
Status string
Error string
Expand Down Expand Up @@ -216,11 +230,13 @@ func BuildDashboard(cfg *engine.ProfilesConfig, probes map[string]StoreProbe) Da
ProfileCount: len(cfg.Profiles),
StoreCount: len(cfg.Stores),
AuthCount: len(cfg.Auth),
SelectedView: ProfileViewSummary,
Profiles: make([]ProfileCard, 0, len(cfg.Profiles)),
}
for _, name := range names {
profile := cfg.Profiles[name]
status, note := profileStatus(cfg, profile, probes[profile.Store])
history := snapshotHistory(profile.Source, probes[profile.Store].Snapshots)
lastBackup, lastRef, lastCreated := latestBackup(profile.Source, probes[profile.Store].Snapshots)
storeHealth := deriveStoreHealth(cfg, profile, probes[profile.Store])
reachability := deriveStoreReachability(storeHealth)
Expand All @@ -243,6 +259,7 @@ func BuildDashboard(cfg *engine.ProfilesConfig, probes map[string]StoreProbe) Da
BackupState: backupState,
LastBackup: lastBackup,
LastRef: lastRef,
History: history,
Actions: deriveProfileActions(status, storeHealth),
})
}
Expand Down Expand Up @@ -357,20 +374,59 @@ func profileStatus(cfg *engine.ProfilesConfig, p engine.BackupProfile, probe Sto
return ProfileStatusReady, ""
}

func latestBackup(sourceURI string, entries []engine.SnapshotEntry) (string, string, time.Time) {
func snapshotHistory(sourceURI string, entries []engine.SnapshotEntry) []ProfileSnapshot {
want := sourceKeyFromURI(sourceURI)
if want.Type == "" {
return "", "", time.Time{}
return nil
}
type item struct {
created time.Time
entry ProfileSnapshot
}
items := make([]item, 0, len(entries))
for _, entry := range entries {
if snapshotMatchesSource(entry.Snap.Source, want) {
snap := ProfileSnapshot{Ref: entry.Ref}
if entry.Created.IsZero() {
return "unknown time", entry.Ref, time.Time{}
snap.Created = "unknown time"
} else {
snap.Created = entry.Created.Local().Format("2006-01-02 15:04")
}
return entry.Created.Local().Format("2006-01-02 15:04"), entry.Ref, entry.Created
items = append(items, item{created: entry.Created, entry: snap})
}
}
return "", "", time.Time{}
sort.SliceStable(items, func(i, j int) bool {
return items[i].created.After(items[j].created)
})
history := make([]ProfileSnapshot, 0, len(items))
for _, item := range items {
history = append(history, item.entry)
}
return history
}

func latestBackup(sourceURI string, entries []engine.SnapshotEntry) (string, string, time.Time) {
want := sourceKeyFromURI(sourceURI)
if want.Type == "" {
return "", "", time.Time{}
}
var latest *engine.SnapshotEntry
for i := range entries {
entry := entries[i]
if !snapshotMatchesSource(entry.Snap.Source, want) {
continue
}
if latest == nil || entry.Created.After(latest.Created) {
latest = &entry
}
}
if latest == nil {
return "", "", time.Time{}
}
if latest.Created.IsZero() {
return "unknown time", latest.Ref, time.Time{}
}
return latest.Created.Local().Format("2006-01-02 15:04"), latest.Ref, latest.Created
}

func deriveStoreHealth(cfg *engine.ProfilesConfig, p engine.BackupProfile, probe StoreProbe) StoreHealth {
Expand Down
60 changes: 60 additions & 0 deletions internal/tui/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func TestBuildDashboard_SortsProfilesAndCountsSections(t *testing.T) {
if got.Profiles[0].LastRef != "snapshot/abc" {
t.Fatalf("last ref = %q want snapshot/abc", got.Profiles[0].LastRef)
}
if got.SelectedView != ProfileViewSummary {
t.Fatalf("selected view = %q want summary", got.SelectedView)
}
if got.Profiles[0].Status != ProfileStatusReady {
t.Fatalf("status = %q want ready", got.Profiles[0].Status)
}
Expand All @@ -82,6 +85,12 @@ func TestBuildDashboard_SortsProfilesAndCountsSections(t *testing.T) {
if got.Profiles[0].BackupState != BackupFreshnessRecent {
t.Fatalf("backup state = %q want recent", got.Profiles[0].BackupState)
}
if len(got.Profiles[0].History) != 1 {
t.Fatalf("history entries = %d want 1", len(got.Profiles[0].History))
}
if got.Profiles[0].History[0].Ref != "snapshot/abc" {
t.Fatalf("history ref = %q want snapshot/abc", got.Profiles[0].History[0].Ref)
}
if len(got.Profiles[0].Actions) != 2 || got.Profiles[0].Actions[0].Kind != ActionKindBackup || !got.Profiles[0].Actions[0].Enabled {
t.Fatalf("unexpected actions: %+v", got.Profiles[0].Actions)
}
Expand Down Expand Up @@ -165,6 +174,57 @@ func TestBuildDashboardFromConfig_LoadsStoreSnapshots(t *testing.T) {
}
}

func TestBuildDashboard_BuildsProfileHistoryNewestFirst(t *testing.T) {
cfg := &engine.ProfilesConfig{
Stores: map[string]engine.ProfileStore{
"remote": {URI: "s3:bucket/prod"},
},
Profiles: map[string]engine.BackupProfile{
"docs": {Source: "local:/docs", Store: "remote"},
},
}

got := BuildDashboard(cfg, map[string]StoreProbe{
"remote": {
Status: "ok",
Snapshots: []engine.SnapshotEntry{
{
Ref: "snapshot/old",
Created: mustTime(t, "2026-04-02T10:00:00Z"),
Snap: core.Snapshot{
Source: &core.SourceInfo{Type: "local", Path: "/docs"},
},
},
{
Ref: "snapshot/new",
Created: mustTime(t, "2026-04-03T12:30:00Z"),
Snap: core.Snapshot{
Source: &core.SourceInfo{Type: "local", Path: "/docs"},
},
},
{
Ref: "snapshot/other",
Created: mustTime(t, "2026-04-04T08:00:00Z"),
Snap: core.Snapshot{
Source: &core.SourceInfo{Type: "local", Path: "/photos"},
},
},
},
},
})

if len(got.Profiles) != 1 {
t.Fatalf("profiles=%d want 1", len(got.Profiles))
}
history := got.Profiles[0].History
if len(history) != 2 {
t.Fatalf("history entries = %d want 2", len(history))
}
if history[0].Ref != "snapshot/new" || history[1].Ref != "snapshot/old" {
t.Fatalf("unexpected history order: %+v", history)
}
}

func TestBuildDashboardFromConfig_StoreErrorBecomesWarning(t *testing.T) {
cfg := &engine.ProfilesConfig{
Stores: map[string]engine.ProfileStore{
Expand Down
Loading
Loading