diff --git a/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java b/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java index 2b3c9a3..dd8f1ea 100644 --- a/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java +++ b/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java @@ -12,6 +12,7 @@ import picocli.CommandLine.*; import picocli.CommandLine.Model.CommandSpec; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Comparator; @@ -187,16 +188,12 @@ public class ConfigProfileCommand implements Callable { private int editProfile(Printer printer, ProfileStore store) { if (name == null) { printer.error(nameRequired()); return 1; } String prefix = this.profile.commandPrefix() + " config profile "; - Path file; - try { - if (store.get(name).isEmpty()) { - printer.error("Profile '" + name + "' not found.\nRun '" + prefix + "create " + name + "' to create it first."); - return 1; - } - file = store.existingFile(name); - } catch (IOException e) { - // Unreadable as it stands: opening it is exactly how the user fixes that. - file = store.existingFile(name); + // 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. + Path file = store.existingFile(name); + if (!Files.exists(file)) { + printer.error("Profile '" + name + "' not found.\nRun '" + prefix + "create " + name + "' to create it first."); + return 1; } List editor = editorCommand(env.apply("VISUAL"), env.apply("EDITOR")); diff --git a/src/main/java/io/floci/cli/config/ProfileStore.java b/src/main/java/io/floci/cli/config/ProfileStore.java index a419605..0db2682 100644 --- a/src/main/java/io/floci/cli/config/ProfileStore.java +++ b/src/main/java/io/floci/cli/config/ProfileStore.java @@ -98,6 +98,10 @@ public class ProfileStore { // 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); + // 'null' and '~' are valid YAML for "nothing": a document, but not a profile. + if (profile == null) { + throw new IOException(file + " holds no settings: it has to be a mapping, like 'port: 4566'"); + } // Hand-written files often omit 'name:'; the file name is the name. if (profile.name == null || profile.name.isBlank()) { profile.name = nameOf(file); diff --git a/src/test/java/io/floci/cli/unit/ErgonomicsTest.java b/src/test/java/io/floci/cli/unit/ErgonomicsTest.java index 02dd400..66b4da7 100644 --- a/src/test/java/io/floci/cli/unit/ErgonomicsTest.java +++ b/src/test/java/io/floci/cli/unit/ErgonomicsTest.java @@ -252,6 +252,37 @@ class ErgonomicsTest { assertTrue(r.err().contains("floci config profile edit dev"), r.err()); } + /** A profile that does not parse, or holds YAML's "nothing", can still be opened and repaired. */ + @ParameterizedTest + @ValueSource(strings = {"port: [unterminated\n", "null\n", "~\n", "- a list\n"}) + @DisabledOnOs(OS.WINDOWS) + void aBrokenProfileCanBeOpenedAndRepaired(String broken) throws Exception { + Path file = Files.createDirectories(tempDir.resolve("profiles")).resolve("dev.yaml"); + Files.writeString(file, broken); + Path editor = editorThat("echo 'port: 14566' > \"$1\""); + + Run r = edit(Map.of("EDITOR", editor.toString()), "dev"); + + assertEquals(0, r.exit(), r.err()); + assertEquals(14566, store().get("dev").orElseThrow().port); + } + + /** Saving YAML's "nothing" is reported like any other file that is no longer a profile. */ + @ParameterizedTest + @ValueSource(strings = {"null", "~"}) + @DisabledOnOs(OS.WINDOWS) + void anEditThatLeavesNothingIsReported(String nothing) throws Exception { + store().save(new Profile(ProductProfile.AWS, "dev")); + Path editor = editorThat("echo '" + nothing + "' > \"$1\""); + + Run r = edit(Map.of("EDITOR", editor.toString()), "dev"); + + assertEquals(1, r.exit()); + assertTrue(r.err().contains("Profile 'dev' is no longer valid"), r.err()); + assertTrue(r.err().contains("holds no settings"), r.err()); + assertFalse(r.err().contains("Exception"), r.err()); + } + @Test @DisabledOnOs(OS.WINDOWS) void anEditorThatFailsIsReported() throws Exception {