diff options
| author | historia <[not public]> | 2026-10-05 18:16:47 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-10-05 18:16:47 -0400 |
| commit | 9d8c43cd68c960b9a6b8f781684f763e204a4498 (patch) | |
| tree | c9e2a02094a8700db29db2768a0d058ce259a4f4 /internal/admin | |
| parent | 7a15a5d70004b28bf2a710fa0f536af2446e5b17 (diff) | |
| download | thehouseoficarus-9d8c43cd68c960b9a6b8f781684f763e204a4498.tar.gz | |
fix: shared YAML encoder and journal directory, text cleanup
Diffstat (limited to 'internal/admin')
| -rw-r--r-- | internal/admin/api_helpers.go | 12 | ||||
| -rw-r--r-- | internal/admin/api_rooms.go | 12 | ||||
| -rw-r--r-- | internal/admin/api_rooms_bulk.go | 2 | ||||
| -rw-r--r-- | internal/admin/yaml_util.go | 21 |
4 files changed, 14 insertions, 33 deletions
diff --git a/internal/admin/api_helpers.go b/internal/admin/api_helpers.go index d5e23c3..2aa75f9 100644 --- a/internal/admin/api_helpers.go +++ b/internal/admin/api_helpers.go @@ -8,6 +8,7 @@ import ( "gopkg.in/yaml.v3" + "thehouseoficarus/internal/behavior" "thehouseoficarus/internal/persistence" "thehouseoficarus/internal/world" ) @@ -121,14 +122,5 @@ func writeMapAsYAML(path string, m map[string]any) ([]byte, error) { // marshalMapYAML serializes a generic map without writing: callers that // only need the after-image (mutation-plan preflight) use this. func marshalMapYAML(m map[string]any) ([]byte, error) { - var buf bytes.Buffer - enc := yaml.NewEncoder(&buf) - enc.SetIndent(2) - if err := enc.Encode(m); err != nil { - return nil, err - } - if err := enc.Close(); err != nil { - return nil, err - } - return buf.Bytes(), nil + return behavior.MarshalYAMLMap(m) } diff --git a/internal/admin/api_rooms.go b/internal/admin/api_rooms.go index 76db231..06e736c 100644 --- a/internal/admin/api_rooms.go +++ b/internal/admin/api_rooms.go @@ -726,7 +726,7 @@ func (s *AdminServer) handleRoomLink(w http.ResponseWriter, r *http.Request) { // Both sides are resolved and serialized before any write, and both // are applied in one journaled transaction so a failure cannot leave // an asymmetric link, and one Undo reverses the whole action. - txn := persistence.NewTransaction(filepath.Join(s.dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(s.dataDir)) if err := txn.Replace(pathA, newA, 0644); err != nil { writeJSONError(w, "write A: "+err.Error(), http.StatusInternalServerError) return @@ -868,7 +868,7 @@ func (s *AdminServer) handleRoomLink(w http.ResponseWriter, r *http.Request) { } if len(writes) > 0 { - txn := persistence.NewTransaction(filepath.Join(s.dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(s.dataDir)) for _, wr := range writes { if err := txn.Replace(wr.path, wr.data, 0644); err != nil { writeJSONError(w, "plan unlink: "+err.Error(), http.StatusInternalServerError) @@ -932,7 +932,7 @@ func (s *AdminServer) handleRoomMove(w http.ResponseWriter, r *http.Request) { } // Move the file in one journaled transaction: a failed destination write // or source removal must not leave the same room ID in two directories. - txn := persistence.NewTransaction(filepath.Join(s.dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(s.dataDir)) if err := txn.Move(oldPath, newPath); err != nil { failJSON(w, "plan move: "+err.Error()) return @@ -1054,7 +1054,7 @@ func (s *AdminServer) handleRoomRename(w http.ResponseWriter, r *http.Request) { } // ---- Apply the file changes as one journal transaction. ---- - txn := persistence.NewTransaction(filepath.Join(s.dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(s.dataDir)) if err := txn.Move(oldPath, newPath); err != nil { writeJSONError(w, "plan move: "+err.Error(), http.StatusInternalServerError) return @@ -1240,7 +1240,7 @@ func (s *AdminServer) handleRoomSwap(w http.ResponseWriter, r *http.Request) { // Apply both files in one journaled transaction so a failure cannot leave // the two rooms half-swapped. - txn := persistence.NewTransaction(filepath.Join(s.dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(s.dataDir)) if err := txn.Replace(aPath, newAContent, 0644); err != nil { writeJSONError(w, "plan write A: "+err.Error(), http.StatusInternalServerError) return @@ -1393,7 +1393,7 @@ func (s *AdminServer) handleResolveDuplicate(w http.ResponseWriter, r *http.Requ } // Move + rewrite in one journaled transaction so a failure cannot // leave the same ID in two directories. - txn := persistence.NewTransaction(filepath.Join(s.dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(s.dataDir)) if err := txn.Move(absPath, newRel); err != nil { failJSON(w, "plan rename failed: "+err.Error()) return diff --git a/internal/admin/api_rooms_bulk.go b/internal/admin/api_rooms_bulk.go index 3296375..eb5e89d 100644 --- a/internal/admin/api_rooms_bulk.go +++ b/internal/admin/api_rooms_bulk.go @@ -143,7 +143,7 @@ func (s *AdminServer) handleBulkEdit(w http.ResponseWriter, r *http.Request) { } // ---- Apply as one journaled transaction. ---- - txn := persistence.NewTransaction(filepath.Join(s.dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(s.dataDir)) for _, wr := range writes { if wr.newPath != wr.oldPath { if err := txn.Move(wr.oldPath, wr.newPath); err != nil { diff --git a/internal/admin/yaml_util.go b/internal/admin/yaml_util.go index e1d4942..2ab7ec4 100644 --- a/internal/admin/yaml_util.go +++ b/internal/admin/yaml_util.go @@ -1,15 +1,13 @@ package admin import ( - "bytes" "fmt" "log" "os" "path/filepath" "strings" - "gopkg.in/yaml.v3" - + "thehouseoficarus/internal/behavior" "thehouseoficarus/internal/persistence" ) @@ -20,16 +18,7 @@ func readYAMLFile(dataDir, subdir, id string) ([]byte, string, error) { } func marshalYAML(data any) ([]byte, error) { - var buf bytes.Buffer - enc := yaml.NewEncoder(&buf) - enc.SetIndent(2) - if err := enc.Encode(data); err != nil { - return nil, err - } - if err := enc.Close(); err != nil { - return nil, err - } - return buf.Bytes(), nil + return behavior.MarshalYAML(data) } func writeYAMLFile(path string, data any) ([]byte, error) { @@ -214,7 +203,7 @@ func moveEntityFile(dataDir, subdir, id, targetDir string) (string, string, []by } // Move in one journaled transaction: a failed write or source removal // must not leave the entity in two directories. - txn := persistence.NewTransaction(filepath.Join(dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(dataDir)) if err := txn.Move(oldPath, newPath); err != nil { return "", "", nil, fmt.Errorf("move: %w", err) } @@ -279,7 +268,7 @@ func renameEntityFile(dataDir, subdir, oldID, newID string) (string, string, []b } // Rename in one journaled transaction: a failed write or source removal // must not leave the entity in two directories. - txn := persistence.NewTransaction(filepath.Join(dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(dataDir)) if err := txn.Move(oldPath, newPath); err != nil { return "", "", nil, fmt.Errorf("rename: %w", err) } @@ -335,7 +324,7 @@ func deleteEntityDir(dataDir, subdir, dir string) ([]ChangeDesc, error) { } var changes []ChangeDesc - txn := persistence.NewTransaction(filepath.Join(dataDir, ".journal")) + txn := persistence.NewTransaction(persistence.JournalDir(dataDir)) for _, e := range entries { if e.IsDir() || filepath.Ext(e.Name()) != ".yaml" { continue |
