mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
refactor!: unify the four product command trees behind ProductProfile (#16)
* refactor!: unify the four product command trees behind ProductProfile
Every shared command now has ONE implementation in commands/,
parameterized by ProductProfile (the single source of per-product
config: endpoint, container, image, env prefix, control prefix,
display name). The gcp/az/oci command classes become ~8-line shims
(GcpStartCommand extends StartCommand { super(ProductProfile.GCP); })
that carry only their per-product help description; the three
near-identical *GlobalOptions clones are deleted. Each product tree
drops from ~1,360 to ~530 lines and copy-drift becomes impossible.
Mechanics: per-product option defaults are constructor-set field
values rendered in help via ${DEFAULT-VALUE} (annotation defaults are
compile-time constants); commands pre-initialize their GlobalOptions
mixin with the profile (picocli uses a non-null mixin field instance
as-is). Verified against a captured baseline of 480 CLI outputs
(every --help x 5 trees, env/status/doctor JSON, exit codes, routing)
plus new pinning tests: ProductProfileTest, ProductTreesParsingTest,
DoctorCheckListTest, GlobalOptionsTest.
Behavior changes, all deliberate:
- snapshot commands in gcp/az/oci trees now perform the real
/snapshots API calls with graceful degradation and exit 1 while the
server lacks the endpoints (previously: note + exit 0)
- product logs/services gained --service/--mode, and services now
resolves the endpoint from the container port mapping in all trees
- the AWS tree brands itself 'Floci AWS' in banners and help,
matching the other products
- snapshot errors link the product issue tracker instead of TODO.md
- completion generates from spec.root() instead of a hardcoded
FlociCli instance (output unchanged)
BREAKING CHANGE: 'floci gcp|az|oci snapshot export/import' (and bare
'floci snapshot ...' with a non-AWS default product) now exit 1
instead of 0 while server-side snapshot support is pending; CI
scripts relying on the silent success of stubbed snapshot commands
will fail.
* fix: snapshot save/load/list/delete resolve the container host port
Same port auto-detection status/services/wait use — a container started
with --port <n> is now reachable by the snapshot commands without an
explicit --endpoint. Applies to all four trees via the unified base
commands.
This commit is contained in:
1 parent
0a65de2303
commit
6dc8fc9df4
100 files changed
+1190
-3283
No files matched your search
@@ -0,0 +1,45 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.DoctorCommand;
|
||||
import io.floci.cli.doctor.Check;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** Pins the per-product doctor check composition and order (Docker checks first, then companions). */
|
||||
class DoctorCheckListTest {
|
||||
|
||||
private static final List<String> BASE = List.of(
|
||||
"DockerInstalledCheck", "DockerDaemonCheck", "DockerSocketCheck", "DockerVersionCheck",
|
||||
"PortAvailableCheck", "ImagePresentCheck", "ImageVersionCheck",
|
||||
"ContainerRunningCheck", "EndpointReachableCheck");
|
||||
|
||||
private static List<String> names(List<Check> checks) {
|
||||
return checks.stream().map(c -> c.getClass().getSimpleName()).toList();
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsHasBaseChecksPlusAwsCliCompanions() {
|
||||
var names = names(new DoctorCommand().allChecks());
|
||||
assertEquals(BASE.size() + 2, names.size());
|
||||
assertEquals(BASE, names.subList(0, BASE.size()));
|
||||
assertEquals(List.of("AwsCliEndpointCheck", "AwsCliS3PathStyleCheck"), names.subList(BASE.size(), names.size()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void azHasBaseChecksPlusAzCliCompanions() {
|
||||
var names = names(new DoctorCommand(ProductProfile.AZ, DoctorCommand.AZ_COMPANION_CHECKS).allChecks());
|
||||
assertEquals(BASE, names.subList(0, BASE.size()));
|
||||
assertEquals(List.of("AzCliInstalledCheck", "AzCliConnectionStringCheck"), names.subList(BASE.size(), names.size()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void gcpAndOciHaveExactlyTheBaseChecks() {
|
||||
for (ProductProfile p : List.of(ProductProfile.GCP, ProductProfile.OCI)) {
|
||||
assertEquals(BASE, names(new DoctorCommand(p, List.of()).allChecks()), p.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.ProductProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class GlobalOptionsTest {
|
||||
|
||||
private static UnaryOperator<String> env(Map<String, String> vars) {
|
||||
return vars::get;
|
||||
}
|
||||
|
||||
@Test
|
||||
void productDefaultsWhenEnvUnset() {
|
||||
GlobalOptions gcp = new GlobalOptions(ProductProfile.GCP, env(Map.of()));
|
||||
assertEquals("http://localhost:4588", gcp.endpoint);
|
||||
assertEquals("floci-gcp", gcp.container);
|
||||
|
||||
GlobalOptions oci = new GlobalOptions(ProductProfile.OCI, env(Map.of()));
|
||||
assertEquals("http://localhost:4599", oci.endpoint);
|
||||
assertEquals("floci-oci", oci.container);
|
||||
}
|
||||
|
||||
@Test
|
||||
void envVarOverridesDefault() {
|
||||
GlobalOptions az = new GlobalOptions(ProductProfile.AZ,
|
||||
env(Map.of("FLOCI_AZ_ENDPOINT", "http://remote:9999", "FLOCI_AZ_CONTAINER", "custom")));
|
||||
assertEquals("http://remote:9999", az.endpoint);
|
||||
assertEquals("custom", az.container);
|
||||
}
|
||||
|
||||
@Test
|
||||
void wrongProductEnvVarIsIgnored() {
|
||||
GlobalOptions gcp = new GlobalOptions(ProductProfile.GCP,
|
||||
env(Map.of("FLOCI_ENDPOINT", "http://aws-only:1")));
|
||||
assertEquals("http://localhost:4588", gcp.endpoint);
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyEnvVarCountsAsSet() {
|
||||
// Matches picocli ${VAR:-fallback} semantics: only null falls back.
|
||||
GlobalOptions aws = new GlobalOptions(ProductProfile.AWS,
|
||||
env(Map.of("FLOCI_ENDPOINT", "")));
|
||||
assertEquals("", aws.endpoint);
|
||||
}
|
||||
|
||||
@Test
|
||||
void noArgConstructorIsAws() {
|
||||
GlobalOptions options = new GlobalOptions();
|
||||
assertEquals(ProductProfile.AWS, options.product);
|
||||
// Cannot assert endpoint literal here: real env may set FLOCI_ENDPOINT.
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointFromPortsUsesProductFallbackPortForPortlessEndpoint() {
|
||||
// Endpoint URL without a port → the product's default port must be matched.
|
||||
for (ProductProfile p : new ProductProfile[]{
|
||||
ProductProfile.AWS, ProductProfile.GCP, ProductProfile.AZ, ProductProfile.OCI}) {
|
||||
GlobalOptions options = new GlobalOptions(p, env(Map.of()));
|
||||
String result = options.endpointFromPorts(
|
||||
"12345->" + p.defaultPort() + "/tcp", "http://localhost");
|
||||
assertEquals("http://localhost:12345", result, p.name());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointFromPortsMatchesExplicitPort() {
|
||||
GlobalOptions gcp = new GlobalOptions(ProductProfile.GCP, env(Map.of()));
|
||||
assertEquals("http://localhost:9999",
|
||||
gcp.endpointFromPorts("9999->4588/tcp", "http://localhost:4588"));
|
||||
assertEquals("http://localhost:4588",
|
||||
gcp.endpointFromPorts("9999->1111/tcp", "http://localhost:4588"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** Pins every field of the four product constants — the drift firewall for the unified tree. */
|
||||
class ProductProfileTest {
|
||||
|
||||
@Test
|
||||
void aws() {
|
||||
assertEquals("aws", ProductProfile.AWS.name());
|
||||
assertEquals("Floci AWS", ProductProfile.AWS.displayName());
|
||||
assertEquals("floci/floci", ProductProfile.AWS.image());
|
||||
assertEquals("floci/floci:latest", ProductProfile.AWS.defaultImageRef());
|
||||
assertEquals("floci", ProductProfile.AWS.defaultContainer());
|
||||
assertEquals(4566, ProductProfile.AWS.defaultPort());
|
||||
assertEquals("http://localhost:4566", ProductProfile.AWS.defaultEndpoint());
|
||||
assertEquals("FLOCI_ENDPOINT", ProductProfile.AWS.envVar("ENDPOINT"));
|
||||
assertEquals("/_floci", ProductProfile.AWS.controlPrefix());
|
||||
assertEquals("floci", ProductProfile.AWS.commandPrefix());
|
||||
}
|
||||
|
||||
@Test
|
||||
void gcp() {
|
||||
assertEquals("gcp", ProductProfile.GCP.name());
|
||||
assertEquals("Floci GCP", ProductProfile.GCP.displayName());
|
||||
assertEquals("floci/floci-gcp", ProductProfile.GCP.image());
|
||||
assertEquals("floci-gcp", ProductProfile.GCP.defaultContainer());
|
||||
assertEquals(4588, ProductProfile.GCP.defaultPort());
|
||||
assertEquals("FLOCI_GCP_CONTAINER", ProductProfile.GCP.envVar("CONTAINER"));
|
||||
assertEquals("/_floci-gcp", ProductProfile.GCP.controlPrefix());
|
||||
assertEquals("floci gcp", ProductProfile.GCP.commandPrefix());
|
||||
}
|
||||
|
||||
@Test
|
||||
void az() {
|
||||
assertEquals("az", ProductProfile.AZ.name());
|
||||
assertEquals("Floci Azure", ProductProfile.AZ.displayName());
|
||||
assertEquals("floci/floci-az", ProductProfile.AZ.image());
|
||||
assertEquals("floci-az", ProductProfile.AZ.defaultContainer());
|
||||
assertEquals(4577, ProductProfile.AZ.defaultPort());
|
||||
assertEquals("FLOCI_AZ_ENDPOINT", ProductProfile.AZ.envVar("ENDPOINT"));
|
||||
// The floci-az server exposes /_floci/* (same as AWS), NOT /_floci-az.
|
||||
assertEquals("/_floci", ProductProfile.AZ.controlPrefix());
|
||||
assertEquals("floci az", ProductProfile.AZ.commandPrefix());
|
||||
}
|
||||
|
||||
@Test
|
||||
void oci() {
|
||||
assertEquals("oci", ProductProfile.OCI.name());
|
||||
assertEquals("Floci OCI", ProductProfile.OCI.displayName());
|
||||
assertEquals("floci/floci-oci", ProductProfile.OCI.image());
|
||||
assertEquals("floci-oci", ProductProfile.OCI.defaultContainer());
|
||||
assertEquals(4599, ProductProfile.OCI.defaultPort());
|
||||
assertEquals("FLOCI_OCI_ENDPOINT", ProductProfile.OCI.envVar("ENDPOINT"));
|
||||
assertEquals("/_floci-oci", ProductProfile.OCI.controlPrefix());
|
||||
assertEquals("floci oci", ProductProfile.OCI.commandPrefix());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.FlociCli;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.Model.CommandSpec;
|
||||
import picocli.CommandLine.ParseResult;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Characterization tests pinning the CLI surface of all four product trees.
|
||||
* Written against the copied-tree implementation; they must keep passing
|
||||
* unchanged through the ProductProfile unification refactor.
|
||||
*/
|
||||
class ProductTreesParsingTest {
|
||||
|
||||
private static final Set<String> LIFECYCLE_COMMANDS = Set.of(
|
||||
"start", "stop", "restart", "status", "logs", "wait",
|
||||
"version", "services", "doctor", "env", "config", "snapshot", "completion", "help");
|
||||
|
||||
private static CommandSpec leafSpec(String... args) {
|
||||
ParseResult r = new CommandLine(new FlociCli()).parseArgs(args);
|
||||
while (r.hasSubcommand()) r = r.subcommand();
|
||||
return r.commandSpec();
|
||||
}
|
||||
|
||||
private static void assertDefaults(String expectedEndpoint, String expectedContainer, String... args) {
|
||||
CommandSpec spec = leafSpec(args);
|
||||
assertEquals(expectedEndpoint, spec.findOption("--endpoint").getValue());
|
||||
assertEquals(expectedContainer, spec.findOption("--container").getValue());
|
||||
}
|
||||
|
||||
private static void assertStartDefaults(String port, String image, String... args) {
|
||||
CommandSpec spec = leafSpec(args);
|
||||
assertEquals(Integer.valueOf(port), spec.findOption("--port").getValue());
|
||||
assertEquals(image, spec.findOption("--image").getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsTreeDefaults() {
|
||||
assertDefaults("http://localhost:4566", "floci", "status");
|
||||
assertDefaults("http://localhost:4566", "floci", "aws", "status");
|
||||
assertStartDefaults("4566", "floci/floci:latest", "start");
|
||||
assertStartDefaults("4566", "floci/floci:latest", "aws", "start");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gcpTreeDefaults() {
|
||||
assertDefaults("http://localhost:4588", "floci-gcp", "gcp", "status");
|
||||
assertStartDefaults("4588", "floci/floci-gcp:latest", "gcp", "start");
|
||||
}
|
||||
|
||||
@Test
|
||||
void azTreeDefaults() {
|
||||
assertDefaults("http://localhost:4577", "floci-az", "az", "status");
|
||||
assertStartDefaults("4577", "floci/floci-az:latest", "az", "start");
|
||||
}
|
||||
|
||||
@Test
|
||||
void ociTreeDefaults() {
|
||||
assertDefaults("http://localhost:4599", "floci-oci", "oci", "status");
|
||||
assertStartDefaults("4599", "floci/floci-oci:latest", "oci", "start");
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointFlagOverridesDefaultInEveryTree() {
|
||||
for (String tree : new String[]{"aws", "gcp", "az", "oci"}) {
|
||||
CommandSpec spec = leafSpec(tree, "status", "--endpoint", "http://x:1");
|
||||
assertEquals("http://x:1", spec.findOption("--endpoint").getValue(), tree);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyProductGroupExposesTheFullCommandSet() {
|
||||
CommandLine root = new CommandLine(new FlociCli());
|
||||
for (String tree : new String[]{"aws", "gcp", "az", "oci"}) {
|
||||
Set<String> names = root.getSubcommands().get(tree).getSubcommands().keySet();
|
||||
assertTrue(names.containsAll(LIFECYCLE_COMMANDS),
|
||||
tree + " is missing " + LIFECYCLE_COMMANDS.stream().filter(c -> !names.contains(c)).toList());
|
||||
}
|
||||
// oci additionally has setup; bare root additionally has update + the product groups
|
||||
assertTrue(root.getSubcommands().get("oci").getSubcommands().containsKey("setup"));
|
||||
assertTrue(root.getSubcommands().keySet().containsAll(Set.of("update", "aws", "gcp", "az", "oci")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void productConfigGroupsHaveNoDefaultProduct() {
|
||||
CommandLine root = new CommandLine(new FlociCli());
|
||||
assertTrue(root.getSubcommands().get("config").getSubcommands().containsKey("default-product"));
|
||||
for (String tree : new String[]{"gcp", "az", "oci"}) {
|
||||
Set<String> configSubs = root.getSubcommands().get(tree)
|
||||
.getSubcommands().get("config").getSubcommands().keySet();
|
||||
assertTrue(configSubs.containsAll(Set.of("show", "validate", "profile")), tree);
|
||||
assertFalse(configSubs.contains("default-product"), tree);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void snapshotGroupsExposeAllLeaves() {
|
||||
CommandLine root = new CommandLine(new FlociCli());
|
||||
Set<String> expected = Set.of("save", "load", "list", "delete", "export", "import");
|
||||
assertTrue(root.getSubcommands().get("snapshot").getSubcommands().keySet().containsAll(expected));
|
||||
for (String tree : new String[]{"gcp", "az", "oci"}) {
|
||||
assertTrue(root.getSubcommands().get(tree)
|
||||
.getSubcommands().get("snapshot").getSubcommands().keySet().containsAll(expected), tree);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sharedFlagsParseInEveryTree() {
|
||||
for (String tree : new String[]{"aws", "gcp", "az", "oci"}) {
|
||||
CommandSpec spec = leafSpec(tree, "status", "-o", "json", "-q", "--no-color", "-v");
|
||||
assertEquals("json", spec.findOption("--output").getValue().toString());
|
||||
assertEquals(Boolean.TRUE, spec.findOption("--quiet").getValue());
|
||||
assertEquals(Boolean.TRUE, spec.findOption("--no-color").getValue());
|
||||
assertEquals(Boolean.TRUE, spec.findOption("--verbose").getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.floci.cli.unit;
|
||||
|
||||
import io.floci.cli.commands.WaitCommand;
|
||||
import io.floci.cli.util.Durations;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
@@ -18,11 +18,11 @@ class WaitCommandParseTest {
|
||||
"10, 10000"
|
||||
})
|
||||
void testDurationParsing(String input, long expectedMillis) {
|
||||
assertEquals(expectedMillis, WaitCommand.parseDuration(input.trim()));
|
||||
assertEquals(expectedMillis, Durations.parseDuration(input.trim()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNullDuration() {
|
||||
assertEquals(30_000, WaitCommand.parseDuration(null));
|
||||
assertEquals(30_000, Durations.parseDuration(null));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user