-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpure.go
46 lines (37 loc) · 1013 Bytes
/
pure.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
package forge
import (
"context"
xos "github.com/frantjc/x/os"
)
type Pure struct {
Image string
Entrypoint []string
Cmd []string
Env []string
}
func (o *Pure) Run(ctx context.Context, containerRuntime ContainerRuntime, opts ...RunOpt) error {
opt := runOptsWithDefaults(opts...)
image, err := containerRuntime.PullImage(ctx, o.Image)
if err != nil {
return err
}
containerConfig := &ContainerConfig{
Entrypoint: o.Entrypoint,
Cmd: o.Cmd,
Env: o.Env,
WorkingDir: opt.WorkingDir,
Mounts: opt.Mounts,
}
container, err := createSleepingContainer(ctx, containerRuntime, image, containerConfig, opt)
if err != nil {
return err
}
defer container.Stop(ctx) //nolint:errcheck
defer container.Remove(ctx) //nolint:errcheck
if exitCode, err := container.Exec(ctx, containerConfig, opt.Streams); err != nil {
return err
} else if exitCode > 0 {
return xos.NewExitCodeError(ErrContainerExitedWithNonzeroExitCode, exitCode)
}
return nil
}