aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/undo.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/admin/undo.go')
-rw-r--r--internal/admin/undo.go113
1 files changed, 89 insertions, 24 deletions
diff --git a/internal/admin/undo.go b/internal/admin/undo.go
index bdd16e6..4921ae5 100644
--- a/internal/admin/undo.go
+++ b/internal/admin/undo.go
@@ -9,15 +9,22 @@ import (
"time"
)
+type ExtraFile struct {
+ FilePath string `json:"file_path"`
+ OldContent []byte `json:"-"`
+ NewContent []byte `json:"-"`
+}
+
type ChangeDesc struct {
- Time string `json:"time"`
- Description string `json:"description"`
- FilePath string `json:"file_path"`
- NewFilePath string `json:"new_file_path,omitempty"`
- OldContent []byte `json:"old_content"`
- NewContent []byte `json:"new_content"`
- IsDelete bool `json:"is_delete"`
- IsCreate bool `json:"is_create"`
+ Time string `json:"time"`
+ Description string `json:"description"`
+ FilePath string `json:"file_path"`
+ NewFilePath string `json:"new_file_path,omitempty"`
+ OldContent []byte `json:"old_content"`
+ NewContent []byte `json:"new_content"`
+ IsDelete bool `json:"is_delete"`
+ IsCreate bool `json:"is_create"`
+ ExtraFiles []ExtraFile `json:"extra_files,omitempty"`
}
type UndoInfo struct {
@@ -77,8 +84,16 @@ func (us *UndoStack) Undo() *ChangeDesc {
log.Printf("undo: failed to restore deleted file %s: %v", last.FilePath, err)
return nil
}
+ for _, ef := range last.ExtraFiles {
+ if err := os.WriteFile(ef.FilePath, ef.OldContent, 0644); err != nil {
+ log.Printf("undo: failed to restore extra file %s: %v", ef.FilePath, err)
+ }
+ }
} else if last.IsCreate {
os.Remove(last.FilePath)
+ for _, ef := range last.ExtraFiles {
+ os.Remove(ef.FilePath)
+ }
} else {
if last.NewFilePath != "" {
if err := os.WriteFile(last.FilePath, last.OldContent, 0644); err != nil {
@@ -91,6 +106,11 @@ func (us *UndoStack) Undo() *ChangeDesc {
return nil
}
}
+ for _, ef := range last.ExtraFiles {
+ if err := os.WriteFile(ef.FilePath, ef.OldContent, 0644); err != nil {
+ log.Printf("undo: failed to restore extra file %s: %v", ef.FilePath, err)
+ }
+ }
}
us.redo = append(us.redo, last)
@@ -122,8 +142,18 @@ func (us *UndoStack) Redo() *ChangeDesc {
return nil
}
}
+ for _, ef := range last.ExtraFiles {
+ if err := os.WriteFile(ef.FilePath, ef.NewContent, 0644); err != nil {
+ log.Printf("redo: failed to create extra file %s: %v", ef.FilePath, err)
+ }
+ }
} else if last.IsDelete {
os.Remove(last.FilePath)
+ for _, ef := range last.ExtraFiles {
+ if err := os.WriteFile(ef.FilePath, ef.NewContent, 0644); err != nil {
+ log.Printf("redo: failed to write extra file %s: %v", ef.FilePath, err)
+ }
+ }
} else {
if last.NewFilePath != "" {
if err := os.WriteFile(last.NewFilePath, last.NewContent, 0644); err != nil {
@@ -136,6 +166,11 @@ func (us *UndoStack) Redo() *ChangeDesc {
return nil
}
}
+ for _, ef := range last.ExtraFiles {
+ if err := os.WriteFile(ef.FilePath, ef.NewContent, 0644); err != nil {
+ log.Printf("redo: failed to write extra file %s: %v", ef.FilePath, err)
+ }
+ }
}
us.history = append(us.history, last)
@@ -162,15 +197,21 @@ func (us *UndoStack) Info() UndoInfo {
}
func (us *UndoStack) save() {
+ type extraEntry struct {
+ FilePath string `json:"file_path"`
+ OldContent string `json:"old_content"`
+ NewContent string `json:"new_content"`
+ }
type entry struct {
- Time string `json:"time"`
- Description string `json:"description"`
- FilePath string `json:"file_path"`
- NewFilePath string `json:"new_file_path,omitempty"`
- OldContent string `json:"old_content"`
- NewContent string `json:"new_content"`
- IsDelete bool `json:"is_delete"`
- IsCreate bool `json:"is_create"`
+ Time string `json:"time"`
+ Description string `json:"description"`
+ FilePath string `json:"file_path"`
+ NewFilePath string `json:"new_file_path,omitempty"`
+ OldContent string `json:"old_content"`
+ NewContent string `json:"new_content"`
+ IsDelete bool `json:"is_delete"`
+ IsCreate bool `json:"is_create"`
+ ExtraFiles []extraEntry `json:"extra_files,omitempty"`
}
type saveData struct {
History []entry `json:"history"`
@@ -179,6 +220,14 @@ func (us *UndoStack) save() {
toEntries := func(changes []ChangeDesc) []entry {
var entries []entry
for _, c := range changes {
+ var extra []extraEntry
+ for _, ef := range c.ExtraFiles {
+ extra = append(extra, extraEntry{
+ FilePath: ef.FilePath,
+ OldContent: string(ef.OldContent),
+ NewContent: string(ef.NewContent),
+ })
+ }
entries = append(entries, entry{
Time: c.Time,
Description: c.Description,
@@ -188,6 +237,7 @@ func (us *UndoStack) save() {
NewContent: string(c.NewContent),
IsDelete: c.IsDelete,
IsCreate: c.IsCreate,
+ ExtraFiles: extra,
})
}
return entries
@@ -209,15 +259,21 @@ func (us *UndoStack) load() {
if err != nil {
return
}
+ type extraEntry struct {
+ FilePath string `json:"file_path"`
+ OldContent string `json:"old_content"`
+ NewContent string `json:"new_content"`
+ }
type entry struct {
- Time string `json:"time"`
- Description string `json:"description"`
- FilePath string `json:"file_path"`
- NewFilePath string `json:"new_file_path,omitempty"`
- OldContent string `json:"old_content"`
- NewContent string `json:"new_content"`
- IsDelete bool `json:"is_delete"`
- IsCreate bool `json:"is_create"`
+ Time string `json:"time"`
+ Description string `json:"description"`
+ FilePath string `json:"file_path"`
+ NewFilePath string `json:"new_file_path,omitempty"`
+ OldContent string `json:"old_content"`
+ NewContent string `json:"new_content"`
+ IsDelete bool `json:"is_delete"`
+ IsCreate bool `json:"is_create"`
+ ExtraFiles []extraEntry `json:"extra_files,omitempty"`
}
var saveData struct {
History []entry `json:"history"`
@@ -229,6 +285,14 @@ func (us *UndoStack) load() {
fromEntries := func(entries []entry) []ChangeDesc {
var changes []ChangeDesc
for _, e := range entries {
+ var extra []ExtraFile
+ for _, ef := range e.ExtraFiles {
+ extra = append(extra, ExtraFile{
+ FilePath: ef.FilePath,
+ OldContent: []byte(ef.OldContent),
+ NewContent: []byte(ef.NewContent),
+ })
+ }
changes = append(changes, ChangeDesc{
Time: e.Time,
Description: e.Description,
@@ -238,6 +302,7 @@ func (us *UndoStack) load() {
NewContent: []byte(e.NewContent),
IsDelete: e.IsDelete,
IsCreate: e.IsCreate,
+ ExtraFiles: extra,
})
}
return changes