feat(reset): never guess which server to reset

A typed --endpoint is used as given instead of being replaced by a local container's port, and a container the user selected has to be found running: a missing or stopped one resets nothing rather than falling back to the default instance. The confirmation goes through a seam, with tests for every answer.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-10-10 21:29:35 -05:00
1 parent 024d5d6c23
commit 9ded9185bd
4 files changed
+294 -6

No files matched your search

@@ -3,6 +3,7 @@ package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.docker.DockerClient;
import io.floci.cli.docker.DockerException;
import io.floci.cli.http.FlociException;
import io.floci.cli.http.FlociHttpClient;
import io.floci.cli.output.Ansi;
@@ -13,6 +14,7 @@ import picocli.CommandLine.*;
import java.io.Console;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
@Command(
@@ -35,25 +37,100 @@ public class ResetCommand implements Callable<Integer> {
}
protected ResetCommand(ProductProfile profile) {
this(profile, new DockerClient(), Prompt.console());
}
/** Test seam: {@code docker} answers the container lookup and {@code prompt} the confirmation. */
public ResetCommand(ProductProfile profile, DockerClient docker, Prompt prompt) {
this.profile = profile;
this.global = new GlobalOptions(profile);
this.docker = docker;
this.prompt = prompt;
}
private final DockerClient docker;
private final Prompt prompt;
/** The person at the terminal, when there is one. */
public interface Prompt {
/** False when nobody can answer (a pipe, CI). */
boolean available();
/** The answer to {@code question}, or null at end of input. */
String ask(String question);
static Prompt console() {
return new Prompt() {
@Override
public boolean available() {
Console console = System.console();
return console != null && console.isTerminal();
}
@Override
public String ask(String question) {
return System.console().readLine("%s", question);
}
};
}
}
/** The server a reset will wipe, or why none could be settled on. Exactly one is set. */
public record Target(String endpoint, String error) {}
/**
* Where this reset acts. Reset deletes data, so unlike the read-only commands it never
* guesses: an {@code --endpoint} the user typed is used as given, and a container the user
* chose must be found running. Falling back to the configured endpoint, as
* {@link GlobalOptions#resolvedEndpoint} does, is right only for the default container: there
* the configured endpoint names the same default instance.
*/
public Target target() {
if (global.typed("--endpoint")) return new Target(global.endpoint, null);
boolean chosen = global.typed("--container") || !profile.defaultContainer().equals(global.container);
String how = "\nRun '" + profile.commandPrefix() + " status" + global.instanceSelector()
+ "' to check it, or pass --endpoint <url> to name the server to reset.";
Optional<DockerClient.ContainerInfo> info;
try {
info = docker.inspectContainer(global.container);
} catch (DockerException e) {
if (chosen) {
return new Target(null, "Could not look up container '" + global.container + "': " + e.getMessage() + how);
}
info = Optional.empty();
}
if (info.isEmpty()) {
return chosen
? new Target(null, "Container '" + global.container + "' not found, so nothing was reset." + how)
: new Target(global.endpoint, null);
}
if (chosen && !"running".equals(info.get().state())) {
return new Target(null, "Container '" + global.container + "' is not running (state: "
+ info.get().state() + "), so nothing was reset." + how);
}
return new Target(global.endpointFromPorts(info.get().ports(), global.endpoint), null);
}
@Override
public Integer call() {
Printer printer = global.printer();
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
Target target = target();
if (target.error() != null) {
printer.error(target.error());
return 1;
}
String effectiveEndpoint = target.endpoint();
if (!yes) {
Console console = System.console();
if (console == null || !console.isTerminal()) {
if (!prompt.available()) {
// Nobody to ask: a script must say so explicitly.
printer.error("reset deletes every resource in " + profile.displayName() + " and needs confirmation.\n"
+ "Pass --yes to reset without asking.");
return 2;
}
String answer = console.readLine("Delete every resource in %s at %s? [y/N] ",
profile.displayName(), effectiveEndpoint);
String answer = prompt.ask("Delete every resource in " + profile.displayName()
+ " at " + effectiveEndpoint + "? [y/N] ");
if (answer == null || !(answer.trim().equalsIgnoreCase("y") || answer.trim().equalsIgnoreCase("yes"))) {
printer.println("Reset cancelled.");
return 1;