From: Anderson A. Giacomolli Date: Fri, 6 Nov 2026 17:35:21 +0000 (-0300) Subject: Move shared helpers to files of their own X-Git-Url: http://git.giacomolli.uk/onechat/commitdiff_plain/0821d23c64f4ffc856760e4914fa39ba2e3260e4?ds=sidebyside Move shared helpers to files of their own 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 --- diff --git a/clock_test.go b/clock_test.go new file mode 100644 index 0000000..61437a5 --- /dev/null +++ b/clock_test.go @@ -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 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 +} diff --git a/log_test.go b/log_test.go index 60b9b64..89b87b3 100644 --- a/log_test.go +++ b/log_test.go @@ -145,18 +145,3 @@ func TestLogRejectsAGapInTheIDs(t *testing.T) { 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 -} diff --git a/memory.go b/memory.go index 733673b..cb55cab 100644 --- a/memory.go +++ b/memory.go @@ -529,25 +529,3 @@ func (m *Memory) context(level, index int) string { 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 -}