]> git.giacomolli.uk - onechat/commitdiff
Move shared helpers to files of their own
authorAnderson A. Giacomolli <agiacomolli@gmail.com>
Fri, 6 Nov 2026 17:35:21 +0000 (14:35 -0300)
committerAnderson A. Giacomolli <agiacomolli@gmail.com>
Fri, 6 Nov 2026 17:35:21 +0000 (14:35 -0300)
writeFileAtomic moves from memory.go to file.go with no change, because
the token file of the login uses it too, and a helper in the file of its
first user hides the dependency of the other. The test clock moves from
log_test.go to clock_test.go for the same reason: the memory, tree, and
replay tests use it only as a clock.

A search of every cross-file use finds no other helper with an unrelated
user. The other shared declarations are parts of the memory core or of
the model client that compose for one purpose.

Issue: b14d537

clock_test.go [new file with mode: 0644]
file.go [new file with mode: 0644]
log_test.go
memory.go

diff --git a/clock_test.go b/clock_test.go
new file mode 100644 (file)
index 0000000..61437a5
--- /dev/null
@@ -0,0 +1,18 @@
+package main
+
+import "time"
+
+// testClock is a clock that tests set by hand.
+type testClock struct {
+       now time.Time
+}
+
+func newTestClock() *testClock {
+       return &testClock{
+               now: time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC),
+       }
+}
+
+func (c *testClock) Now() time.Time {
+       return c.now
+}
diff --git a/file.go b/file.go
new file mode 100644 (file)
index 0000000..a7ba5bb
--- /dev/null
+++ b/file.go
@@ -0,0 +1,30 @@
+package main
+
+import (
+       "errors"
+       "fmt"
+       "os"
+       "path/filepath"
+)
+
+// writeFileAtomic replaces the file at path with data through a temporary file
+// and a rename. A reader or a crash of the process sees the old file or the new
+// file, never a part of one.
+func writeFileAtomic(path string, data []byte) error {
+       file, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
+       if err != nil {
+               return fmt.Errorf("create temporary file: %w", err)
+       }
+
+       _, err = file.Write(data)
+       err = errors.Join(err, file.Close())
+       if err == nil {
+               err = os.Rename(file.Name(), path)
+       }
+
+       if err != nil {
+               return errors.Join(fmt.Errorf("replace file: %w", err), os.Remove(file.Name()))
+       }
+
+       return nil
+}
index 60b9b6428e68d2576fd35b933125e4e50f186ebc..89b87b337a7240709934057742938fd2f5b39f59 100644 (file)
@@ -145,18 +145,3 @@ func TestLogRejectsAGapInTheIDs(t *testing.T) {
 func truncateToOneAndAHalf(path string) error {
        return os.Truncate(path, positionBytes+positionBytes/2)
 }
 func truncateToOneAndAHalf(path string) error {
        return os.Truncate(path, positionBytes+positionBytes/2)
 }
-
-// testClock is a clock that tests set by hand.
-type testClock struct {
-       now time.Time
-}
-
-func newTestClock() *testClock {
-       return &testClock{
-               now: time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC),
-       }
-}
-
-func (c *testClock) Now() time.Time {
-       return c.now
-}
index 733673bf8607b50019e8417a80d4d0d9c6352151..cb55cab9820de7f5cda2436921312c1d1cd31f56 100644 (file)
--- a/memory.go
+++ b/memory.go
@@ -529,25 +529,3 @@ func (m *Memory) context(level, index int) string {
 
        return m.cview.Context(end)
 }
 
        return m.cview.Context(end)
 }
-
-// writeFileAtomic replaces the file at path with data through a temporary file
-// and a rename. A reader or a crash of the process sees the old file or the new
-// file, never a part of one.
-func writeFileAtomic(path string, data []byte) error {
-       file, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
-       if err != nil {
-               return fmt.Errorf("create temporary file: %w", err)
-       }
-
-       _, err = file.Write(data)
-       err = errors.Join(err, file.Close())
-       if err == nil {
-               err = os.Rename(file.Name(), path)
-       }
-
-       if err != nil {
-               return errors.Join(fmt.Errorf("replace file: %w", err), os.Remove(file.Name()))
-       }
-
-       return nil
-}