-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
71 lines (62 loc) · 1.52 KB
/
fs.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
70
71
package nginx
import (
"embed"
"io"
"io/fs"
"os"
"path/filepath"
)
//go:embed conf/*
var content embed.FS
// fsCopyTo copies the content of the embedded filesystem to the destination
func fsCopyTo(dst string) error {
// Open the source directory
src, err := content.ReadDir("conf")
if err != nil {
return err
}
return fsCopyDir(dst, "conf", src)
}
// fsCopyDir copies the content of a directory to the destination
func fsCopyDir(dst string, srcPath string, entries []fs.DirEntry) error {
for _, entry := range entries {
srcEntryPath := filepath.Join(srcPath, entry.Name())
dstEntryPath := filepath.Join(dst, entry.Name())
if entry.IsDir() {
// Create the destination directory
if err := os.MkdirAll(dstEntryPath, os.ModePerm); err != nil {
return err
}
// Read the directory contents
subEntries, err := content.ReadDir(srcEntryPath)
if err != nil {
return err
}
// Recursively copy the directory
if err := fsCopyDir(dstEntryPath, srcEntryPath, subEntries); err != nil {
return err
}
} else if err := fsCopyFile(dstEntryPath, srcEntryPath); err != nil {
return err
}
}
return nil
}
// fsCopyFile copies a file from the embedded filesystem to the destination
func fsCopyFile(dstFile string, srcFile string) error {
src, err := content.Open(srcFile)
if err != nil {
return err
}
defer src.Close()
dst, err := os.Create(dstFile)
if err != nil {
return err
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
return err
}
// Return success
return nil
}