Skip to content

Commit dd9a998

Browse files
committed
Polish "Add property to specify Docker Compose flags"
See gh-42571
1 parent 2bee29c commit dd9a998

File tree

10 files changed

+48
-167
lines changed

10 files changed

+48
-167
lines changed

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/core/DockerCliIntegrationTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
import org.junit.jupiter.api.Test;
3131
import org.junit.jupiter.api.io.TempDir;
3232

33+
import org.springframework.boot.docker.compose.core.DockerCli.DockerComposeOptions;
3334
import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeConfig;
3435
import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeDown;
3536
import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposePs;
3637
import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeStart;
3738
import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeStop;
3839
import org.springframework.boot.docker.compose.core.DockerCliCommand.ComposeUp;
3940
import org.springframework.boot.docker.compose.core.DockerCliCommand.Inspect;
40-
import org.springframework.boot.docker.compose.core.DockerCompose.Options;
4141
import org.springframework.boot.logging.LogLevel;
4242
import org.springframework.boot.testsupport.container.DisabledIfDockerUnavailable;
4343
import org.springframework.boot.testsupport.container.TestImage;
@@ -72,9 +72,8 @@ void runBasicCommand() {
7272
void runLifecycle() throws IOException {
7373
File composeFile = createComposeFile("redis-compose.yaml");
7474
String projectName = UUID.randomUUID().toString();
75-
Options options = Options.get(DockerComposeFile.of(composeFile), Collections.emptySet(),
76-
List.of("--project-name=" + projectName));
77-
DockerCli cli = new DockerCli(null, options);
75+
DockerCli cli = new DockerCli(null, new DockerComposeOptions(DockerComposeFile.of(composeFile),
76+
Collections.emptySet(), List.of("--project-name=" + projectName)));
7877
try {
7978
// Verify that no services are running (this is a fresh compose project)
8079
List<DockerCliComposePsResponse> ps = cli.run(new ComposePs());
@@ -113,8 +112,8 @@ void runLifecycle() throws IOException {
113112
@Test
114113
void shouldWorkWithMultipleComposeFiles() throws IOException {
115114
List<File> composeFiles = createComposeFiles();
116-
Options options = Options.get(DockerComposeFile.of(composeFiles), Set.of("dev"), Collections.emptyList());
117-
DockerCli cli = new DockerCli(null, options);
115+
DockerCli cli = new DockerCli(null,
116+
new DockerComposeOptions(DockerComposeFile.of(composeFiles), Set.of("dev"), Collections.emptyList()));
118117
try {
119118
// List the config and verify that both redis are there
120119
DockerCliComposeConfigResponse config = cli.run(new ComposeConfig());
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
redis1:
3-
profiles: [ dev ]
3+
profiles: [ 'dev' ]
44
image: '{imageName}'
55
ports:
66
- '6379'
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
redis3:
3-
profiles: [ prod ]
3+
profiles: [ 'prod' ]
44
image: '{imageName}'
55
ports:
66
- '6379'

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DefaultDockerCompose.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ public List<RunningService> getRunningServices() {
9797
if (runningPsResponses.isEmpty()) {
9898
return Collections.emptyList();
9999
}
100-
DockerCompose.Options options = this.cli.getDockerComposeOptions();
101-
DockerComposeFile dockerComposeFile = options.getComposeFile();
100+
DockerComposeFile dockerComposeFile = this.cli.getDockerComposeFile();
102101
List<RunningService> result = new ArrayList<>();
103102
Map<String, DockerCliInspectResponse> inspected = inspect(runningPsResponses);
104103
for (DockerCliComposePsResponse psResponse : runningPsResponses) {

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCli.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.util.ArrayList;
21+
import java.util.Collections;
2122
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
@@ -49,18 +50,18 @@ class DockerCli {
4950

5051
private final DockerCommands dockerCommands;
5152

52-
private final DockerCompose.Options dockerComposeOptions;
53+
private final DockerComposeOptions dockerComposeOptions;
5354

5455
/**
5556
* Create a new {@link DockerCli} instance.
5657
* @param workingDirectory the working directory or {@code null}
5758
* @param dockerComposeOptions the Docker Compose options to use or {@code null}.
5859
*/
59-
DockerCli(File workingDirectory, DockerCompose.Options dockerComposeOptions) {
60+
DockerCli(File workingDirectory, DockerComposeOptions dockerComposeOptions) {
6061
this.processRunner = new ProcessRunner(workingDirectory);
6162
this.dockerCommands = dockerCommandsCache.computeIfAbsent(workingDirectory,
6263
(key) -> new DockerCommands(this.processRunner));
63-
this.dockerComposeOptions = (dockerComposeOptions != null) ? dockerComposeOptions : DockerCompose.Options.NONE;
64+
this.dockerComposeOptions = (dockerComposeOptions != null) ? dockerComposeOptions : DockerComposeOptions.none();
6465
}
6566

6667
/**
@@ -89,8 +90,7 @@ private List<String> createCommand(Type type) {
8990
case DOCKER -> new ArrayList<>(this.dockerCommands.get(type));
9091
case DOCKER_COMPOSE -> {
9192
List<String> result = new ArrayList<>(this.dockerCommands.get(type));
92-
DockerCompose.Options options = this.dockerComposeOptions;
93-
DockerComposeFile composeFile = options.getComposeFile();
93+
DockerComposeFile composeFile = this.dockerComposeOptions.composeFile();
9494
if (composeFile != null) {
9595
for (File file : composeFile.getFiles()) {
9696
result.add("--file");
@@ -99,14 +99,14 @@ private List<String> createCommand(Type type) {
9999
}
100100
result.add("--ansi");
101101
result.add("never");
102-
Set<String> activeProfiles = options.getActiveProfiles();
102+
Set<String> activeProfiles = this.dockerComposeOptions.activeProfiles();
103103
if (!CollectionUtils.isEmpty(activeProfiles)) {
104104
for (String profile : activeProfiles) {
105105
result.add("--profile");
106106
result.add(profile);
107107
}
108108
}
109-
List<String> arguments = options.getArguments();
109+
List<String> arguments = this.dockerComposeOptions.arguments();
110110
if (!CollectionUtils.isEmpty(arguments)) {
111111
result.addAll(arguments);
112112
}
@@ -116,11 +116,11 @@ private List<String> createCommand(Type type) {
116116
}
117117

118118
/**
119-
* Return the {@link DockerCompose.Options} being used by this CLI instance.
120-
* @return the Docker Compose options
119+
* Return the {@link DockerComposeFile} being used by this CLI instance.
120+
* @return the Docker Compose file
121121
*/
122-
DockerCompose.Options getDockerComposeOptions() {
123-
return this.dockerComposeOptions;
122+
DockerComposeFile getDockerComposeFile() {
123+
return this.dockerComposeOptions.composeFile();
124124
}
125125

126126
/**
@@ -190,4 +190,22 @@ List<String> get(Type type) {
190190

191191
}
192192

193+
/**
194+
* Options for Docker Compose.
195+
*
196+
* @param composeFile the Docker Compose file to use
197+
* @param activeProfiles the profiles to activate
198+
* @param arguments the arguments to pass to Docker Compose
199+
*/
200+
record DockerComposeOptions(DockerComposeFile composeFile, Set<String> activeProfiles, List<String> arguments) {
201+
DockerComposeOptions {
202+
activeProfiles = (activeProfiles != null) ? activeProfiles : Collections.emptySet();
203+
arguments = (arguments != null) ? arguments : Collections.emptyList();
204+
}
205+
206+
static DockerComposeOptions none() {
207+
return new DockerComposeOptions(null, null, null);
208+
}
209+
}
210+
193211
}

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCompose.java

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Set;
2323

24+
import org.springframework.boot.docker.compose.core.DockerCli.DockerComposeOptions;
2425
import org.springframework.boot.logging.LogLevel;
2526

2627
/**
@@ -126,61 +127,23 @@ public interface DockerCompose {
126127
* @return a {@link DockerCompose} instance
127128
*/
128129
static DockerCompose get(DockerComposeFile file, String hostname, Set<String> activeProfiles) {
129-
DockerCli cli = new DockerCli(null, Options.get(file, activeProfiles, Collections.emptyList()));
130-
return new DefaultDockerCompose(cli, hostname);
130+
return get(file, hostname, activeProfiles, Collections.emptyList());
131131
}
132132

133133
/**
134134
* Factory method used to create a {@link DockerCompose} instance.
135+
* @param file the Docker Compose file
135136
* @param hostname the hostname used for services or {@code null} if the hostname
136-
* @param options the Docker Compose options or {@code null}
137+
* should be deduced
138+
* @param activeProfiles a set of the profiles that should be activated
139+
* @param arguments the arguments to pass to Docker Compose
137140
* @return a {@link DockerCompose} instance
138141
* @since 3.4.0
139142
*/
140-
static DockerCompose get(String hostname, Options options) {
141-
DockerCli cli = new DockerCli(null, options);
143+
static DockerCompose get(DockerComposeFile file, String hostname, Set<String> activeProfiles,
144+
List<String> arguments) {
145+
DockerCli cli = new DockerCli(null, new DockerComposeOptions(file, activeProfiles, arguments));
142146
return new DefaultDockerCompose(cli, hostname);
143147
}
144148

145-
/**
146-
* Docker Compose options that should be applied before any subcommand.
147-
*/
148-
interface Options {
149-
150-
/**
151-
* No options.
152-
*/
153-
Options NONE = get(null, Collections.emptySet(), Collections.emptyList());
154-
155-
/**
156-
* Factory method used to create a {@link DockerCompose.Options} instance.
157-
* @param file the Docker Compose file to use
158-
* @param activeProfiles the Docker Compose profiles to activate
159-
* @param arguments the additional Docker Compose arguments
160-
* @return the Docker Compose options
161-
*/
162-
static Options get(DockerComposeFile file, Set<String> activeProfiles, List<String> arguments) {
163-
return new DockerComposeOptions(file, activeProfiles, arguments);
164-
}
165-
166-
/**
167-
* the Docker Compose a file to use.
168-
* @return compose a file to use
169-
*/
170-
DockerComposeFile getComposeFile();
171-
172-
/**
173-
* the Docker Compose profiles to activate.
174-
* @return profiles to activate
175-
*/
176-
Set<String> getActiveProfiles();
177-
178-
/**
179-
* the additional Docker Compose arguments.
180-
* @return additional arguments
181-
*/
182-
List<String> getArguments();
183-
184-
}
185-
186149
}

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerComposeOptions.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ protected DockerComposeFile getComposeFile() {
162162

163163
protected DockerCompose getDockerCompose(DockerComposeFile composeFile, Set<String> activeProfiles,
164164
List<String> arguments) {
165-
DockerCompose.Options options = DockerCompose.Options.get(composeFile, activeProfiles, arguments);
166-
return DockerCompose.get(this.properties.getHost(), options);
165+
return DockerCompose.get(composeFile, this.properties.getHost(), activeProfiles, arguments);
167166
}
168167

169168
private boolean isIgnored(RunningService service) {

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class DockerComposeProperties {
4747
private boolean enabled = true;
4848

4949
/**
50-
* Arguments to pass to the docker compose command.
50+
* Arguments to pass to the Docker Compose command.
5151
*/
5252
private final List<String> arguments = new ArrayList<>();
5353

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/core/DefaultDockerComposeTests.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ void getRunningServicesReturnsServices() {
106106
HostConfig hostConfig = null;
107107
DockerCliInspectResponse inspectResponse = new DockerCliInspectResponse(id, config, networkSettings,
108108
hostConfig);
109-
willReturn(mock(DockerCompose.Options.class)).given(this.cli).getDockerComposeOptions();
110109
willReturn(List.of(psResponse)).given(this.cli).run(new DockerCliCommand.ComposePs());
111110
willReturn(List.of(inspectResponse)).given(this.cli).run(new DockerCliCommand.Inspect(List.of(id)));
112111
DefaultDockerCompose compose = new DefaultDockerCompose(this.cli, HOST);
@@ -133,7 +132,6 @@ void getRunningServicesWhenNoHostUsesHostFromContext() {
133132
hostConfig);
134133
willReturn(List.of(new DockerCliContextResponse("test", true, "https://192.168.1.1"))).given(this.cli)
135134
.run(new DockerCliCommand.Context());
136-
willReturn(mock(DockerCompose.Options.class)).given(this.cli).getDockerComposeOptions();
137135
willReturn(List.of(psResponse)).given(this.cli).run(new DockerCliCommand.ComposePs());
138136
willReturn(List.of(inspectResponse)).given(this.cli).run(new DockerCliCommand.Inspect(List.of(id)));
139137
DefaultDockerCompose compose = new DefaultDockerCompose(this.cli, null);
@@ -150,7 +148,6 @@ void worksWithTruncatedIds() {
150148
DockerCliComposePsResponse psResponse = new DockerCliComposePsResponse(shortId, "name", "redis", "running");
151149
Config config = new Config("redis", Collections.emptyMap(), Collections.emptyMap(), Collections.emptyList());
152150
DockerCliInspectResponse inspectResponse = new DockerCliInspectResponse(longId, config, null, null);
153-
willReturn(mock(DockerCompose.Options.class)).given(this.cli).getDockerComposeOptions();
154151
willReturn(List.of(new DockerCliContextResponse("test", true, "https://192.168.1.1"))).given(this.cli)
155152
.run(new DockerCliCommand.Context());
156153
willReturn(List.of(psResponse)).given(this.cli).run(new DockerCliCommand.ComposePs());

0 commit comments

Comments
 (0)