-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
48 lines (42 loc) · 943 Bytes
/
publisher.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
package publisher
import (
"embed"
"os"
"strings"
)
// CopyTo publish your `//go:embed` file or directory to the specified dst directory
func CopyTo(dstDir string, fs embed.FS) error {
fsDirPath := "."
return copyTo(dstDir, fs, fsDirPath)
}
func copyTo(dstDir string, fs embed.FS, fsDirPath string) error {
fsDirPath = strings.Replace(fsDirPath, "./", "", -1)
if _, err := os.Stat(dstDir); os.IsNotExist(err) {
err := os.Mkdir(dstDir, os.ModePerm)
if err != nil {
return err
}
}
fsDir, err := fs.ReadDir(fsDirPath)
if err != nil {
return err
}
for _, f := range fsDir {
if f.IsDir() {
err := copyTo(dstDir+"/"+f.Name(), fs, fsDirPath+"/"+f.Name())
if err != nil {
return err
}
} else {
b, err := fs.ReadFile(fsDirPath + "/" + f.Name())
if err != nil {
return err
}
err = os.WriteFile(dstDir+"/"+f.Name(), b, os.ModePerm)
if err != nil {
return err
}
}
}
return nil
}