mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-23 06:00:47 -05:00
fix(cli): exit 2 for every rejected value, not 1 in some commands (#66)
* fix(cli): exit 2 for every rejected value, not 1 in some commands config default-product, config profile (unknown action, missing name, --port out of range), update --version, an invalid snapshot name, an invalid az setup container name and an unusable --persist path returned 1, the code for a command that ran and failed. They now return 2 like every other rejected value. A persist directory that cannot be created is still 1. Signed-off-by: Hector Ventura <hectorvent@gmail.com> * test(restart): pin exit 2 for an unusable persist path before anything is stopped Signed-off-by: Hector Ventura <hectorvent@gmail.com> --------- Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
188aa23edc
commit
c65987abba
16 files changed
+175
-32
No files matched your search
@@ -74,6 +74,11 @@ public class RestartCommand implements Callable<Integer> {
|
||||
printer.error(invalid);
|
||||
return 2;
|
||||
}
|
||||
String rejected = start.persistPathError();
|
||||
if (rejected != null) {
|
||||
printer.error(rejected);
|
||||
return ExitCode.USAGE;
|
||||
}
|
||||
String unwritable = start.preparePersistDir();
|
||||
if (unwritable != null) {
|
||||
printer.error(unwritable);
|
||||
|
||||
@@ -187,8 +187,13 @@ public class StartCommand implements Callable<Integer> {
|
||||
return preparePersistDir(docker.daemonHost());
|
||||
}
|
||||
|
||||
/** {@link #preparePersistDir()} for a daemon reached through {@code daemon}; the test seam. */
|
||||
public String preparePersistDir(DockerClient.DockerHost daemon) {
|
||||
/**
|
||||
* Why the {@code --persist} value cannot be used at all, or {@code null}. This is about the
|
||||
* value, not about the file system: a path this machine cannot parse, or a relative one for
|
||||
* a remote daemon. Callers report it as a usage error (exit 2); a directory that is fine as a
|
||||
* value but cannot be created is {@link #preparePersistDir}'s to report (exit 1).
|
||||
*/
|
||||
public String persistPathError(DockerClient.DockerHost daemon) {
|
||||
if (persistDir == null || persistDir.isBlank()) return null;
|
||||
// A remote daemon reads the bind source on its own machine, where this CLI can neither
|
||||
// create nor check it; it is passed on untouched (bindSource), so nothing here can fail
|
||||
@@ -199,14 +204,29 @@ public class StartCommand implements Callable<Integer> {
|
||||
: "With a remote Docker daemon, --persist must be an absolute path on the daemon's machine, but was '"
|
||||
+ persistDir + "'.\nPass an absolute path, for example /srv/floci-data.";
|
||||
}
|
||||
Path path;
|
||||
try {
|
||||
path = Path.of(persistPath());
|
||||
Path.of(persistPath());
|
||||
return null;
|
||||
} catch (InvalidPathException e) {
|
||||
// Names persistDir as given: resolving it is what failed.
|
||||
return "Invalid persist directory '" + persistDir + "': " + e.getReason()
|
||||
+ ".\nPass a --persist path that is valid on this system.";
|
||||
}
|
||||
}
|
||||
|
||||
/** {@link #persistPathError(DockerClient.DockerHost)} for the daemon docker resolved. */
|
||||
public String persistPathError() {
|
||||
return persistPathError(docker.daemonHost());
|
||||
}
|
||||
|
||||
/** {@link #preparePersistDir()} for a daemon reached through {@code daemon}; the test seam. */
|
||||
public String preparePersistDir(DockerClient.DockerHost daemon) {
|
||||
if (persistDir == null || persistDir.isBlank()) return null;
|
||||
String rejected = persistPathError(daemon);
|
||||
if (rejected != null) return rejected;
|
||||
// A remote daemon creates the directory on its own machine.
|
||||
if (daemon.kind() == DockerClient.Kind.TCP) return null;
|
||||
Path path = Path.of(persistPath());
|
||||
try {
|
||||
Files.createDirectories(path);
|
||||
return null;
|
||||
@@ -314,6 +334,11 @@ public class StartCommand implements Callable<Integer> {
|
||||
// whether the daemon docker will talk to is on this machine.
|
||||
DockerClient.DockerHost daemon = docker.daemonHost();
|
||||
|
||||
String rejected = persistPathError(daemon);
|
||||
if (rejected != null) {
|
||||
printer.error(rejected);
|
||||
return ExitCode.USAGE;
|
||||
}
|
||||
String unwritable = preparePersistDir(daemon);
|
||||
if (unwritable != null) {
|
||||
printer.error(unwritable);
|
||||
|
||||
@@ -101,8 +101,10 @@ public class UpdateCommand implements Callable<Integer> {
|
||||
String current = CliVersion.CLI_VERSION;
|
||||
boolean pinned = to != null && !to.isBlank();
|
||||
if (pinned && !Version.isValid(to.trim())) {
|
||||
// It goes into the download URL; refuse anything that is not a release version.
|
||||
throw new UpdateException("invalid --version '" + to + "'.\nPass a release version such as 0.3.0.");
|
||||
// It goes into the download URL; refuse anything that is not a release version. A
|
||||
// value the command cannot accept, so the exit code of any other bad argument.
|
||||
printer.error("invalid --version '" + to + "'.\nPass a release version such as 0.3.0.");
|
||||
return ExitCode.USAGE;
|
||||
}
|
||||
String target = pinned ? to.trim() : fetchLatestVersion();
|
||||
|
||||
|
||||
@@ -120,8 +120,9 @@ public class AzSetupCommand implements Callable<Integer> {
|
||||
try {
|
||||
flociAzDir = InstanceState.dir(stateRoot, ProductProfile.AZ, cloud);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// A container name that cannot be used: the same exit code as any other bad argument.
|
||||
printer.error(e.getMessage());
|
||||
return 1;
|
||||
return ExitCode.USAGE;
|
||||
}
|
||||
try {
|
||||
return reset ? reset(printer, flociAzDir, cloud) : setup(printer, flociAzDir, cloud);
|
||||
|
||||
@@ -28,7 +28,7 @@ public class ConfigDefaultProductCommand implements Callable<Integer> {
|
||||
Printer printer = global.printer();
|
||||
if (!"aws".equals(product) && !"az".equals(product) && !"gcp".equals(product) && !"oci".equals(product)) {
|
||||
printer.error("Unknown product '" + product + "'.\nUse one of: aws, az, gcp, oci.");
|
||||
return 1;
|
||||
return ExitCode.USAGE; // a value the command cannot accept, like any other bad argument
|
||||
}
|
||||
GlobalConfigStore store = new GlobalConfigStore();
|
||||
try {
|
||||
|
||||
@@ -100,7 +100,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
case "delete" -> deleteProfile(printer, store);
|
||||
default -> {
|
||||
printer.error("Unknown action '" + action + "'.\nUse one of: list, show, create, edit, delete.");
|
||||
yield 1;
|
||||
yield ExitCode.USAGE;
|
||||
}
|
||||
};
|
||||
} catch (IllegalArgumentException e) {
|
||||
@@ -157,7 +157,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
}
|
||||
|
||||
private int showProfile(Printer printer, ProfileStore store) {
|
||||
if (name == null) { printer.error(nameRequired()); return 1; }
|
||||
if (name == null) { printer.error(nameRequired()); return ExitCode.USAGE; }
|
||||
try {
|
||||
var profile = store.get(name);
|
||||
if (profile.isEmpty()) {
|
||||
@@ -187,7 +187,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
* not created here, since {@code create} is what writes the product's defaults into it.
|
||||
*/
|
||||
private int editProfile(Printer printer, ProfileStore store) {
|
||||
if (name == null) { printer.error(nameRequired()); return 1; }
|
||||
if (name == null) { printer.error(nameRequired()); return ExitCode.USAGE; }
|
||||
String prefix = this.profile.commandPrefix() + " config profile ";
|
||||
// Asked of the file, not of its contents: a profile that no longer parses is exactly the
|
||||
// one that has to be opened to be repaired.
|
||||
@@ -299,7 +299,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
}
|
||||
|
||||
private int createProfile(Printer printer, ProfileStore store) {
|
||||
if (name == null) { printer.error(nameRequired()); return 1; }
|
||||
if (name == null) { printer.error(nameRequired()); return ExitCode.USAGE; }
|
||||
try {
|
||||
if (store.get(name).isPresent()) {
|
||||
printer.error("Profile '" + name + "' already exists.\nDelete it first, or edit " + store.existingFile(name) + ".");
|
||||
@@ -308,7 +308,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
if (port != null && (port < 1 || port > 65535)) {
|
||||
printer.error("--port must be between 1 and 65535, but was " + port + ".\n"
|
||||
+ "Pick a free port, for example " + (profile.defaultPort() + 10000) + ".");
|
||||
return 1;
|
||||
return ExitCode.USAGE;
|
||||
}
|
||||
Profile p = new Profile(profile, name);
|
||||
// Only flags the user typed: the mixin's own values may come from FLOCI_* env vars, and
|
||||
@@ -388,7 +388,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
}
|
||||
|
||||
private int deleteProfile(Printer printer, ProfileStore store) {
|
||||
if (name == null) { printer.error(nameRequired()); return 1; }
|
||||
if (name == null) { printer.error(nameRequired()); return ExitCode.USAGE; }
|
||||
try {
|
||||
List<Path> removed = store.delete(name);
|
||||
if (removed.isEmpty()) {
|
||||
|
||||
@@ -38,6 +38,11 @@ public class SnapshotDeleteCommand implements Callable<Integer> {
|
||||
@Override
|
||||
public Integer call() {
|
||||
Printer printer = global.printer();
|
||||
String invalidName = FlociHttpClient.snapshotNameError(name);
|
||||
if (invalidName != null) {
|
||||
printer.error(invalidName);
|
||||
return ExitCode.USAGE;
|
||||
}
|
||||
// Resolve the endpoint from the container's port mapping, like status/services/wait do,
|
||||
// so a container started with --port <n> is still found without --endpoint.
|
||||
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
||||
|
||||
@@ -38,6 +38,11 @@ public class SnapshotLoadCommand implements Callable<Integer> {
|
||||
@Override
|
||||
public Integer call() {
|
||||
Printer printer = global.printer();
|
||||
String invalidName = FlociHttpClient.snapshotNameError(name);
|
||||
if (invalidName != null) {
|
||||
printer.error(invalidName);
|
||||
return ExitCode.USAGE;
|
||||
}
|
||||
// Resolve the endpoint from the container's port mapping, like status/services/wait do,
|
||||
// so a container started with --port <n> is still found without --endpoint.
|
||||
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
||||
|
||||
@@ -41,6 +41,11 @@ public class SnapshotSaveCommand implements Callable<Integer> {
|
||||
@Override
|
||||
public Integer call() {
|
||||
Printer printer = global.printer();
|
||||
String invalidName = FlociHttpClient.snapshotNameError(name);
|
||||
if (invalidName != null) {
|
||||
printer.error(invalidName);
|
||||
return ExitCode.USAGE;
|
||||
}
|
||||
// Resolve the endpoint from the container's port mapping, like status/services/wait do,
|
||||
// so a container started with --port <n> is still found without --endpoint.
|
||||
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
||||
|
||||
@@ -313,12 +313,22 @@ public class FlociHttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Why {@code name} cannot be a snapshot name, or {@code null}. Commands ask before they send
|
||||
* anything, so a bad name is a usage error rather than a failed request.
|
||||
*/
|
||||
public static String snapshotNameError(String name) {
|
||||
if (name == null || name.isBlank() || name.equals(".") || name.equals("..") || name.contains("/")) {
|
||||
return "Invalid snapshot name '" + name + "'.\n"
|
||||
+ "Use a name that is not '.' or '..' and has no '/'.";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** A snapshot name as one URL path segment; names that could change the path are refused. */
|
||||
static String snapshotSegment(String name) throws FlociException {
|
||||
if (name == null || name.isBlank() || name.equals(".") || name.equals("..") || name.contains("/")) {
|
||||
throw new FlociException("Invalid snapshot name '" + name + "'.\n"
|
||||
+ "Use a name that is not '.' or '..' and has no '/'.");
|
||||
}
|
||||
String invalid = snapshotNameError(name);
|
||||
if (invalid != null) throw new FlociException(invalid);
|
||||
return URLEncoder.encode(name, StandardCharsets.UTF_8).replace("+", "%20");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user