-
Notifications
You must be signed in to change notification settings - Fork 3
/
embed.go
59 lines (50 loc) · 1.05 KB
/
embed.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"
"io/fs"
"log"
"os"
)
const (
staticsPath = "statics"
templatesPath = "templates"
presetsPath = "presets"
)
var (
//go:embed statics/*
staticsEmbed embed.FS
statics fs.FS
//go:embed templates/*
templatesEmbed embed.FS
templates fs.FS
//go:embed presets/*
presetsEmbed embed.FS
presets fs.FS
)
func mustStripFSPrefix(sfs fs.FS, prefix string) fs.FS {
dfs, err := fs.Sub(sfs, prefix)
if err != nil {
panic(err)
}
return dfs
}
func prepareFS(debug bool) {
statics = mustStripFSPrefix(staticsEmbed, staticsPath)
templates = mustStripFSPrefix(templatesEmbed, templatesPath)
presets = mustStripFSPrefix(presetsEmbed, presetsPath)
if debug {
useLiveReload(&statics, staticsPath)
useLiveReload(&templates, templatesPath)
useLiveReload(&presets, presetsPath)
}
}
func useLiveReload(target *fs.FS, path string) {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
// Cannot live reload
return
}
}
log.Printf("live reload ./%v/*", path)
*target = os.DirFS(path)
}