fix(profile): make profile names, files and exit codes consistent (#33)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-08 22:21:02 -05:00
1 parent bf9ee4037b
commit 5c0cc339bf
9 files changed
+263 -44

No files matched your search

@@ -2,6 +2,7 @@ package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.config.ProfileNotFoundException;
import io.floci.cli.config.ProfileStore;
import io.floci.cli.output.Printer;
import picocli.CommandLine.*;
@@ -44,7 +45,15 @@ public class RestartCommand implements Callable<Integer> {
// Resolve the profile BEFORE stopping anything. A failure here must not leave the
// container stopped, and the values must not be re-read from a file that could change
// during the stop plus the one second wait below.
StartCommand start = buildStartCommand();
StartCommand start;
try {
start = buildStartCommand();
} catch (ProfileNotFoundException e) {
// Gone or broken since the parse resolved it: nothing has been stopped yet, and the
// exit code matches a bad --profile at parse time.
printer.error(e.getMessage());
return ExitCode.USAGE;
}
String invalid = start.validationError();
if (invalid != null) {
printer.error(invalid);
@@ -11,6 +11,7 @@ import picocli.CommandLine.*;
import picocli.CommandLine.Model.CommandSpec;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
@@ -78,16 +79,23 @@ public class ConfigProfileCommand implements Callable<Integer> {
public Integer call() {
Printer printer = global.printer();
return switch (action.toLowerCase()) {
case "list" -> listProfiles(printer, store);
case "show" -> showProfile(printer, store);
case "create" -> createProfile(printer, store);
case "delete" -> deleteProfile(printer, store);
default -> {
printer.error("Unknown action '" + action + "'. Use: list, show, create, delete");
yield 1;
}
};
try {
return switch (action.toLowerCase()) {
case "list" -> listProfiles(printer, store);
case "show" -> showProfile(printer, store);
case "create" -> createProfile(printer, store);
case "delete" -> deleteProfile(printer, store);
default -> {
printer.error("Unknown action '" + action + "'. Use: list, show, create, delete");
yield 1;
}
};
} catch (IllegalArgumentException e) {
// An invalid profile name: the same cause exits 2 from --profile, so it does here too.
// The message already ends with the next step.
printer.error(e.getMessage());
return ExitCode.USAGE;
}
}
private int listProfiles(Printer printer, ProfileStore store) {
@@ -159,7 +167,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
if (name == null) { printer.error("Profile name required."); return 1; }
try {
if (store.get(name).isPresent()) {
printer.error("Profile '" + name + "' already exists. Delete it first or edit " + store.profileFile(name));
printer.error("Profile '" + name + "' already exists. Delete it first or edit " + store.existingFile(name));
return 1;
}
if (port != null && (port < 1 || port > 65535)) {
@@ -247,11 +255,15 @@ public class ConfigProfileCommand implements Callable<Integer> {
private int deleteProfile(Printer printer, ProfileStore store) {
if (name == null) { printer.error("Profile name required."); return 1; }
try {
if (!store.delete(name)) {
printer.error("Profile '" + name + "' not found.");
List<Path> removed = store.delete(name);
if (removed.isEmpty()) {
printer.error("Profile '" + name + "' not found.\nRun '" + profile.commandPrefix() + " config profile list' to see available profiles.");
return 1;
}
printer.println(Ansi.green("Deleted") + " profile '" + name + "'");
String files = removed.size() > 1
? " (" + removed.get(0).getFileName() + " and " + removed.get(1).getFileName() + ")"
: "";
printer.println(Ansi.green("Deleted") + " profile '" + name + "'" + files);
return 0;
} catch (IOException e) {
printer.error("Could not delete profile: " + e.getMessage());
@@ -5,6 +5,7 @@ import io.floci.cli.ProductProfile;
import io.floci.cli.commands.StartCommand;
import io.floci.cli.config.Profile;
import io.floci.cli.config.ProfileDefaultValueProvider;
import io.floci.cli.config.ProfileNotFoundException;
import io.floci.cli.config.ProfileStore;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
@@ -59,7 +60,14 @@ public class ConfigShowCommand implements Callable<Integer> {
// image/port/persistDir/services have no global option, so they are read back from the
// profile itself — they only take effect on 'start'.
addStartSettings(data);
try {
addStartSettings(data);
} catch (ProfileNotFoundException e) {
// The profile went away (or broke) after the parse resolved it. Same cause, same exit
// code as a bad --profile at parse time.
printer.error(e.getMessage());
return ExitCode.USAGE;
}
if (printer.format() != OutputFormat.text) {
printer.structured(data);