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 <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-11-04 17:29:33 -06:00
1 parent 0a8d1dcd57
commit 2a745d66bc
3 files changed
+40 -1

No files matched your search

+10 -1
View File
@@ -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 = "<url>")
@@ -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);
}
@@ -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;
}
@@ -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");