From 11de360c11bd2f060100f8b41c50eed4da4d2ba1 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sat, 10 Oct 2026 21:51:42 -0500 Subject: [PATCH] feat(reset): stop when a selected container publishes no port for the emulator A selected container that is running without a matching published port has nothing to be found by, and the configured endpoint may be another instance. Signed-off-by: Hector Ventura --- src/main/java/io/floci/cli/GlobalOptions.java | 17 ++++++++--- .../io/floci/cli/commands/ResetCommand.java | 8 ++++- .../io/floci/cli/unit/ResetCommandTest.java | 29 +++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/floci/cli/GlobalOptions.java b/src/main/java/io/floci/cli/GlobalOptions.java index 375224f..7f213fa 100644 --- a/src/main/java/io/floci/cli/GlobalOptions.java +++ b/src/main/java/io/floci/cli/GlobalOptions.java @@ -9,6 +9,7 @@ import picocli.CommandLine.Option; import picocli.CommandLine.Spec; import java.net.URI; +import java.util.Optional; import java.util.function.UnaryOperator; import java.util.regex.Pattern; @@ -147,9 +148,17 @@ public class GlobalOptions { // Matches the container-side port from the configured endpoint to find the actual host port. public String endpointFromPorts(String ports, String fallback) { - if (ports == null || ports.isBlank()) return fallback; + return publishedEndpoint(ports, fallback).orElse(fallback); + } + + /** + * The endpoint a container with these published {@code ports} is reached on, or empty when it + * publishes no port for the one {@code configured} names. For callers that must not fall back. + */ + public Optional publishedEndpoint(String ports, String configured) { + if (ports == null || ports.isBlank()) return Optional.empty(); try { - int containerPort = URI.create(fallback).getPort(); + int containerPort = URI.create(configured).getPort(); if (containerPort == -1) containerPort = product.defaultPort(); for (String mapping : ports.trim().split("\\s+")) { int arrow = mapping.indexOf("->"); @@ -158,11 +167,11 @@ public class GlobalOptions { String rest = mapping.substring(arrow + 2); String cPort = rest.contains("/") ? rest.substring(0, rest.indexOf('/')) : rest; if (String.valueOf(containerPort).equals(cPort)) { - return "http://localhost:" + hostPort; + return Optional.of("http://localhost:" + hostPort); } } } catch (Exception ignored) {} - return fallback; + return Optional.empty(); } private static boolean isStdoutTty() { diff --git a/src/main/java/io/floci/cli/commands/ResetCommand.java b/src/main/java/io/floci/cli/commands/ResetCommand.java index e976102..aa2f877 100644 --- a/src/main/java/io/floci/cli/commands/ResetCommand.java +++ b/src/main/java/io/floci/cli/commands/ResetCommand.java @@ -109,7 +109,13 @@ public class ResetCommand implements Callable { return new Target(null, "Container '" + global.container + "' is not running (state: " + info.get().state() + "), so nothing was reset." + how); } - return new Target(global.endpointFromPorts(info.get().ports(), global.endpoint), null); + Optional published = global.publishedEndpoint(info.get().ports(), global.endpoint); + if (chosen && published.isEmpty()) { + // No port to find it by: the configured endpoint may well be another instance. + return new Target(null, "Container '" + global.container + + "' publishes no port for the emulator, so nothing was reset." + how); + } + return new Target(published.orElse(global.endpoint), null); } @Override diff --git a/src/test/java/io/floci/cli/unit/ResetCommandTest.java b/src/test/java/io/floci/cli/unit/ResetCommandTest.java index 1eadcf1..804ee30 100644 --- a/src/test/java/io/floci/cli/unit/ResetCommandTest.java +++ b/src/test/java/io/floci/cli/unit/ResetCommandTest.java @@ -193,6 +193,35 @@ class ResetCommandTest { assertTrue(target.error().contains("is not running (state: exited)"), target.error()); } + /** + * Running, but with no published port for the emulator (or only an unrelated one): there is + * nothing to find it by, and the configured endpoint may be the default instance. + */ + @ParameterizedTest + @ValueSource(strings = {"", "18080->8080/tcp"}) + void aChosenContainerWithoutAMatchingPortResetsNothing(String ports) { + ResetCommand.Target target = targetOf(dockerWith("floci-team-b", "running", ports), "--container", "floci-team-b"); + + assertNull(target.endpoint()); + assertTrue(target.error().contains("publishes no port for the emulator, so nothing was reset."), target.error()); + } + + @Test + void aChosenContainerIsFoundThroughItsPortMapping() { + ResetCommand.Target target = targetOf(dockerWith("floci-team-b", "running", "14566->4566/tcp"), + "--container", "floci-team-b"); + + assertEquals("http://localhost:14566", target.endpoint()); + } + + /** The default container without a published port (host networking) is still the default instance. */ + @Test + void theDefaultContainerWithoutAPublishedPortUsesItsConfiguredEndpoint() { + ResetCommand.Target target = targetOf(dockerWith("floci", "running", "")); + + assertEquals("http://localhost:4566", target.endpoint()); + } + @Test void aChosenContainerDockerCannotLookUpResetsNothing() { DockerClient broken = new DockerClient() {