🗃️ Refactored Fetch method of static repository
This commit is contained in:
parent
2215cc751b
commit
49824ed100
|
@ -46,28 +46,40 @@ func (repo *fileServerStaticRepository) Get(ctx context.Context, p string) (*dom
|
|||
return domain.NewResource(info.ModTime(), content, p), nil
|
||||
}
|
||||
|
||||
func (repo *fileServerStaticRepository) Fetch(ctx context.Context, dir string) (domain.Resources, int, error) {
|
||||
targets := make([]string, 0)
|
||||
if err := fs.WalkDir(repo.root, dir, func(path string, de fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("received error while walking through '%s': %w", dir, err)
|
||||
}
|
||||
func (repo *fileServerStaticRepository) Fetch(ctx context.Context, pattern string) (domain.Resources, int, error) {
|
||||
var (
|
||||
err error
|
||||
matches []string
|
||||
)
|
||||
|
||||
if pattern != "" {
|
||||
if matches, err = fs.Glob(repo.root, pattern); err != nil {
|
||||
return nil, 0, fmt.Errorf("cannot match any static by pattern '%s': %w", pattern, err)
|
||||
}
|
||||
} else {
|
||||
if err = fs.WalkDir(repo.root, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("catched error while walk: %w", err)
|
||||
}
|
||||
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
matches = append(matches, path)
|
||||
|
||||
if de.IsDir() {
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, 0, fmt.Errorf("cannot walk through static directories: %w", err)
|
||||
}
|
||||
|
||||
targets = append(targets, path)
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, 0, fmt.Errorf("cannot read directory on path '%s': %w", dir, err)
|
||||
}
|
||||
|
||||
out := make(domain.Resources, len(targets))
|
||||
out := make(domain.Resources, 0, len(matches))
|
||||
|
||||
for i := range targets {
|
||||
out[i], _ = repo.Get(ctx, targets[i])
|
||||
for i := range matches {
|
||||
if r, err := repo.Get(ctx, matches[i]); err == nil {
|
||||
out = append(out, r)
|
||||
}
|
||||
}
|
||||
|
||||
return out, len(out), nil
|
||||
|
|
Loading…
Reference in New Issue