diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-14 11:16:45 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-14 11:16:45 +0300 |
| commit | 2bc4e64acf93f04c8871d964d75f041ada57f89d (patch) | |
| tree | c156e09a6c65c47e35006a9c108f4a85e78ef1e2 /internal/recordsdir | |
| parent | a58a6cf092338e0dac45608efa9f2c7b81cbe01f (diff) | |
refactor: share .records file discovery (ask u3)
Extract ListNonEmptyFiles and HostFromFileName into internal/recordsdir
for aggregate and storage ImportFromDir. Behavior unchanged.
Made-with: Cursor
Diffstat (limited to 'internal/recordsdir')
| -rw-r--r-- | internal/recordsdir/recordsdir.go | 40 | ||||
| -rw-r--r-- | internal/recordsdir/recordsdir_test.go | 58 |
2 files changed, 98 insertions, 0 deletions
diff --git a/internal/recordsdir/recordsdir.go b/internal/recordsdir/recordsdir.go new file mode 100644 index 0000000..9f3ce5b --- /dev/null +++ b/internal/recordsdir/recordsdir.go @@ -0,0 +1,40 @@ +package recordsdir + +import ( + "os" + "path/filepath" + "strings" +) + +type Entry struct { + Path string + Host string +} + +func HostFromFileName(name string) string { + host := strings.TrimSuffix(name, filepath.Ext(name)) + if idx := strings.Index(host, "."); idx > 0 { + host = host[:idx] + } + return host +} + +func ListNonEmptyFiles(dir string) ([]Entry, error) { + entries, err := os.ReadDir(dir) + if err != nil { + return nil, err + } + var out []Entry + for _, e := range entries { + if e.IsDir() || !strings.HasSuffix(e.Name(), ".records") { + continue + } + path := filepath.Join(dir, e.Name()) + info, err := os.Stat(path) + if err != nil || info.Size() == 0 { + continue + } + out = append(out, Entry{Path: path, Host: HostFromFileName(e.Name())}) + } + return out, nil +} diff --git a/internal/recordsdir/recordsdir_test.go b/internal/recordsdir/recordsdir_test.go new file mode 100644 index 0000000..bee8d67 --- /dev/null +++ b/internal/recordsdir/recordsdir_test.go @@ -0,0 +1,58 @@ +package recordsdir + +import ( + "os" + "path/filepath" + "testing" +) + +func TestHostFromFileName(t *testing.T) { + tests := []struct { + file string + want string + }{ + {"earth.records", "earth"}, + {"myhost.example.com.records", "myhost"}, + {"single.records", "single"}, + } + for _, tt := range tests { + t.Run(tt.file, func(t *testing.T) { + if got := HostFromFileName(tt.file); got != tt.want { + t.Fatalf("HostFromFileName(%q) = %q, want %q", tt.file, got, tt.want) + } + }) + } +} + +func TestListNonEmptyFiles(t *testing.T) { + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "skip.txt"), []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "empty.records"), nil, 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "h1.records"), []byte("line\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.Mkdir(filepath.Join(dir, "nested.records"), 0o755); err != nil { + t.Fatal(err) + } + entries, err := ListNonEmptyFiles(dir) + if err != nil { + t.Fatal(err) + } + if len(entries) != 1 { + t.Fatalf("len(entries) = %d, want 1: %#v", len(entries), entries) + } + if entries[0].Host != "h1" || filepath.Base(entries[0].Path) != "h1.records" { + t.Fatalf("unexpected entry: %#v", entries[0]) + } +} + +func TestListNonEmptyFiles_ReadError(t *testing.T) { + _, err := ListNonEmptyFiles(filepath.Join(t.TempDir(), "nonexistent-subdir-nope")) + if err == nil { + t.Fatal("expected error") + } +} |
