diff --git a/config/fs.go b/config/fs.go index 6a98ff3..1657018 100644 --- a/config/fs.go +++ b/config/fs.go @@ -1,13 +1,16 @@ package config import ( - "github.com/spf13/afero" + "os" "path/filepath" + "slices" + + "github.com/spf13/afero" ) var ( - contentExts = []string{"md"} - listExts = []string{"yaml"} + contentExts = []string{".md"} + listExts = []string{".yaml", ".yml"} ) type Fs struct { @@ -51,7 +54,7 @@ func (f *Fs) findFileWithExtension(paths, exts []string) string { return p } for _, e := range exts { - if pe := p + "." + e; f.IsFile(pe) { + if pe := p + e; f.IsFile(pe) { return pe } } @@ -59,6 +62,34 @@ func (f *Fs) findFileWithExtension(paths, exts []string) string { return "" } +func (fs *Fs) WalkContent(walkFn func(path, key string, fi os.FileInfo, err error)) error { + return fs.walkFilesByExts(fs.Config.ContentDir, contentExts, walkFn) +} + +func (fs *Fs) WalkLists(walkFn func(path, key string, fi os.FileInfo, err error)) error { + return fs.walkFilesByExts(fs.Config.ListDir, listExts, walkFn) +} + +// Iteration helper to find all files with multiple possible extensions in a directory +func (fs *Fs) walkFilesByExts(dir string, exts []string, walkFn func(path, key string, fi os.FileInfo, err error)) error { + return afero.Walk(fs, dir, func(path string, fi os.FileInfo, err error) error { + if err != nil || fi.IsDir() { + return nil + } + + pathExt := filepath.Ext(path) + if !slices.Contains(exts, pathExt) { + return nil + } + + // Remove dir prefix and extension + key, _ := filepath.Rel(dir, path) + key = key[:len(key)-len(pathExt)] + walkFn(path, key, fi, nil) + return nil + }) +} + func (f *Fs) IsFile(path string) bool { s, err := f.Stat(path) return err == nil && !s.IsDir() diff --git a/server/listings.go b/server/listings.go index 6a4038c..1def663 100644 --- a/server/listings.go +++ b/server/listings.go @@ -1,12 +1,8 @@ package server import ( - "github.com/rykov/paperboy/config" - "github.com/spf13/afero" - "context" "os" - "path/filepath" ) // ===== Campaigns listing resolver ====== @@ -19,7 +15,7 @@ func (r *Resolver) Campaigns(ctx context.Context) ([]*Campaign, error) { param: key, }) } - err := walkFilesByExt(r.cfg.AppFs, r.cfg.ContentDir, ".md", walkFn) + err := r.cfg.AppFs.WalkContent(walkFn) return campaigns, err } @@ -46,7 +42,7 @@ func (r *Resolver) Lists(ctx context.Context) ([]*List, error) { param: key, }) } - err := walkFilesByExt(r.cfg.AppFs, r.cfg.ListDir, ".yaml", walkFn) + err := r.cfg.AppFs.WalkLists(walkFn) return lists, err } @@ -62,23 +58,3 @@ func (c *List) Param() string { func (c *List) Name() string { return c.name } - -// Iteration helper to find all files with a certain extension in a directory -func walkFilesByExt(fs *config.Fs, dir, ext string, walkFn func(path, key string, fi os.FileInfo, err error)) error { - return afero.Walk(fs, dir, func(path string, fi os.FileInfo, err error) error { - if err != nil || fi.IsDir() { - return nil - } - - pathExt := filepath.Ext(path) - if pathExt != ext { - return nil - } - - // Remove dir prefix and extension - key, _ := filepath.Rel(dir, path) - key = key[:len(key)-len(pathExt)] - walkFn(path, key, fi, nil) - return nil - }) -}