mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
feat(reset): add floci reset to delete every resource in a running emulator
floci reset (and floci gcp/az/oci reset) calls the emulator's state reset endpoint, so a test run can start clean without restarting the container. It asks for confirmation on a terminal; --yes skips the prompt and is required when there is no terminal. A server without the endpoint (floci-az today) gets a 'not available' message. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
14 files changed
+288
-2
No files matched your search
@@ -42,6 +42,7 @@ import java.util.Arrays;
|
||||
WaitCommand.class,
|
||||
VersionCommand.class,
|
||||
ServicesCommand.class,
|
||||
ResetCommand.class,
|
||||
DoctorCommand.class,
|
||||
EnvCommand.class,
|
||||
ConfigCommand.class,
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
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.http.FlociException;
|
||||
import io.floci.cli.http.FlociHttpClient;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
import io.floci.cli.output.Printer;
|
||||
import picocli.CommandLine.*;
|
||||
|
||||
import java.io.Console;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@Command(
|
||||
name = "reset",
|
||||
description = "Delete every resource in the running Floci AWS instance, keeping it running",
|
||||
mixinStandardHelpOptions = true
|
||||
)
|
||||
public class ResetCommand implements Callable<Integer> {
|
||||
|
||||
protected final ProductProfile profile;
|
||||
|
||||
@Mixin
|
||||
protected GlobalOptions global;
|
||||
|
||||
@Option(names = {"--yes", "-y"}, description = "Reset without asking for confirmation")
|
||||
boolean yes;
|
||||
|
||||
public ResetCommand() {
|
||||
this(ProductProfile.AWS);
|
||||
}
|
||||
|
||||
protected ResetCommand(ProductProfile profile) {
|
||||
this.profile = profile;
|
||||
this.global = new GlobalOptions(profile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() {
|
||||
Printer printer = global.printer();
|
||||
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
||||
|
||||
if (!yes) {
|
||||
Console console = System.console();
|
||||
if (console == null || !console.isTerminal()) {
|
||||
// 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);
|
||||
if (answer == null || !(answer.trim().equalsIgnoreCase("y") || answer.trim().equalsIgnoreCase("yes"))) {
|
||||
printer.println("Reset cancelled.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
new FlociHttpClient(effectiveEndpoint, profile).resetState();
|
||||
} catch (FlociException e) {
|
||||
if (e.getMessage().contains("HTTP 404") || e.getMessage().contains("HTTP 501")) {
|
||||
printer.error("Resetting state is not available on this " + profile.displayName() + " server version.\n"
|
||||
+ "Restart it without --persist for a clean state, or track progress: " + profile.serverRepo() + "/issues");
|
||||
} else {
|
||||
String message = "Failed to reset " + profile.displayName() + ": " + e.getMessage();
|
||||
// Connection errors already say what to do; others get the status hint.
|
||||
printer.error(message.contains("\n") ? message
|
||||
: message + "\nRun '" + profile.commandPrefix() + " status" + global.instanceSelector() + "' to check the emulator.");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (printer.format() != OutputFormat.text) {
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
out.put("reset", true);
|
||||
out.put("endpoint", effectiveEndpoint);
|
||||
printer.structured(out);
|
||||
return 0;
|
||||
}
|
||||
printer.println(Ansi.green("✓") + " " + profile.displayName() + " state reset" + Ansi.gray(" (" + effectiveEndpoint + ")"));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import picocli.CommandLine.*;
|
||||
WaitCommand.class,
|
||||
VersionCommand.class,
|
||||
ServicesCommand.class,
|
||||
ResetCommand.class,
|
||||
DoctorCommand.class,
|
||||
EnvCommand.class,
|
||||
ConfigCommand.class,
|
||||
|
||||
@@ -25,6 +25,7 @@ import picocli.CommandLine.*;
|
||||
AzWaitCommand.class,
|
||||
AzVersionCommand.class,
|
||||
AzServicesCommand.class,
|
||||
AzResetCommand.class,
|
||||
AzDoctorCommand.class,
|
||||
AzEnvCommand.class,
|
||||
AzSetupCommand.class,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.floci.cli.commands.az;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.ResetCommand;
|
||||
import picocli.CommandLine.Command;
|
||||
|
||||
@Command(
|
||||
name = "reset",
|
||||
description = "Delete every resource in the running Floci Azure instance, keeping it running",
|
||||
mixinStandardHelpOptions = true
|
||||
)
|
||||
public class AzResetCommand extends ResetCommand {
|
||||
|
||||
public AzResetCommand() {
|
||||
super(ProductProfile.AZ);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import picocli.CommandLine.*;
|
||||
GcpWaitCommand.class,
|
||||
GcpVersionCommand.class,
|
||||
GcpServicesCommand.class,
|
||||
GcpResetCommand.class,
|
||||
GcpDoctorCommand.class,
|
||||
GcpEnvCommand.class,
|
||||
GcpConfigCommand.class,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.floci.cli.commands.gcp;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.ResetCommand;
|
||||
import picocli.CommandLine.Command;
|
||||
|
||||
@Command(
|
||||
name = "reset",
|
||||
description = "Delete every resource in the running Floci GCP instance, keeping it running",
|
||||
mixinStandardHelpOptions = true
|
||||
)
|
||||
public class GcpResetCommand extends ResetCommand {
|
||||
|
||||
public GcpResetCommand() {
|
||||
super(ProductProfile.GCP);
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import picocli.CommandLine.*;
|
||||
OciWaitCommand.class,
|
||||
OciVersionCommand.class,
|
||||
OciServicesCommand.class,
|
||||
OciResetCommand.class,
|
||||
OciDoctorCommand.class,
|
||||
OciEnvCommand.class,
|
||||
OciSetupCommand.class,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.floci.cli.commands.oci;
|
||||
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.commands.ResetCommand;
|
||||
import picocli.CommandLine.Command;
|
||||
|
||||
@Command(
|
||||
name = "reset",
|
||||
description = "Delete every resource in the running Floci OCI instance, keeping it running",
|
||||
mixinStandardHelpOptions = true
|
||||
)
|
||||
public class OciResetCommand extends ResetCommand {
|
||||
|
||||
public OciResetCommand() {
|
||||
super(ProductProfile.OCI);
|
||||
}
|
||||
}
|
||||
@@ -171,6 +171,11 @@ public class FlociHttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
/** Deletes every resource the emulator holds ({@code POST <prefix>/state/reset}). */
|
||||
public Map<String, Object> resetState() throws FlociException {
|
||||
return postJson(controlPrefix + "/state/reset", "{}");
|
||||
}
|
||||
|
||||
public Map<String, Object> postSnapshot(String name) throws FlociException {
|
||||
return postJson(controlPrefix + "/snapshots/" + name, "{}");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user