refactor: reduce cognitive complexity of DetectDockerRunner
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
+import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import com.github.dockerjava.api.DockerClient;
@@ -28,6 +29,19 @@ public class DetectDockerRunner {
.exec().getId();
try {
+ return runContainer(dockerClient, containerId);
+ } catch (Exception e) {
+ Assertions.assertNull(e, "An exception occurred running docker commands.");
+ } finally {
+ dockerClient.removeContainerCmd(containerId).withRemoveVolumes(true).exec();
+ }
+
+ Assertions.fail("Should have already returned! Something has gone wrong!");
+ return null;
+ }
+
+ @NotNull
+ private DockerDetectResult runContainer(DockerClient dockerClient, String containerId) throws InterruptedException {
dockerClient.startContainerCmd(containerId).exec();
int exitCode = dockerClient.waitContainerCmd(containerId)
@@ -46,14 +60,6 @@ public class DetectDockerRunner {
}
return new DockerDetectResult(exitCode, logs);
- } catch (Exception e) {
- Assertions.assertNull(e, "An exception occurred running docker commands.");
- } finally {
- dockerClient.removeContainerCmd(containerId).withRemoveVolumes(true).exec();
- }
-
- Assertions.fail("Should have already returned! Something has gone wrong!");
- return null;
}
public DockerClient connectToDocker() {
