2020-09-05 07:34:09 +02:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-25 19:40:56 +02:00
|
|
|
"io/fs"
|
2020-09-05 07:34:09 +02:00
|
|
|
"path/filepath"
|
2022-09-21 22:26:12 +02:00
|
|
|
|
|
|
|
"github.com/kataras/iris/v12/context"
|
2020-09-05 07:34:09 +02:00
|
|
|
)
|
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
// walk recursively in "fileSystem" descends "root" path, calling "walkFn".
|
|
|
|
func walk(fileSystem fs.FS, root string, walkFn filepath.WalkFunc) error {
|
|
|
|
if root != "" && root != "/" && root != "." {
|
|
|
|
sub, err := fs.Sub(fileSystem, root)
|
2020-09-05 07:34:09 +02:00
|
|
|
if err != nil {
|
2022-09-25 19:40:56 +02:00
|
|
|
return err
|
2020-09-05 07:34:09 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
fileSystem = sub
|
2020-09-05 07:34:09 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
if root == "" {
|
|
|
|
root = "."
|
2020-09-08 00:58:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
return fs.WalkDir(fileSystem, root, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
|
|
}
|
2020-09-05 07:34:09 +02:00
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
info, err := d.Info()
|
|
|
|
if err != nil {
|
|
|
|
if err != filepath.SkipDir {
|
|
|
|
return fmt.Errorf("%s: %w", path, err)
|
|
|
|
}
|
2020-09-08 00:58:02 +02:00
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
return nil
|
2020-09-08 00:58:02 +02:00
|
|
|
}
|
2020-09-05 07:34:09 +02:00
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-08 00:58:02 +02:00
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
return walkFn(path, info, err)
|
|
|
|
})
|
2020-09-08 00:58:02 +02:00
|
|
|
|
2020-09-09 13:43:26 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
func asset(fileSystem fs.FS, name string) ([]byte, error) {
|
|
|
|
return fs.ReadFile(fileSystem, name)
|
2020-09-05 07:34:09 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 19:40:56 +02:00
|
|
|
func getFS(fsOrDir interface{}) fs.FS {
|
|
|
|
return context.ResolveFS(fsOrDir)
|
2020-09-05 07:34:09 +02:00
|
|
|
}
|