MCPcopy Create free account
hub / github.com/efficientgo/e2e / Containerize

Function Containerize

local.go:20–92  ·  view source on GitHub ↗

Containerize inspects startFn and builds Go shim with local process endpoint that imports given `startFn` function. Binary is then put in adhoc container and returned as runnable ready to be started.

(e Environment, name string, startFn func(context.Context) error)

Source from the content-addressed store, hash-verified

18// Containerize inspects startFn and builds Go shim with local process endpoint that imports given `startFn` function.
19// Binary is then put in adhoc container and returned as runnable ready to be started.
20func Containerize(e Environment, name string, startFn func(context.Context) error) (Runnable, error) {
21 de, ok := e.(*DockerEnvironment)
22 if !ok {
23 return nil, errors.New("not implemented")
24 }
25
26 // Not portable, but good enough for local unit tests.
27 wd, err := os.Getwd()
28 if err != nil {
29 return nil, err
30 }
31
32 strs := strings.Split(runtime.FuncForPC(reflect.ValueOf(startFn).Pointer()).Name(), ".")
33 funcName := strs[len(strs)-1]
34 pkg := strings.Join(strs[:len(strs)-1], ".")
35
36 modulePath := pkg
37 absModulePath := wd
38 for len(absModulePath) > 0 {
39 _, err := os.Stat(filepath.Join(absModulePath, "go.mod"))
40 if os.IsNotExist(err) {
41 absModulePath = filepath.Dir(absModulePath)
42 modulePath = filepath.Dir(modulePath)
43 continue
44 }
45 if err == nil {
46 break
47 }
48 return nil, err
49 }
50
51 if len(absModulePath) == 0 {
52 return nil, errors.Newf("not a Go module %v", wd)
53 }
54
55 f := e.Runnable(name).WithPorts(map[string]int{"http": 80}).Future()
56 dir := filepath.Join(f.Dir(), "shim")
57
58 if err := os.MkdirAll(dir, os.ModePerm); err != nil {
59 return nil, err
60 }
61
62 // TODO(saswatamcode): Maybe we can do away with goModTmpl, and just run go mod init shim, go mod edit -replace %v=%v and go mod tidy here.
63 if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte(fmt.Sprintf(goModTmpl, modulePath, modulePath, absModulePath)), os.ModePerm); err != nil {
64 return nil, err
65 }
66 if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte(fmt.Sprintf(mainFileTmpl, pkg, funcName)), os.ModePerm); err != nil {
67 return nil, err
68 }
69 if err := os.WriteFile(filepath.Join(dir, "Dockerfile"), []byte(dockerFile), os.ModePerm); err != nil {
70 return nil, err
71 }
72
73 cmd := de.exec("go", "mod", "tidy")
74 cmd.Dir = dir
75 if out, err := cmd.CombinedOutput(); err != nil {
76 return nil, errors.Wrap(err, string(out))
77 }

Callers

nothing calls this directly

Calls 7

NameMethod · 0.65
DirMethod · 0.65
FutureMethod · 0.65
WithPortsMethod · 0.65
RunnableMethod · 0.65
InitMethod · 0.65
execMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…