github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/pkg/embed/diskfs.go (about)

     1  package embed
     2  
     3  import (
     4  	"io/fs"
     5  	"os"
     6  	"path/filepath"
     7  )
     8  
     9  // DirFS is like http.Dir but implements fs.ReadDirFS
    10  type DirFS string
    11  
    12  var _ fs.ReadDirFS = (*DirFS)(nil)
    13  
    14  func (root DirFS) Open(name string) (fs.File, error) {
    15  	return os.Open(join(root, name))
    16  }
    17  
    18  func (root DirFS) ReadDir(name string) ([]fs.DirEntry, error) {
    19  	return os.ReadDir(join(root, name))
    20  }
    21  
    22  func join(root DirFS, name string) string {
    23  	if root == "" {
    24  		root = "."
    25  	}
    26  
    27  	return filepath.Join(string(root), name)
    28  }