forked from dagger/agents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (45 loc) · 1.17 KB
/
main.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
// A toy workspace for editing and building go programs
package main
import (
"context"
"dagger/toy-workspace/internal/dagger"
)
func New() ToyWorkspace {
return ToyWorkspace{
Container: dag.Container().
From("golang").
WithMountedCache("/go/pkg/mod", dag.CacheVolume("go_mod_cache")).
WithWorkdir("/app"),
}
}
type ToyWorkspace struct {
// The workspace container.
// +internal-use-only
Container *dagger.Container
}
// Read a file
func (w *ToyWorkspace) Read(
ctx context.Context,
// The path of the file
path string) (string, error) {
return w.Container.File(path).Contents(ctx)
}
// Write a file
func (w *ToyWorkspace) Write(
// The path of the file
path string,
// The content to write
content string,
) *ToyWorkspace {
w.Container = w.Container.WithNewFile(path, content)
return w
}
// Build the code at the current directory in the workspace
func (w *ToyWorkspace) Build(ctx context.Context) (string, error) {
// We just execute "go build" in the container,
// and discard the changes
buildCommand := []string{"go", "build", "./..."}
return w.Container.
WithExec(buildCommand, dagger.ContainerWithExecOpts{Expect: dagger.ReturnTypeAny}).
Stderr(ctx)
}