aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/index/scan.go
blob: dd3d266f68aaa5fb72581e50a7620846d036624c (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
41
42
43
44
45
46
47
package index

import (
	"fmt"
	"log/slog"
	"os"
	"path"

	"github.com/LMBishop/scrapbook/pkg/config"
	"github.com/LMBishop/scrapbook/pkg/site"
)

func ScanDirectory(dir string, dst *SiteIndex) error {
	entries, err := os.ReadDir(dir)
	if err != nil {
		return err
	}

	for _, e := range entries {
		if !e.IsDir() {
			continue
		}

		siteName := e.Name()
		sitePath := path.Join(dir, siteName)
		cfg, err := readSiteConfig(sitePath)
		if err != nil {
			slog.Warn("failed to read site", "site", siteName, "reason", err)
			continue
		}

		site := site.NewSite(siteName, sitePath, cfg)
		dst.AddSite(site)
	}

	return nil
}

func readSiteConfig(dir string) (*config.SiteConfig, error) {
	siteFile := path.Join(dir, "site.toml")
	cfg := &config.SiteConfig{}
	err := config.ReadSiteConfig(siteFile, cfg)
	if err != nil {
		return nil, fmt.Errorf("site file invalid: %s", err)
	}
	return cfg, nil
}