summaryrefslogtreecommitdiff
path: root/internal/recordsdir/recordsdir.go
blob: 9f3ce5b3e48a1723d7b03488d02152ca6af6b33f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
}