Skip to content

Commit 8ea5f31

Browse files
committed
embed runtime files
1 parent 8c45fbc commit 8ea5f31

File tree

9 files changed

+112
-2
lines changed

9 files changed

+112
-2
lines changed

pkg/docker/handler.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7+
"embed"
78
"fmt"
89
"io"
910
"log"
@@ -23,6 +24,9 @@ import (
2324
"github.com/google/uuid"
2425
)
2526

27+
//go:embed runtimes
28+
var runtimes embed.FS
29+
2630
const (
2731
TmpDir = "./tmp"
2832
containerTimeout = 1
@@ -93,11 +97,13 @@ func (db *DockerBackend) Create(name string, env string, threads int, filedir st
9397
}
9498

9599
// copy Docker stuff into folder
96-
// cp ./runtimes/<env>/* <folder>
97-
err = util.CopyAll(path.Join("./runtimes", dh.env), dh.filePath)
100+
// cp runtimes/<env>/* <folder>
101+
102+
err = util.CopyDirFromEmbed(runtimes, path.Join("runtimes", dh.env), dh.filePath)
98103
if err != nil {
99104
return nil, err
100105
}
106+
101107
log.Println("copied runtime files to folder", dh.filePath)
102108

103109
// copy function into folder
File renamed without changes.
File renamed without changes.
File renamed without changes.

pkg/util/copyembed.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package util
2+
3+
import (
4+
"embed"
5+
"errors"
6+
"io"
7+
"io/fs"
8+
"log"
9+
"os"
10+
"path/filepath"
11+
)
12+
13+
func CopyFileFromEmbed(src embed.FS, srcPath string, dstPath string) (err error) {
14+
srcFile, err := src.Open(srcPath)
15+
if err != nil {
16+
return
17+
}
18+
defer srcFile.Close()
19+
20+
_, err = os.Stat(dstPath)
21+
if err == nil || !errors.Is(err, fs.ErrNotExist) {
22+
log.Printf("Destination file %s already exists, skipping", dstPath)
23+
return
24+
}
25+
26+
err = os.MkdirAll(filepath.Dir(dstPath), 0755)
27+
if err != nil {
28+
return
29+
}
30+
31+
dstFile, err := os.Create(dstPath)
32+
33+
if err != nil {
34+
return
35+
}
36+
37+
defer func() {
38+
if e := dstFile.Close(); e != nil {
39+
err = e
40+
}
41+
}()
42+
43+
_, err = io.Copy(dstFile, srcFile)
44+
45+
if err != nil {
46+
return
47+
}
48+
49+
err = dstFile.Sync()
50+
51+
if err != nil {
52+
return
53+
}
54+
55+
return
56+
57+
}
58+
59+
func CopyDirFromEmbed(src embed.FS, srcPath string, dstPath string) (err error) {
60+
entries, err := fs.ReadDir(src, srcPath)
61+
62+
if err != nil {
63+
return
64+
}
65+
66+
err = os.MkdirAll(dstPath, 0755)
67+
if err != nil {
68+
return
69+
}
70+
71+
for _, entry := range entries {
72+
srcPath := filepath.Join(srcPath, entry.Name())
73+
dstPath := filepath.Join(dstPath, entry.Name())
74+
75+
if entry.IsDir() {
76+
err = CopyDirFromEmbed(src, srcPath, dstPath)
77+
if err != nil {
78+
return
79+
}
80+
} else {
81+
// Skip symlinks.
82+
if entry.Type()&fs.ModeSymlink != 0 {
83+
continue
84+
}
85+
86+
err = CopyFileFromEmbed(src, srcPath, dstPath)
87+
if err != nil {
88+
return
89+
}
90+
}
91+
}
92+
93+
return
94+
95+
}
96+
97+
// func CopyAllFromEmbed(src embed.FS, dst string) (err error) {
98+
// err = CopyDirFromEmbed(src, ".", dst)
99+
// if err != nil {
100+
// return
101+
// }
102+
103+
// return
104+
// }

0 commit comments

Comments
 (0)