From 2a745d66bc73efb9e7f14db4e3bfd7713695ca03 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Wed, 4 Nov 2026 17:29:33 -0600 Subject: [PATCH] fix(cli): treat a profile port equal to the default as a chosen port A profile with a non-default container and the product's default port yields the default endpoint, which read as no endpoint chosen, so env exited 1 and status reported null. The provider now records that the port came from the profile. A profile with neither a port nor an endpoint stays unknown. Addresses the review of #75. Signed-off-by: Hector Ventura --- src/main/java/io/floci/cli/GlobalOptions.java | 11 ++++++- .../config/ProfileDefaultValueProvider.java | 1 + .../cli/unit/MissingInstanceEndpointTest.java | 29 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/floci/cli/GlobalOptions.java b/src/main/java/io/floci/cli/GlobalOptions.java index 8a2745b..450b532 100644 --- a/src/main/java/io/floci/cli/GlobalOptions.java +++ b/src/main/java/io/floci/cli/GlobalOptions.java @@ -50,6 +50,10 @@ public class GlobalOptions { // The value FLOCI_ENDPOINT supplied, if any: that is a choice, like typing --endpoint. private final String endpointFromEnv; + // Whether the endpoint came from a profile's port. The string cannot say: a profile with + // port 4566 yields the product's default endpoint, yet that port was chosen. + private boolean portFromProfile; + @Option(names = {"--endpoint"}, description = "Floci server endpoint URL (default: ${DEFAULT-VALUE})", paramLabel = "") @@ -195,9 +199,14 @@ public class GlobalOptions { return noEmoji || !terminal || noColorEnv; } + /** Called by the profile provider when it derives the endpoint from a profile's {@code port}. */ + public void endpointCameFromProfilePort() { + portFromProfile = true; + } + /** Whether a configured endpoint can identify this instance without a container lookup. */ public boolean canUseConfiguredEndpoint() { - return typed("--endpoint") || endpoint.equals(endpointFromEnv) + return typed("--endpoint") || endpoint.equals(endpointFromEnv) || portFromProfile || !product.defaultEndpoint().equals(endpoint) || product.defaultContainer().equals(container); } diff --git a/src/main/java/io/floci/cli/config/ProfileDefaultValueProvider.java b/src/main/java/io/floci/cli/config/ProfileDefaultValueProvider.java index 806c309..358e1ff 100644 --- a/src/main/java/io/floci/cli/config/ProfileDefaultValueProvider.java +++ b/src/main/java/io/floci/cli/config/ProfileDefaultValueProvider.java @@ -94,6 +94,7 @@ public class ProfileDefaultValueProvider implements IDefaultValueProvider { } for (String optionName : option.names()) { GlobalOptions global = (GlobalOptions) command.mixins().get("global").userObject(); + if ("--endpoint".equals(optionName) && profile.port != null) global.endpointCameFromProfilePort(); String value = ProfileDefaults.valueFor(profile, optionName, global.product); if (value != null) return value; } diff --git a/src/test/java/io/floci/cli/unit/MissingInstanceEndpointTest.java b/src/test/java/io/floci/cli/unit/MissingInstanceEndpointTest.java index 95cf4d9..9072bfa 100644 --- a/src/test/java/io/floci/cli/unit/MissingInstanceEndpointTest.java +++ b/src/test/java/io/floci/cli/unit/MissingInstanceEndpointTest.java @@ -114,6 +114,35 @@ class MissingInstanceEndpointTest { assertTrue(out.toString().contains("\"endpoint\" : \"" + product.defaultEndpoint() + "\""), out.toString()); } + private GlobalOptions parsedEnv(ProductProfile product, String yaml) throws Exception { + Files.writeString(temp.resolve("other.yaml"), yaml); + var parsed = FlociCli.buildCommandLine(new ProfileStore(temp)).parseArgs(product.name().toLowerCase(), "env", "--profile", "other"); + while (parsed.hasSubcommand()) parsed = parsed.subcommand(); + return (GlobalOptions) parsed.commandSpec().mixins().get("global").userObject(); + } + + @ParameterizedTest + @MethodSource("products") + void aProfilePortEqualToTheDefaultIsStillAChosenPort(ProductProfile product) throws Exception { + GlobalOptions global = parsedEnv(product, "container: other\nport: " + product.defaultPort() + "\n"); + assertTrue(global.canUseConfiguredEndpoint()); + assertEquals("http://localhost:" + product.defaultPort(), global.resolvedEndpoint(missing)); + } + + @ParameterizedTest + @MethodSource("products") + void aProfileWithNeitherPortNorEndpointStaysUnknown(ProductProfile product) throws Exception { + GlobalOptions global = parsedEnv(product, "container: other\n"); + assertFalse(global.canUseConfiguredEndpoint()); + assertThrows(IllegalStateException.class, () -> global.resolvedEndpoint(missing)); + } + + @Test + void aRunningContainerStillCorrectsAProfilePortEqualToTheDefault() throws Exception { + GlobalOptions global = parsedEnv(ProductProfile.AWS, "container: other\nport: 4566\n"); + assertEquals("http://localhost:16000", global.resolvedEndpoint(running)); + } + @Test void aProfileEndpointOnAnotherHostBeatsALocalContainer() throws Exception { Files.writeString(temp.resolve("other.yaml"), "container: other\nendpoint: http://remote:9000\n");