fix(cli): keep wait's spinner out of -o json, reject bad durations and make --verbose work (#48)

* fix(cli): keep wait's spinner out of -o json, reject bad durations and make --verbose work

wait draws its spinner only for text output on a terminal and clears it before the result; --timeout values that are not a positive number with ms, s, m or h exit 2 with a hint instead of a stack trace; --verbose now echoes each docker command to stderr and prints stack traces for errors.

* fix(cli): reject durations that overflow and pin the spinner rule on a terminal

A duration is checked for sign before it is converted and the conversion is overflow-checked, so -9223372036854775807s no longer wraps to a positive timeout. wait takes its terminal check through a seam, with tests for json, yaml and text output on a terminal.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-10 17:33:15 -05:00
1 parent 7ecc895baa
commit 52ebdee98d
8 files changed
+230 -10

No files matched your search

@@ -12,6 +12,7 @@ import io.floci.cli.output.Printer;
import java.util.Map;
import picocli.CommandLine.*;
import java.io.Console;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Callable;
@@ -19,6 +20,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BooleanSupplier;
@Command(
name = "wait",
@@ -42,12 +44,19 @@ public class WaitCommand implements Callable<Integer> {
/** Test seam: {@code docker} answers the container lookup that finds the endpoint. */
public WaitCommand(ProductProfile profile, DockerClient docker) {
this(profile, docker, WaitCommand::interactive);
}
/** Test seam: {@code terminal} stands in for "stdout is a terminal", which a test run is not. */
public WaitCommand(ProductProfile profile, DockerClient docker, BooleanSupplier terminal) {
this.profile = profile;
this.global = new GlobalOptions(profile);
this.docker = docker;
this.terminal = terminal;
}
private final DockerClient docker;
private final BooleanSupplier terminal;
@Option(names = {"--timeout"}, description = "Maximum time to wait (e.g. 30s, 2m)", defaultValue = "30s", paramLabel = "<duration>")
String timeout;
@@ -68,7 +77,16 @@ public class WaitCommand implements Callable<Integer> {
@Override
public Integer call() {
Printer printer = global.printer();
long timeoutMillis = Durations.parseDuration(timeout);
long timeoutMillis;
try {
timeoutMillis = Durations.parseDuration(timeout);
} catch (IllegalArgumentException e) {
printer.error(e.getMessage());
return 2;
}
// Only a person at a terminal watching text output gets the spinner: in -o json/yaml or
// in a pipe its frames would land in the data.
boolean spinner = printer.format() == OutputFormat.text && terminal.getAsBoolean();
// The deadline starts now, so the container lookup below counts against --timeout too.
Instant deadline = Instant.now().plusMillis(timeoutMillis);
String effectiveEndpoint = knownEndpoint != null ? knownEndpoint : endpointWithin(deadline);
@@ -79,6 +97,7 @@ public class WaitCommand implements Callable<Integer> {
long delay = FIRST_DELAY_MS;
while (Instant.now().isBefore(deadline)) {
if (isReady(client, service, requestTimeout(Duration.between(Instant.now(), deadline)))) {
if (spinner) clearSpinner(printer);
if (printer.format() != OutputFormat.text) {
printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint));
} else {
@@ -86,7 +105,7 @@ public class WaitCommand implements Callable<Integer> {
}
return 0;
}
printSpinner(printer, deadline);
if (spinner) printSpinner(printer, deadline);
long remaining = Duration.between(Instant.now(), deadline).toMillis();
if (remaining <= 0) break;
try { Thread.sleep(Math.min(delay, remaining)); } catch (InterruptedException e) {
@@ -96,6 +115,7 @@ public class WaitCommand implements Callable<Integer> {
delay = nextDelay(delay);
}
if (spinner) clearSpinner(printer);
printer.error("Timed out waiting for " + profile.displayName() + " after " + timeout
+ ".\nIs the container running? Try '" + profile.commandPrefix() + " status'.");
return 1;
@@ -153,6 +173,15 @@ public class WaitCommand implements Callable<Integer> {
}
}
private static boolean interactive() {
Console console = System.console();
return console != null && console.isTerminal();
}
private static void clearSpinner(Printer printer) {
printer.print("\r" + " ".repeat(40) + "\r");
}
private void printSpinner(Printer printer, Instant deadline) {
long remaining = Duration.between(Instant.now(), deadline).toSeconds();
printer.print("\r" + Ansi.gray("Waiting... (" + remaining + "s remaining)") + " ");