Skip to content

Commit 2fe3ddd

Browse files
authored
Merge pull request #39 from gbonnefille/support-yml
Support yml extension
2 parents d2f0f9d + 21dee61 commit 2fe3ddd

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

Diff for: config/fs.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package config
22

33
import (
4-
"github.com/spf13/afero"
4+
"os"
55
"path/filepath"
6+
"slices"
7+
8+
"github.com/spf13/afero"
69
)
710

811
var (
9-
contentExts = []string{"md"}
10-
listExts = []string{"yaml"}
12+
contentExts = []string{".md"}
13+
listExts = []string{".yaml", ".yml"}
1114
)
1215

1316
type Fs struct {
@@ -51,14 +54,42 @@ func (f *Fs) findFileWithExtension(paths, exts []string) string {
5154
return p
5255
}
5356
for _, e := range exts {
54-
if pe := p + "." + e; f.IsFile(pe) {
57+
if pe := p + e; f.IsFile(pe) {
5558
return pe
5659
}
5760
}
5861
}
5962
return ""
6063
}
6164

65+
func (fs *Fs) WalkContent(walkFn func(path, key string, fi os.FileInfo, err error)) error {
66+
return fs.walkFilesByExts(fs.Config.ContentDir, contentExts, walkFn)
67+
}
68+
69+
func (fs *Fs) WalkLists(walkFn func(path, key string, fi os.FileInfo, err error)) error {
70+
return fs.walkFilesByExts(fs.Config.ListDir, listExts, walkFn)
71+
}
72+
73+
// Iteration helper to find all files with multiple possible extensions in a directory
74+
func (fs *Fs) walkFilesByExts(dir string, exts []string, walkFn func(path, key string, fi os.FileInfo, err error)) error {
75+
return afero.Walk(fs, dir, func(path string, fi os.FileInfo, err error) error {
76+
if err != nil || fi.IsDir() {
77+
return nil
78+
}
79+
80+
pathExt := filepath.Ext(path)
81+
if !slices.Contains(exts, pathExt) {
82+
return nil
83+
}
84+
85+
// Remove dir prefix and extension
86+
key, _ := filepath.Rel(dir, path)
87+
key = key[:len(key)-len(pathExt)]
88+
walkFn(path, key, fi, nil)
89+
return nil
90+
})
91+
}
92+
6293
func (f *Fs) IsFile(path string) bool {
6394
s, err := f.Stat(path)
6495
return err == nil && !s.IsDir()

Diff for: server/listings.go

+2-26
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package server
22

33
import (
4-
"github.com/rykov/paperboy/config"
5-
"github.com/spf13/afero"
6-
74
"context"
85
"os"
9-
"path/filepath"
106
)
117

128
// ===== Campaigns listing resolver ======
@@ -19,7 +15,7 @@ func (r *Resolver) Campaigns(ctx context.Context) ([]*Campaign, error) {
1915
param: key,
2016
})
2117
}
22-
err := walkFilesByExt(r.cfg.AppFs, r.cfg.ContentDir, ".md", walkFn)
18+
err := r.cfg.AppFs.WalkContent(walkFn)
2319
return campaigns, err
2420
}
2521

@@ -46,7 +42,7 @@ func (r *Resolver) Lists(ctx context.Context) ([]*List, error) {
4642
param: key,
4743
})
4844
}
49-
err := walkFilesByExt(r.cfg.AppFs, r.cfg.ListDir, ".yaml", walkFn)
45+
err := r.cfg.AppFs.WalkLists(walkFn)
5046
return lists, err
5147
}
5248

@@ -62,23 +58,3 @@ func (c *List) Param() string {
6258
func (c *List) Name() string {
6359
return c.name
6460
}
65-
66-
// Iteration helper to find all files with a certain extension in a directory
67-
func walkFilesByExt(fs *config.Fs, dir, ext string, walkFn func(path, key string, fi os.FileInfo, err error)) error {
68-
return afero.Walk(fs, dir, func(path string, fi os.FileInfo, err error) error {
69-
if err != nil || fi.IsDir() {
70-
return nil
71-
}
72-
73-
pathExt := filepath.Ext(path)
74-
if pathExt != ext {
75-
return nil
76-
}
77-
78-
// Remove dir prefix and extension
79-
key, _ := filepath.Rel(dir, path)
80-
key = key[:len(key)-len(pathExt)]
81-
walkFn(path, key, fi, nil)
82-
return nil
83-
})
84-
}

0 commit comments

Comments
 (0)