aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/index
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/index')
-rw-r--r--pkg/index/index.go63
-rw-r--r--pkg/index/scan.go47
2 files changed, 110 insertions, 0 deletions
diff --git a/pkg/index/index.go b/pkg/index/index.go
new file mode 100644
index 0000000..35423bd
--- /dev/null
+++ b/pkg/index/index.go
@@ -0,0 +1,63 @@
+package index
+
+import (
+ "maps"
+ "slices"
+ "sort"
+ "sync"
+
+ "github.com/LMBishop/scrapbook/pkg/site"
+)
+
+type SiteIndex struct {
+ mu sync.RWMutex
+ sites map[string]*site.Site
+ sitesByHost map[string]*site.Site
+}
+
+func NewSiteIndex() *SiteIndex {
+ var siteIndex SiteIndex
+ siteIndex.sites = make(map[string]*site.Site)
+ siteIndex.sitesByHost = make(map[string]*site.Site)
+ return &siteIndex
+}
+
+func (s *SiteIndex) GetSiteByHost(host string) *site.Site {
+ s.mu.RLock()
+ defer s.mu.RUnlock()
+
+ return s.sitesByHost[host]
+}
+
+func (s *SiteIndex) GetSite(site string) *site.Site {
+ s.mu.RLock()
+ defer s.mu.RUnlock()
+
+ return s.sites[site]
+}
+
+func (s *SiteIndex) GetSites() []*site.Site {
+ s.mu.RLock()
+ defer s.mu.RUnlock()
+
+ sites := slices.Collect(maps.Values(s.sites))
+ sort.Slice(sites, func(i, j int) bool {
+ return sites[i].Name < sites[j].Name
+ })
+ return sites
+}
+
+func (s *SiteIndex) AddSite(site *site.Site) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ s.sites[site.Name] = site
+ s.updateSiteIndexes()
+}
+
+func (s *SiteIndex) updateSiteIndexes() {
+ clear(s.sitesByHost)
+ for _, site := range s.sites {
+ s.sitesByHost[site.SiteConfig.Host] = site
+ }
+}
diff --git a/pkg/index/scan.go b/pkg/index/scan.go
new file mode 100644
index 0000000..dd3d266
--- /dev/null
+++ b/pkg/index/scan.go
@@ -0,0 +1,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
+}