Files
floci-cli/src/main/java/io/floci/cli/commands/WaitCommand.java
T
Hector Ventura f7b934a6be 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.
2026-07-28 21:22:14 -05:00

93 lines
3.1 KiB
Java

package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.http.FlociHttpClient;
import io.floci.cli.util.Durations;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import java.util.Map;
import picocli.CommandLine.*;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Callable;
@Command(
name = "wait",
description = "Wait until Floci AWS is ready to accept requests",
mixinStandardHelpOptions = true
)
public class WaitCommand implements Callable<Integer> {
protected final ProductProfile profile;
@Mixin
protected GlobalOptions global;
public WaitCommand() {
this(ProductProfile.AWS);
}
protected WaitCommand(ProductProfile profile) {
this.profile = profile;
this.global = new GlobalOptions(profile);
}
@Option(names = {"--timeout"}, description = "Maximum time to wait (e.g. 30s, 2m)", defaultValue = "30s", paramLabel = "<duration>")
String timeout;
@Option(names = {"--service"}, description = "Wait until a specific service is enabled", paramLabel = "<name>")
String service;
@Override
public Integer call() {
Printer printer = global.printer();
long timeoutMillis = Durations.parseDuration(timeout);
String effectiveEndpoint = global.resolvedEndpoint(new io.floci.cli.docker.DockerClient());
FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix());
Instant deadline = Instant.now().plusMillis(timeoutMillis);
while (Instant.now().isBefore(deadline)) {
if (isReady(client, service)) {
if (printer.format() != OutputFormat.text) {
printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint));
} else {
printer.println(Ansi.green(profile.displayName() + " is ready") + " (" + effectiveEndpoint + ")");
}
return 0;
}
printSpinner(printer, deadline);
try { Thread.sleep(500); } catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
printer.error("Timed out waiting for " + profile.displayName() + " after " + timeout
+ ".\nIs the container running? Try '" + profile.commandPrefix() + " status'.");
return 1;
}
private boolean isReady(FlociHttpClient client, String requiredService) {
try {
var health = client.health();
if (requiredService == null) return true;
for (String s : health.services()) {
if (s.equalsIgnoreCase(requiredService)) return true;
}
return false;
} catch (Exception e) {
return false;
}
}
private void printSpinner(Printer printer, Instant deadline) {
long remaining = Duration.between(Instant.now(), deadline).toSeconds();
printer.print("\r" + Ansi.gray("Waiting... (" + remaining + "s remaining)") + " ");
}
}