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
@@ -7,18 +7,32 @@ import io.floci.cli.output.Printer;
|
||||
import picocli.CommandLine.Option;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
/**
|
||||
* Shared global options for every product tree, parameterized by {@link ProductProfile}.
|
||||
*
|
||||
* The per-product defaults for {@code --endpoint} and {@code --container} are set as
|
||||
* field initial values in the constructor (env var override included) instead of
|
||||
* annotation {@code defaultValue}s — annotation strings are compile-time constants and
|
||||
* cannot vary by product. Picocli keeps a parsed-over field's initial value when the
|
||||
* option has no defaultValue, and renders it for {@code ${DEFAULT-VALUE}} in help.
|
||||
*
|
||||
* Commands must pre-initialize their {@code @Mixin GlobalOptions} field in their
|
||||
* constructor (picocli uses a non-null field instance as-is); a command that forgets
|
||||
* gets AWS defaults — covered by the per-tree parse tests.
|
||||
*/
|
||||
public class GlobalOptions {
|
||||
|
||||
public final ProductProfile product;
|
||||
|
||||
@Option(names = {"--endpoint"},
|
||||
description = "Floci server endpoint URL",
|
||||
defaultValue = "${FLOCI_ENDPOINT:-http://localhost:4566}",
|
||||
description = "Floci server endpoint URL (default: ${DEFAULT-VALUE})",
|
||||
paramLabel = "<url>")
|
||||
public String endpoint;
|
||||
|
||||
@Option(names = {"--container"},
|
||||
description = "Floci container name",
|
||||
defaultValue = "${FLOCI_CONTAINER:-floci}",
|
||||
description = "Floci container name (default: ${DEFAULT-VALUE})",
|
||||
paramLabel = "<name>")
|
||||
public String container;
|
||||
|
||||
@@ -42,6 +56,27 @@ public class GlobalOptions {
|
||||
@Option(names = {"--no-color"}, description = "Disable ANSI colors")
|
||||
public boolean noColor;
|
||||
|
||||
public GlobalOptions() {
|
||||
this(ProductProfile.AWS);
|
||||
}
|
||||
|
||||
public GlobalOptions(ProductProfile product) {
|
||||
this(product, System::getenv);
|
||||
}
|
||||
|
||||
/** Test seam: {@code envLookup} replaces {@code System.getenv}. */
|
||||
public GlobalOptions(ProductProfile product, UnaryOperator<String> envLookup) {
|
||||
this.product = product;
|
||||
this.endpoint = envOr(envLookup, product.envVar("ENDPOINT"), product.defaultEndpoint());
|
||||
this.container = envOr(envLookup, product.envVar("CONTAINER"), product.defaultContainer());
|
||||
}
|
||||
|
||||
// Matches picocli's ${VAR:-fallback}: only a NULL env var falls back — empty counts as set.
|
||||
private static String envOr(UnaryOperator<String> env, String var, String fallback) {
|
||||
String value = env.apply(var);
|
||||
return value != null ? value : fallback;
|
||||
}
|
||||
|
||||
public Printer printer() {
|
||||
if (noColor || !isStdoutTty()) {
|
||||
Ansi.disable();
|
||||
@@ -66,7 +101,7 @@ public class GlobalOptions {
|
||||
if (ports == null || ports.isBlank()) return fallback;
|
||||
try {
|
||||
int containerPort = URI.create(fallback).getPort();
|
||||
if (containerPort == -1) containerPort = 4566;
|
||||
if (containerPort == -1) containerPort = product.defaultPort();
|
||||
for (String mapping : ports.trim().split("\\s+")) {
|
||||
int arrow = mapping.indexOf("->");
|
||||
if (arrow < 0) continue;
|
||||
|
||||
Reference in new issue
Block a user