🗃️ Search site files from root of FS

This commit is contained in:
Maxim Lebedev 2023-11-08 04:54:24 +06:00
parent 30ddd71e40
commit 2ffe9277be
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
2 changed files with 11 additions and 14 deletions

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io/fs"
"path/filepath"
"github.com/adrg/frontmatter"
"golang.org/x/text/language"
@ -22,7 +21,7 @@ type (
}
fileSystemSiteRepository struct {
fs fs.FS
dir fs.FS
rootPath string
}
)
@ -31,28 +30,27 @@ var FrontMatterFormats = []*frontmatter.Format{
frontmatter.NewFormat(`---`, `---`, yaml.Unmarshal),
}
func NewFileSystemSiteRepository(fs fs.FS, rootPath string) site.Repository {
func NewFileSystemSiteRepository(rootDir fs.FS) site.Repository {
return &fileSystemSiteRepository{
fs: fs,
rootPath: rootPath,
dir: rootDir,
}
}
func (repo *fileSystemSiteRepository) Get(ctx context.Context, lang language.Tag) (*domain.Site, error) {
targetPath := filepath.Join(repo.rootPath, "index."+lang.String()+".md")
target := "index." + lang.String() + ".md"
if lang == language.Und {
targetPath = filepath.Join(repo.rootPath, "index.md")
target = "index.md"
}
f, err := repo.fs.Open(targetPath)
f, err := repo.dir.Open(target)
if err != nil {
return nil, fmt.Errorf("cannot open site file for '%s' language: %w", lang, err)
return nil, fmt.Errorf("cannot open '%s' site file: %w", target, err)
}
defer f.Close()
data := new(Site)
if data.Content, err = frontmatter.Parse(f, data, FrontMatterFormats...); err != nil {
return nil, fmt.Errorf("cannot parse file content as FrontMatter: %w", err)
return nil, fmt.Errorf("cannot parse site content as FrontMatter: %w", err)
}
return &domain.Site{

View File

@ -2,7 +2,6 @@ package fs_test
import (
"context"
"path/filepath"
"testing"
"testing/fstest"
@ -17,15 +16,15 @@ func TestGet(t *testing.T) {
t.Parallel()
testData := fstest.MapFS{
filepath.Join("content", "index.ru.md"): &fstest.MapFile{
"index.ru.md": &fstest.MapFile{
Data: []byte("---\ntitle: пример\n---\nпривет, мир!\n"),
},
filepath.Join("content", "index.en.md"): &fstest.MapFile{
"index.en.md": &fstest.MapFile{
Data: []byte("---\ntitle: example\n---\nhello, world!\n"),
},
}
repo := repository.NewFileSystemSiteRepository(testData, "content")
repo := repository.NewFileSystemSiteRepository(testData)
for name, tc := range map[string]struct {
input language.Tag