Skip to content

Commit

Permalink
Merge pull request #39 from gbonnefille/support-yml
Browse files Browse the repository at this point in the history
Support yml extension
  • Loading branch information
rykov authored Nov 14, 2024
2 parents d2f0f9d + 21dee61 commit 2fe3ddd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
39 changes: 35 additions & 4 deletions config/fs.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -51,14 +54,42 @@ 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
}
}
}
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()
Expand Down
28 changes: 2 additions & 26 deletions server/listings.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package server

import (
"github.com/rykov/paperboy/config"
"github.com/spf13/afero"

"context"
"os"
"path/filepath"
)

// ===== Campaigns listing resolver ======
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
})
}

0 comments on commit 2fe3ddd

Please sign in to comment.