Files
Reims-Go/internal/app/snapshot_flow.go
NwflowerandClaude Opus 5.5 565577589a refactor: give each VM action one window binding
Every VM action had two bindings: one by name and one by driver and name.
The window used only the second, so the first is gone and the WithDriver
suffix with it. Unused bindings and frontend exports are removed too.

Tests now reject bindings the window never calls, stale generated
bindings and unused frontend exports. TypeScript rejects unused locals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:16:28 -07:00

62 lines
2.1 KiB
Go

package app
import (
"errors"
"fmt"
"strings"
"github.com/Nwflower/reims-go/internal/driver"
"github.com/Nwflower/reims-go/internal/i18n"
"github.com/Nwflower/reims-go/internal/vmstart"
)
// ListSnapshots returns all driver-owned restore points for a VM, newest first.
func (a *App) ListSnapshots(driverID, name string) ([]driver.DiskSnapshot, error) {
a.mu.Lock()
svc := a.lifecycleSvc
a.mu.Unlock()
if svc == nil {
return nil, errors.New("driver lifecycle is not initialized")
}
return svc.ListSnapshots(a.baseCtx(), driver.VMRef{DriverID: driverID, Name: name})
}
func (a *App) CreateSnapshot(driverID, name, snapName string) error {
return a.runSnapshotOp(driverID, name, "create", snapName, "Created restore point")
}
func (a *App) RestoreSnapshot(driverID, name, snapName string) error {
return a.runSnapshotOp(driverID, name, "restore", snapName, "Restored restore point")
}
func (a *App) DeleteSnapshot(driverID, name, snapName string) error {
return a.runSnapshotOp(driverID, name, "delete", snapName, "Deleted restore point")
}
func (a *App) runSnapshotOp(driverID, name, operation, snapName, verb string) error {
loc := a.locale()
cleanSnap := strings.TrimSpace(snapName)
if cleanSnap == "" {
return i18n.NewCodedError(loc, "E_SNAPSHOT_NAME_EMPTY", "err.snapshot.name_empty", nil, nil)
}
if len([]rune(cleanSnap)) > 64 {
return i18n.NewCodedError(loc, "E_SNAPSHOT_NAME_TOO_LONG", "err.snapshot.name_too_long", nil, nil)
}
if _, err := driver.ValidateSnapshotName(cleanSnap); err != nil {
return i18n.NewCodedError(loc, "E_SNAPSHOT_NAME_INVALID", "err.snapshot.name_invalid", nil, err)
}
a.mu.Lock()
svc := a.lifecycleSvc
a.mu.Unlock()
if svc == nil {
return errors.New("driver lifecycle is not initialized")
}
ref := driver.VMRef{DriverID: driverID, Name: name}
w := vmstart.LineWriter(func(line string) { a.emitVMLogForDriver(driverID, name, line) })
if err := svc.RunSnapshot(a.baseCtx(), ref, operation, cleanSnap, w, w); err != nil {
return err
}
a.emitVMLogForDriver(driverID, name, fmt.Sprintf("%s %q", verb, cleanSnap))
a.wakePoll()
a.emitStateChanged()
return nil
}