-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
64 lines (51 loc) · 1.02 KB
/
web.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
package web
import (
"embed"
"io"
"io/fs"
"os"
"path/filepath"
)
//go:embed static
var StaticFiles embed.FS
func CopyStaticFiles(srcFS embed.FS, srcDir, dstDir string) error {
err := fs.WalkDir(srcFS, srcDir, func(srcPath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
relPath, _ := filepath.Rel(srcDir, srcPath)
dstPath := filepath.Join(dstDir, relPath)
err = os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
if err != nil {
return err
}
if filepath.Base(srcPath) == "SUMMARY.md" {
return nil
}
if filepath.Ext(srcPath) == ".md" {
return nil
}
srcFile, err := srcFS.Open(srcPath)
if err != nil {
return err
}
defer srcFile.Close()
return copyFile(srcFile, dstPath)
})
return err
}
func copyFile(srcFile fs.File, dstPath string) error {
dstFile, err := os.Create(dstPath)
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
return dstFile.Sync()
}