diff options
Diffstat (limited to 'internal/recordsdir')
| -rw-r--r-- | internal/recordsdir/recordsdir.go | 43 | ||||
| -rw-r--r-- | internal/recordsdir/recordsdir_test.go | 3 |
2 files changed, 20 insertions, 26 deletions
diff --git a/internal/recordsdir/recordsdir.go b/internal/recordsdir/recordsdir.go index 94f7f0e..6ebf491 100644 --- a/internal/recordsdir/recordsdir.go +++ b/internal/recordsdir/recordsdir.go @@ -6,11 +6,13 @@ import ( "path" "path/filepath" "strings" + "time" ) type Entry struct { - Path string - Host string + Path string + Host string + ModTime time.Time } func HostFromFileName(name string) string { @@ -21,12 +23,12 @@ func HostFromFileName(name string) string { return host } -func listRecordsFileNames(fsys fs.FS, root string) ([]string, error) { +func listRecordsFileNames(fsys fs.FS, root string) ([]Entry, error) { entries, err := fs.ReadDir(fsys, root) if err != nil { return nil, err } - var names []string + var out []Entry for _, e := range entries { if e.IsDir() || !strings.HasSuffix(e.Name(), ".records") { continue @@ -36,38 +38,27 @@ func listRecordsFileNames(fsys fs.FS, root string) ([]string, error) { if err != nil || info.Size() == 0 { continue } - names = append(names, e.Name()) + out = append(out, Entry{ + Path: rel, + Host: HostFromFileName(e.Name()), + ModTime: info.ModTime(), + }) } - return names, nil + return out, nil } // ListNonEmptyFilesFS returns non-empty .records files under root within fsys. func ListNonEmptyFilesFS(fsys fs.FS, root string) ([]Entry, error) { - names, err := listRecordsFileNames(fsys, root) - if err != nil { - return nil, err - } - var out []Entry - for _, name := range names { - out = append(out, Entry{ - Path: path.Join(root, name), - Host: HostFromFileName(name), - }) - } - return out, nil + return listRecordsFileNames(fsys, root) } func ListNonEmptyFiles(dir string) ([]Entry, error) { - names, err := listRecordsFileNames(os.DirFS(dir), ".") + entries, err := listRecordsFileNames(os.DirFS(dir), ".") if err != nil { return nil, err } - var out []Entry - for _, name := range names { - out = append(out, Entry{ - Path: filepath.Join(dir, name), - Host: HostFromFileName(name), - }) + for i := range entries { + entries[i].Path = filepath.Join(dir, filepath.Base(entries[i].Path)) } - return out, nil + return entries, nil } diff --git a/internal/recordsdir/recordsdir_test.go b/internal/recordsdir/recordsdir_test.go index 5e72d9d..40028ac 100644 --- a/internal/recordsdir/recordsdir_test.go +++ b/internal/recordsdir/recordsdir_test.go @@ -49,6 +49,9 @@ func TestListNonEmptyFiles(t *testing.T) { if entries[0].Host != "h1" || filepath.Base(entries[0].Path) != "h1.records" { t.Fatalf("unexpected entry: %#v", entries[0]) } + if entries[0].ModTime.IsZero() { + t.Fatal("expected non-zero ModTime") + } } func TestListNonEmptyFiles_ReadError(t *testing.T) { |
