-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfileystem.go
58 lines (49 loc) · 1.66 KB
/
fileystem.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
package builder
import (
"os"
"path/filepath"
"github.com/darklab8/fl-darkstat/darkstat/settings/logus"
"github.com/darklab8/go-typelog/typelog"
"github.com/darklab8/go-utils/goutils/utils/utils_filepath"
"github.com/darklab8/go-utils/goutils/utils/utils_types"
)
/*
Filesystem allows us to write to files to memory for later reusage in web app serving static assets from memory
Optionally same filesystem supports rendering to local, for deployment of static assets
*/
type Filesystem struct {
Files map[utils_types.FilePath][]byte
build_root utils_types.FilePath
}
func NewFileystem(build_root utils_types.FilePath) *Filesystem {
b := &Filesystem{
Files: make(map[utils_types.FilePath][]byte),
build_root: build_root,
}
return b
}
var PermReadWrite os.FileMode = 0666
func (f *Filesystem) WriteToMem(path utils_types.FilePath, content []byte) {
f.Files[path] = content
}
func (f *Filesystem) RenderToLocal() {
os.RemoveAll(f.build_root.ToString())
os.MkdirAll(f.build_root.ToString(), os.ModePerm)
for path, content := range f.Files {
final_path := utils_filepath.Join(f.build_root, path)
haveParentFoldersCreated(final_path)
// TODO add check for creating all missing folders in the path
err := os.WriteFile(final_path.ToString(), []byte(content), PermReadWrite)
logus.Log.CheckFatal(err, "failed to export bases to file")
}
}
func haveParentFoldersCreated(buildpath utils_types.FilePath) {
path := buildpath.ToString()
folder_path := filepath.Dir(path)
err := os.MkdirAll(folder_path, os.ModePerm)
logus.Log.CheckError(err,
"haveParentFoldersCreated finished",
typelog.String("folderpath", folder_path),
typelog.String("path", path),
)
}