-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
92 lines (77 loc) · 1.8 KB
/
fs.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package util_packaged
import (
"embed"
"fmt"
"github.com/gigurra/flycd/pkg/util/util_work_dir"
"io"
"io/fs"
"os"
"path/filepath"
)
type PackagedFile struct {
Name string
Contents string
}
type PackagedFileSystem struct {
Files []PackagedFile
Directories []embed.FS
}
func (embedded PackagedFileSystem) WriteOut(path string) error {
if path == "" {
return fmt.Errorf("PackagedFileSystem.WriteOut: path cannot be empty")
}
workDir := util_work_dir.NewWorkDir(path)
fmt.Printf("Writing embedded fs to: %s\n", workDir.Cwd())
// Write out all files to tempDir
for _, file := range embedded.Files {
err := workDir.WriteFile(file.Name, file.Contents)
if err != nil {
return fmt.Errorf("failed to write file %s: %w", file.Name, err)
}
}
// Write out all directories to tempDir
for _, dir := range embedded.Directories {
err := writeFS(workDir.Cwd(), dir)
if err != nil {
return fmt.Errorf("failed to create directory %v: %w", dir, err)
}
}
return nil
}
func writeFS(outDir string, fsys fs.FS) error {
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
outPath := filepath.Join(outDir, path)
if d.IsDir() {
if err := os.MkdirAll(outPath, 0755); err != nil {
return err
}
} else {
outFile, err := os.Create(outPath)
if err != nil {
return err
}
defer func(outFile *os.File) {
err := outFile.Close()
if err != nil {
fmt.Printf("Error closing file: %v\n", err)
}
}(outFile)
inFile, err := fsys.Open(path)
if err != nil {
return err
}
defer func(inFile fs.File) {
err := inFile.Close()
if err != nil {
fmt.Printf("Error closing file: %v\n", err)
}
}(inFile)
_, err = io.Copy(outFile, inFile)
return err
}
return nil
})
}