-
Notifications
You must be signed in to change notification settings - Fork 5
/
newsite.go
62 lines (54 loc) · 1.12 KB
/
newsite.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
package main
import (
"io"
"io.github.nollgo/assets"
"io/fs"
"os"
"path/filepath"
)
const themePath = "theme"
func newSite(path string) error {
if path == "" {
path = "."
}
dir, err := assets.Dir.ReadDir(themePath)
if err != nil {
return err
}
// create dir
_ = os.MkdirAll(path, 0755)
if err = write(path, themePath, dir); err != nil {
return err
}
return nil
}
func write(path string, parent string, entries []fs.DirEntry) error {
for _, entry := range entries {
entryPath := filepath.Join(path, entry.Name())
if entry.IsDir() {
if err := os.Mkdir(entryPath, 0755); err != nil {
return err
}
dir, err := assets.Dir.ReadDir(filepath.Join(parent, entry.Name()))
if err != nil {
return err
}
if err = write(entryPath, filepath.Join(parent, entry.Name()), dir); err != nil {
return err
}
} else {
f, err := assets.Dir.Open(filepath.Join(parent, entry.Name()))
if err != nil {
return err
}
bytes, err := io.ReadAll(f)
if err != nil {
return err
}
if err = os.WriteFile(entryPath, bytes, 0644); err != nil {
return err
}
}
}
return nil
}