Files
skillshare/internal/sync/extras_restore_preview.go
T
Willie 88f5201911 feat(instructions): choose sync modes, preview restores, and move files
The AGENTS.md tab picked each target's mode for the user (import where
supported, a link elsewhere), which left Windows users without Developer
Mode no way out of a broken link and gave no copy option. Restore replaced
a file without showing what would come back, and only custom tools could
change which file a target reads.

- Each connected target gets a mode picker (import, symlink, copy); symlink
  is unavailable when the machine cannot create file links, and a target
  whose file is a folder link says so.
- Restore opens a preview of the file after restore and its diff.
- Every supported target can change its file location and name in a
  dialog, with reset to default.
- The target file tab is a single column with the read order on one line,
  a tinted managed import block, soft wrap, and an edit/preview switch;
  universal lists the files of the tools that read its skills.
- The convert dialog shows only the methods that apply and a ready preview.

Refs #299
2026-09-28 13:55:51 +08:00

93 lines
3.0 KiB
Go

package sync
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// What restoring a single-file extra target leaves at its path.
const (
RestoreKindContent = "content" // a file with ExtraRestorePreview.Content
RestoreKindDelete = "delete" // no file: none existed before attaching
RestoreKindLink = "link" // the user's own link to ExtraRestorePreview.LinkTo
)
// ExtraRestorePreview describes what RestoreExtraTarget would put back.
type ExtraRestorePreview struct {
Kind string
Content string
LinkTo string
// RecordedAt is when the target was attached; zero without a record.
RecordedAt time.Time
// Drift: edits made after attaching are, or will be, kept as a drift
// backup rather than restored.
Drift bool
}
// PreviewRestoreExtraTarget reports what RestoreExtraTarget(f) would put back,
// read from the attach-time record, without changing anything.
func PreviewRestoreExtraTarget(f ExtraFile) ExtraRestorePreview {
dir := extraBackupDir(f.Target)
record, at := extraAttachRecord(f.Target)
p := ExtraRestorePreview{RecordedAt: at, Drift: hasExtraDriftSince(dir, at)}
if f.Mode == "import" {
// Only the import line goes, unless nothing else is left in the file.
data, _ := os.ReadFile(f.Target)
stripped, _ := removeImportLine(string(data), f.importLine())
if strings.TrimSpace(stripped) != "" || record == "" {
p.Kind, p.Content = RestoreKindContent, stripped
return p
}
} else if info, err := os.Lstat(f.Target); err == nil && info.Mode().IsRegular() && !contentEqual(f.Source, f.Target) && !isOurExtraCopy(f.Target) {
// An edited target is saved as a drift backup when restored.
p.Drift = true
}
switch record {
case attachCreated:
p.Kind = RestoreKindDelete
case attachRestoreLink:
dest, _ := os.ReadFile(filepath.Join(dir, record))
p.Kind, p.LinkTo = RestoreKindLink, string(dest)
case attachRestore:
data, _ := os.ReadFile(filepath.Join(dir, record))
p.Kind, p.Content = RestoreKindContent, string(data)
default:
// Without a record, restore falls back to the latest backup.
if data, ok, err := latestExtraBackup(f.Target); ok && err == nil {
p.Kind, p.Content = RestoreKindContent, string(data)
} else {
p.Kind = RestoreKindDelete
}
}
return p
}
// extraAttachRecord returns the attach-time record of path and when it was
// written, or "" when there is none.
func extraAttachRecord(path string) (string, time.Time) {
dir := extraBackupDir(path)
for _, name := range []string{attachCreated, attachRestore, attachRestoreLink} {
if info, err := os.Stat(filepath.Join(dir, name)); err == nil {
return name, info.ModTime()
}
}
return "", time.Time{}
}
// hasExtraDriftSince reports whether a drift backup in dir was made at or
// after since (any, when since is zero).
func hasExtraDriftSince(dir string, since time.Time) bool {
for _, name := range extraBackupNames(filepath.Join(dir, "drift")) {
n, err := strconv.ParseInt(strings.TrimSuffix(name, ".bak"), 10, 64)
if err == nil && (since.IsZero() || n >= since.UnixNano()) {
return true
}
}
return false
}