mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(profile): make profile names, files and exit codes consistent (#33)
This commit is contained in:
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);
|
||||
|
||||
@@ -128,6 +128,6 @@ public class ProfileDefaultValueProvider implements IDefaultValueProvider {
|
||||
private ProfileNotFoundException invalid(CommandLine commandLine, String name, String detail) {
|
||||
return new ProfileNotFoundException(commandLine,
|
||||
"Profile '" + name + "' is invalid: " + detail + "\n"
|
||||
+ "Edit " + store.profileFile(name) + " and try again.");
|
||||
+ "Edit " + store.existingFile(name) + " and try again.");
|
||||
}
|
||||
}
|
||||
@@ -64,15 +64,11 @@ public class ProfileStore {
|
||||
List<Profile> profiles = new ArrayList<>();
|
||||
try (var stream = Files.list(profilesDir)) {
|
||||
stream.filter(p -> p.toString().endsWith(".yaml") || p.toString().endsWith(".yml"))
|
||||
// A file no command can address (".yaml" has an empty name) is not a profile.
|
||||
.filter(p -> isUsableName(nameOf(p)))
|
||||
.forEach(p -> {
|
||||
try {
|
||||
Profile profile = YAML.readValue(p.toFile(), Profile.class);
|
||||
// Hand-written files often omit 'name:'; the file name is the name.
|
||||
if (profile.name == null || profile.name.isBlank()) {
|
||||
String file = p.getFileName().toString();
|
||||
profile.name = file.substring(0, file.lastIndexOf('.'));
|
||||
}
|
||||
profiles.add(profile);
|
||||
profiles.add(read(p));
|
||||
} catch (IOException ignored) {}
|
||||
});
|
||||
}
|
||||
@@ -80,9 +76,33 @@ public class ProfileStore {
|
||||
}
|
||||
|
||||
public Optional<Profile> get(String name) throws IOException {
|
||||
Path file = existingProfileFile(name);
|
||||
Path file = existingFile(name);
|
||||
if (!Files.exists(file)) return Optional.empty();
|
||||
return Optional.of(YAML.readValue(file.toFile(), Profile.class));
|
||||
return Optional.of(read(file));
|
||||
}
|
||||
|
||||
// The one read path, so list and show agree on a profile's name.
|
||||
private static Profile read(Path file) throws IOException {
|
||||
Profile profile = YAML.readValue(file.toFile(), Profile.class);
|
||||
// Hand-written files often omit 'name:'; the file name is the name.
|
||||
if (profile.name == null || profile.name.isBlank()) {
|
||||
profile.name = nameOf(file);
|
||||
}
|
||||
return profile;
|
||||
}
|
||||
|
||||
private static boolean isUsableName(String name) {
|
||||
try {
|
||||
validateName(name);
|
||||
return true;
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static String nameOf(Path file) {
|
||||
String fileName = file.getFileName().toString();
|
||||
return fileName.substring(0, fileName.lastIndexOf('.'));
|
||||
}
|
||||
|
||||
public void save(Profile profile) throws IOException {
|
||||
@@ -90,43 +110,59 @@ public class ProfileStore {
|
||||
YAML.writeValue(profileFile(profile.name).toFile(), profile);
|
||||
}
|
||||
|
||||
public boolean delete(String name) throws IOException {
|
||||
Path file = existingProfileFile(name);
|
||||
return Files.deleteIfExists(file);
|
||||
/**
|
||||
* Deletes every spelling of {@code name}. Removing only the one reads prefer would leave the
|
||||
* other answering {@code --profile} after a delete that reported success.
|
||||
*
|
||||
* @return the files removed: 0 when the profile did not exist
|
||||
*/
|
||||
public List<Path> delete(String name) throws IOException {
|
||||
List<Path> removed = new ArrayList<>();
|
||||
for (Path file : List.of(profileFile(name), resolveInProfilesDir(validateName(name), ".yml"))) {
|
||||
if (Files.deleteIfExists(file)) removed.add(file);
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
/** Where {@link #save} writes {@code name}. Always {@code .yaml}. */
|
||||
public Path profileFile(String name) {
|
||||
return resolveInProfilesDir(validateName(name) + ".yaml");
|
||||
return resolveInProfilesDir(validateName(name), ".yaml");
|
||||
}
|
||||
|
||||
// The traversal guarantee: whatever the name looks like, the file it resolves to must sit
|
||||
// directly in profilesDir. Also the place a name that is illegal on this platform but legal
|
||||
// on another (a colon on Windows) turns into a clean message instead of InvalidPathException.
|
||||
private Path resolveInProfilesDir(String fileName) {
|
||||
// Messages quote the name as the user typed it, not the file name with its suffix.
|
||||
private Path resolveInProfilesDir(String name, String suffix) {
|
||||
Path dir = profilesDir.toAbsolutePath().normalize();
|
||||
Path resolved;
|
||||
try {
|
||||
resolved = dir.resolve(fileName).normalize();
|
||||
resolved = dir.resolve(name + suffix).normalize();
|
||||
} catch (InvalidPathException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid profile name '" + fileName + "': not a valid file name on this platform.\n"
|
||||
"Invalid profile name '" + name + "': not a valid file name on this platform.\n"
|
||||
+ "Run 'floci config profile list' to see available profiles.");
|
||||
}
|
||||
if (!dir.equals(resolved.getParent())) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid profile name '" + fileName + "': it would resolve outside " + profilesDir + ".\n"
|
||||
"Invalid profile name '" + name + "': it would resolve outside " + profilesDir + ".\n"
|
||||
+ "Run 'floci config profile list' to see available profiles.");
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
||||
// list() has always accepted .yml, so reads must too — otherwise a .yml profile shows up in
|
||||
// 'config profile list' and then reports "not found" when passed to --profile.
|
||||
private Path existingProfileFile(String name) {
|
||||
/**
|
||||
* The file {@code name} is read from: its {@code .yaml}, else its {@code .yml}, else the
|
||||
* {@code .yaml} path {@link #save} would create. Messages that tell the user which file to
|
||||
* edit name this one, not {@link #profileFile}.
|
||||
*
|
||||
* <p>list() has always accepted .yml, so reads must too — otherwise a .yml profile shows up in
|
||||
* 'config profile list' and then reports "not found" when passed to --profile.
|
||||
*/
|
||||
public Path existingFile(String name) {
|
||||
Path yaml = profileFile(name);
|
||||
if (Files.exists(yaml)) return yaml;
|
||||
Path yml = resolveInProfilesDir(validateName(name) + ".yml");
|
||||
Path yml = resolveInProfilesDir(validateName(name), ".yml");
|
||||
return Files.exists(yml) ? yml : yaml;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user