Skip to content

Commit

Permalink
added mount option
Browse files Browse the repository at this point in the history
  • Loading branch information
abecodes committed Nov 2, 2024
1 parent 77bd974 commit 4a8c463
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 42 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/abecodes/dft)](https://goreportcard.com/report/github.com/abecodes/dft)
[![Go Report Card](https://goreportcard.com/badge/github.com/abecodes/dft)](https://goreportcard.com/report/github.com/abecodes/dft)
[![Go Reference](https://pkg.go.dev/badge/github.com/abecodes/dft.svg)](https://pkg.go.dev/github.com/abecodes/dft)

# 🥼 DFT
Expand Down Expand Up @@ -134,6 +134,7 @@ func TestUserService(tt *testing.T) {
| --- | --- | --- |
| WithCmd | Overwrite [CMD]. | `WithCmd([]string{"--tlsCAFile", "/run/tls/ca.crt"})` |
| WithEnvVar | Set an envvar inside the container.<br>Can be called multiple times.<br>If two options use the same key the latest one will overwrite existing ones. | `WithEnvVar("intent", "prod")` |
| WithMount | Mount a local dir or file<br>Can be called multiple times. | `WithMount("./host/folder", "/target")` |
| WithPort | Expose an internal port on a specific host port. | `WithPort(27017,8080)` |
| WithRandomPort | Expose an internal port on a random host port.<br>Use `ExposedPorts` or `ExposedPortAddresses` to get the correct host port. | `WithRandomPort(27017)` |

Expand Down
54 changes: 49 additions & 5 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,52 @@ type Container struct {
func newContainer(
ctx context.Context,
imageName string,
exposedPorts [][2]uint,
envVars []string,
args []string,
opts ...ContainerOption,
) (*Container, error) {
id, err := startContainer(ctx, imageName, exposedPorts, envVars, args)
cfg := containerCfg{
args: nil,
env: nil,
mounts: nil,
ports: nil,
}

// INFO: we could pass the options further down and parse them in functions
// we are calling, but we need the exposed ports here to check if we are up
for i := range opts {
opts[i](&cfg)
}

var (
arguments []string
envVars []string
exposedPorts [][2]uint
mounts [][2]string
)

if cfg.args != nil {
arguments = *cfg.args
}

if cfg.env != nil {
envVars = *cfg.env
}

if cfg.mounts != nil {
mounts = *cfg.mounts
}

if cfg.ports != nil {
exposedPorts = *cfg.ports
}

id, err := startContainer(
ctx,
imageName,
arguments,
envVars,
exposedPorts,
mounts,
)
if err != nil {
return nil, fmt.Errorf(
"[%s](%s) %w",
Expand All @@ -43,7 +84,10 @@ func newContainer(
if err != nil {
ctr := Container{id: id}

sCtx, sCtxCancel := context.WithTimeout(context.Background(), 5*time.Second)
sCtx, sCtxCancel := context.WithTimeout(
context.Background(),
5*time.Second,
)
_ = ctr.Stop(sCtx)
sCtxCancel()
}
Expand Down
31 changes: 1 addition & 30 deletions dft.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,9 @@ func StartContainer(
return nil, err
}

cfg := containerCfg{
env: nil,
ports: nil,
}

for i := range opts {
opts[i](&cfg)
}

var (
args []string
envVars []string
exposedPorts [][2]uint
)

if cfg.args != nil {
args = *cfg.args
}

if cfg.env != nil {
envVars = *cfg.env
}

if cfg.ports != nil {
exposedPorts = *cfg.ports
}

return newContainer(
ctx,
imageName,
exposedPorts,
envVars,
args,
opts...,
)
}
6 changes: 5 additions & 1 deletion dft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func TestDFT(tt *testing.T) {
},
),
)
if !strings.Contains(err.Error(), "container in invalid state: 'exited'") {
if !strings.Contains(
err.Error(),
"container in invalid state: 'exited'",
) {
t.Errorf("[dft.StartContainer] unexpected error: %v", err)
tt.FailNow()

Expand All @@ -66,6 +69,7 @@ func TestDFT(tt *testing.T) {
ctr, err = dft.StartContainer(
ctx,
"mongo:7-jammy",
dft.WithMount("./testfile", "/etc/testfile"),
dft.WithRandomPort(27017),
dft.WithPort(27017, 27017),
)
Expand Down
18 changes: 16 additions & 2 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ const (
func startContainer(
ctx context.Context,
imageName string,
exposedPorts [][2]uint,
envVars []string,
arguments []string,
envVars []string,
exposedPorts [][2]uint,
mounts [][2]string,
) (string, error) {
var (
stdOutCapture bytes.Buffer
Expand Down Expand Up @@ -70,6 +71,19 @@ func startContainer(
args = append(args, "-e", envVars[i])
}

// passing envVars
for i := range mounts {
args = append(
args,
"--mount",
fmt.Sprintf(
"type=bind,source=%s,target=%s",
mounts[i][0],
mounts[i][1],
),
)
}

args = append(args, imageName)

// appending command overwrites
Expand Down
21 changes: 18 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package dft
import "strings"

type containerCfg struct {
args *[]string
env *[]string
ports *[][2]uint
args *[]string
env *[]string
mounts *[][2]string
ports *[][2]uint
}

type waitCfg struct {
Expand Down Expand Up @@ -53,6 +54,20 @@ func WithEnvVar(key string, value string) ContainerOption {
}
}

// WithExecuteInsideContainer defines if the wait cmd is executed inside the container
// or on the host machine
func WithMount(dest string, trgt string) ContainerOption {
return func(cfg *containerCfg) {
if cfg.mounts == nil {
cfg.mounts = &[][2]string{{dest, trgt}}

return
}

*cfg.mounts = append(*cfg.mounts, [2]string{dest, trgt})
}
}

// WithPort will expose the passed internal port via a given target port on the host.
func WithPort(port uint, target uint) ContainerOption {
return func(cfg *containerCfg) {
Expand Down

0 comments on commit 4a8c463

Please sign in to comment.