Skip to content

Commit c5ace15

Browse files
mdelapenyaclaude
andauthored
chore(nats): use Run function (#3418)
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent d0404be commit c5ace15

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

modules/nats/nats.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,44 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
2929

3030
// Run creates an instance of the NATS container type
3131
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*NATSContainer, error) {
32-
req := testcontainers.ContainerRequest{
33-
Image: img,
34-
ExposedPorts: []string{defaultClientPort, defaultRoutingPort, defaultMonitoringPort},
35-
Cmd: []string{"-DV", "-js"},
36-
WaitingFor: wait.ForListeningPort(defaultClientPort),
37-
}
38-
39-
genericContainerReq := testcontainers.GenericContainerRequest{
40-
ContainerRequest: req,
41-
Started: true,
42-
}
43-
4432
// Gather all config options (defaults and then apply provided options)
4533
settings := defaultOptions()
4634
for _, opt := range opts {
4735
if apply, ok := opt.(CmdOption); ok {
4836
apply(&settings)
4937
}
50-
if err := opt.Customize(&genericContainerReq); err != nil {
51-
return nil, err
52-
}
5338
}
5439

40+
moduleOpts := []testcontainers.ContainerCustomizer{
41+
testcontainers.WithExposedPorts(defaultClientPort, defaultRoutingPort, defaultMonitoringPort),
42+
testcontainers.WithCmd("-DV", "-js"),
43+
testcontainers.WithWaitStrategy(wait.ForListeningPort(defaultClientPort)),
44+
}
45+
46+
moduleOpts = append(moduleOpts, opts...)
47+
5548
// Include the command line arguments
49+
cmdArgs := []string{}
5650
for k, v := range settings.CmdArgs {
5751
// always prepend the dash because it was removed in the options
58-
genericContainerReq.Cmd = append(genericContainerReq.Cmd, []string{"--" + k, v}...)
52+
cmdArgs = append(cmdArgs, "--"+k, v)
53+
}
54+
if len(cmdArgs) > 0 {
55+
moduleOpts = append(moduleOpts, testcontainers.WithCmdArgs(cmdArgs...))
5956
}
6057

61-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
58+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
6259
var c *NATSContainer
63-
if container != nil {
60+
if ctr != nil {
6461
c = &NATSContainer{
65-
Container: container,
62+
Container: ctr,
6663
User: settings.CmdArgs["user"],
6764
Password: settings.CmdArgs["pass"],
6865
}
6966
}
7067

7168
if err != nil {
72-
return c, fmt.Errorf("generic container: %w", err)
69+
return c, fmt.Errorf("run nats: %w", err)
7370
}
7471

7572
return c, nil

0 commit comments

Comments
 (0)