-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
59 lines (53 loc) · 1.35 KB
/
example.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
package main
import (
"embed"
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
)
func generateExampleContent(rootDir, templatesDir string) error {
if rootDir == templatesDir {
return errors.New("source and templates directories cannot be the same")
}
fn := func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
b, err := exampleContent.ReadFile(p)
if err != nil {
return err
}
dst := filepath.Join(rootDir, filepath.FromSlash(strings.TrimPrefix(p, examplesRoot)))
return writeIfNotExists(dst, b)
}
if err := fs.WalkDir(exampleContent, ".", fn); err != nil {
return err
}
return writeIfNotExists(filepath.Join(templatesDir, "default.html"), exampleTemplate)
}
//go:embed example-content
var exampleContent embed.FS
const examplesRoot = "example-content"
//go:embed example-template.html
var exampleTemplate []byte
// writeIfNotExists creates file at dst and writes b as its content. It fails
// if file already exists. Parent directories created as necessary.
func writeIfNotExists(dst string, b []byte) error {
if err := os.MkdirAll(filepath.Dir(dst), 0777); err != nil {
return err
}
f, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0666)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Write(b); err != nil {
return err
}
return f.Close()
}