-
Notifications
You must be signed in to change notification settings - Fork 0
/
makesite.go
75 lines (61 loc) · 1.44 KB
/
makesite.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
72
73
74
75
package main
import (
"embed"
_ "embed"
"fmt"
"io/fs"
"os"
"path/filepath"
)
//go:embed theme
var defaultTheme embed.FS
func makeSite(path string, meta SiteMetadata) error {
// Create web root dir
err := os.MkdirAll(filepath.Join(path, S4gDir), 0755)
if err != nil {
return fmt.Errorf("make site: %w", err)
}
// Write site metadata file
data := MarshalMetadata(&meta)
err = os.WriteFile(filepath.Join(path, SettingsPath), data, 0664)
if err != nil {
return fmt.Errorf("write site metadata: %w", err)
}
// Copy default theme into new site
copyTheme(defaultTheme, filepath.Dir(path+"/"+ThemePath))
// Write default index page
indexData := []byte(`Title: Home
ShowInFeed: false
PageType: home
---
`)
err = os.WriteFile(filepath.Join(path, "index.dj"), indexData, 0664)
if err != nil {
panic(err)
}
// Write empty redirects file
err = os.WriteFile(filepath.Join(path, RedirectsPath), []byte{}, 0664)
if err != nil {
panic(err)
}
return nil
}
func copyTheme(src fs.FS, dst string) error {
fs.WalkDir(src, ".", func(path string, d fs.DirEntry, err error) error {
dstPath := filepath.Join(dst, path)
if d.IsDir() {
os.MkdirAll(dstPath, 0755)
return nil
}
content, err := fs.ReadFile(src, path)
if err != nil {
return fmt.Errorf("read source file: %w", err)
}
err = os.WriteFile(dstPath, content, 0644)
if err != nil {
return fmt.Errorf("write dest file: %w", err)
}
return nil
})
return nil
}