-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdebme.go
69 lines (59 loc) · 1.62 KB
/
debme.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package debme
import (
"embed"
"io"
"io/fs"
"os"
"path/filepath"
)
type embedFS = embed.FS
// Debme is an embed.FS compatible wrapper, providing Sub() functionality
type Debme struct {
basedir string
embedFS
}
// FS creates an embed.FS compatible struct, anchored to the given basedir.
func FS(fs embed.FS, basedir string) (Debme, error) {
result := Debme{embedFS: fs, basedir: basedir}
_, err := result.ReadDir(".")
if err != nil {
return Debme{}, err
}
return result, nil
}
func (d Debme) calculatePath(path string) string {
base := filepath.Join(d.basedir, path)
return filepath.ToSlash(base)
}
// Open opens the named file for reading and returns it as an fs.File.
func (d Debme) Open(name string) (fs.File, error) {
path := d.calculatePath(name)
return d.embedFS.Open(path)
}
// ReadDir reads and returns the entire named directory.
func (d Debme) ReadDir(name string) ([]fs.DirEntry, error) {
path := d.calculatePath(name)
return d.embedFS.ReadDir(path)
}
// ReadFile reads and returns the content of the named file.
func (d Debme) ReadFile(name string) ([]byte, error) {
path := d.calculatePath(name)
return d.embedFS.ReadFile(path)
}
// FS returns a new Debme anchored at the given subdirectory.
func (d Debme) FS(subDir string) (Debme, error) {
path := d.calculatePath(subDir)
return FS(d.embedFS, path)
}
func (d Debme) CopyFile(sourcePath string, target string, perm os.FileMode) error {
sourceFile, err := d.Open(sourcePath)
if err != nil {
return err
}
targetFile, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, perm)
if err != nil {
return err
}
_, err = io.Copy(targetFile, sourceFile)
return err
}