1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 09:57:01 +00:00

minor (see prev commit)

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-09-26 00:34:18 +03:00
parent 512ed6ffc0
commit 68daa8d51d
10 changed files with 133 additions and 69 deletions

View File

@@ -152,6 +152,8 @@ func (s *AmberEngine) AddFunc(funcName string, funcBody interface{}) {
//
// Returns an error if something bad happens, user is responsible to catch it.
func (s *AmberEngine) Load() error {
rootDirName := getRootDirName(s.fs)
return walk(s.fs, "", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -167,6 +169,11 @@ func (s *AmberEngine) Load() error {
}
}
if s.rootDir == rootDirName {
path = strings.TrimPrefix(path, rootDirName)
path = strings.TrimPrefix(path, "/")
}
contents, err := asset(s.fs, path)
if err != nil {
return fmt.Errorf("%s: %w", path, err)

View File

@@ -54,7 +54,7 @@ func (s *BlocksEngine) Name() string {
}
// RootDir sets the directory to use as the root one inside the provided File System.
func (s *BlocksEngine) RootDir(root string) *BlocksEngine { // TODO: update blocks for the new fs.FS interface and use it for Sub.
func (s *BlocksEngine) RootDir(root string) *BlocksEngine {
s.Engine.RootDir(root)
return s
}

View File

@@ -222,6 +222,8 @@ func (s *DjangoEngine) RegisterTag(tagName string, fn TagParser) error {
//
// Returns an error if something bad happens, user is responsible to catch it.
func (s *DjangoEngine) Load() error {
rootDirName := getRootDirName(s.fs)
return walk(s.fs, "", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -237,6 +239,11 @@ func (s *DjangoEngine) Load() error {
}
}
if s.rootDir == rootDirName {
path = strings.TrimPrefix(path, rootDirName)
path = strings.TrimPrefix(path, "/")
}
contents, err := asset(s.fs, path)
if err != nil {
return err

View File

@@ -15,7 +15,6 @@ func walk(fileSystem fs.FS, root string, walkFn filepath.WalkFunc) error {
if err != nil {
return err
}
fileSystem = sub
}
@@ -25,13 +24,13 @@ func walk(fileSystem fs.FS, root string, walkFn filepath.WalkFunc) error {
return fs.WalkDir(fileSystem, root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("%s: %w", path, err)
return fmt.Errorf("walk: %s: %w", path, err)
}
info, err := d.Info()
if err != nil {
if err != filepath.SkipDir {
return fmt.Errorf("%s: %w", path, err)
return fmt.Errorf("walk stat: %s: %w", path, err)
}
return nil
@@ -41,15 +40,37 @@ func walk(fileSystem fs.FS, root string, walkFn filepath.WalkFunc) error {
return nil
}
return walkFn(path, info, err)
walkFnErr := walkFn(path, info, err)
if walkFnErr != nil {
return fmt.Errorf("walk: walkFn: %w", walkFnErr)
}
return nil
})
}
func asset(fileSystem fs.FS, name string) ([]byte, error) {
return fs.ReadFile(fileSystem, name)
data, err := fs.ReadFile(fileSystem, name)
if err != nil {
return nil, fmt.Errorf("asset: read file: %w", err)
}
return data, nil
}
func getFS(fsOrDir interface{}) fs.FS {
return context.ResolveFS(fsOrDir)
}
func getRootDirName(fileSystem fs.FS) string {
rootDirFile, err := fileSystem.Open(".")
if err == nil {
rootDirStat, err := rootDirFile.Stat()
if err == nil {
return rootDirStat.Name()
}
}
return ""
}

View File

@@ -134,6 +134,8 @@ func (s *HandlebarsEngine) AddGlobalFunc(funcName string, funcBody interface{})
//
// Returns an error if something bad happens, user is responsible to catch it.
func (s *HandlebarsEngine) Load() error {
rootDirName := getRootDirName(s.fs)
return walk(s.fs, "", func(path string, info os.FileInfo, _ error) error {
if info == nil || info.IsDir() {
return nil
@@ -145,6 +147,11 @@ func (s *HandlebarsEngine) Load() error {
}
}
if s.rootDir == rootDirName {
path = strings.TrimPrefix(path, rootDirName)
path = strings.TrimPrefix(path, "/")
}
contents, err := asset(s.fs, path)
if err != nil {
return err

View File

@@ -109,7 +109,6 @@ func (s *HTMLEngine) RootDir(root string) *HTMLEngine {
if err != nil {
panic(err)
}
s.fs = sub // here so the "middleware" can work.
}
@@ -255,6 +254,8 @@ func (s *HTMLEngine) load() error {
return err
}
rootDirName := getRootDirName(s.fs)
err := walk(s.fs, "", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -270,6 +271,11 @@ func (s *HTMLEngine) load() error {
}
}
if s.rootDir == rootDirName {
path = strings.TrimPrefix(path, rootDirName)
path = strings.TrimPrefix(path, "/")
}
buf, err := asset(s.fs, path)
if err != nil {
return fmt.Errorf("%s: %w", path, err)

View File

@@ -226,6 +226,8 @@ func (l *jetLoader) Exists(name string) bool {
// Load should load the templates from a physical system directory or by an embedded one (assets/go-bindata).
func (s *JetEngine) Load() error {
rootDirName := getRootDirName(s.fs)
return walk(s.fs, "", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -241,6 +243,11 @@ func (s *JetEngine) Load() error {
}
}
if s.rootDir == rootDirName {
path = strings.TrimPrefix(path, rootDirName)
path = strings.TrimPrefix(path, "/")
}
buf, err := asset(s.fs, path)
if err != nil {
return fmt.Errorf("%s: %w", path, err)