diff --git a/src/main/java/io/floci/cli/FlociCli.java b/src/main/java/io/floci/cli/FlociCli.java index b712c08..dee05f5 100644 --- a/src/main/java/io/floci/cli/FlociCli.java +++ b/src/main/java/io/floci/cli/FlociCli.java @@ -81,6 +81,11 @@ public class FlociCli implements Runnable { if (colorDisabled(args, System.getenv("NO_COLOR") != null, System.console() != null)) { Ansi.disable(); } + // Decided here too, not only when a command builds its printer: the update notice below + // is printed before any command runs. + if (plainGlyphs(args, System.getenv("NO_COLOR") != null, System.console() != null)) { + Glyphs.plain(); + } // If no explicit product subcommand, route bare commands to the configured default. // "floci az start" and "floci aws start" always win; only bare "floci start" is affected. @@ -155,6 +160,13 @@ public class FlociCli implements Runnable { * arguments here as well as by {@code GlobalOptions.printer()}: a parse error is printed by the * exception handlers before any command, and so any printer, exists. */ + /** Whether marks are plain ASCII for this invocation, read from the raw arguments like {@link #colorDisabled}. */ + public static boolean plainGlyphs(String[] args, boolean noColorEnv, boolean interactive) { + boolean flag = Arrays.stream(args) + .anyMatch(a -> a.equals("--no-emoji") || a.equalsIgnoreCase("--no-emoji=true")); + return GlobalOptions.plainGlyphs(flag, interactive, noColorEnv); + } + public static boolean colorDisabled(String[] args, boolean noColorEnv, boolean interactive) { return noColorEnv || !interactive || Arrays.stream(args) .anyMatch(a -> a.equals("--no-color") || a.equalsIgnoreCase("--no-color=true")); diff --git a/src/main/java/io/floci/cli/ServerCompatibility.java b/src/main/java/io/floci/cli/ServerCompatibility.java index b08e51e..e951de3 100644 --- a/src/main/java/io/floci/cli/ServerCompatibility.java +++ b/src/main/java/io/floci/cli/ServerCompatibility.java @@ -15,15 +15,21 @@ public final class ServerCompatibility { * when there is nothing to say. Only the AWS emulator has a minimum: the others are versioned * on their own lines (0.x), where the same number would flag every release. A version that * does not read as one ("unknown", a development build) is not judged. + * + * @param instanceSelector the flags that select the instance the version came from + * ({@link GlobalOptions#instanceSelector()}), so the hint acts on that + * instance and not on the default one */ - public static Optional warning(ProductProfile product, String serverVersion) { + public static Optional warning(ProductProfile product, String serverVersion, String instanceSelector) { if (!"aws".equals(product.name()) || !Version.isValid(serverVersion) || ImageVersionCheck.meetsMinimum(serverVersion, ImageVersionCheck.MIN_VERSION)) { return Optional.empty(); } + // restart, not stop and start: it brings the instance back with the port, data directory + // and services it has now. return Optional.of(product.displayName() + " server " + serverVersion + " is older than " + ImageVersionCheck.MIN_VERSION + ", the oldest version this CLI supports; some commands may not work." - + "\nRun '" + product.commandPrefix() + " stop --remove' and then '" + product.commandPrefix() - + " start --pull always' to run a newer image."); + + "\nPull a newer image ('docker pull " + product.defaultImageRef() + "'), then run '" + + product.commandPrefix() + " restart" + instanceSelector + "'."); } } diff --git a/src/main/java/io/floci/cli/commands/StatusCommand.java b/src/main/java/io/floci/cli/commands/StatusCommand.java index fe289c1..7e155f2 100644 --- a/src/main/java/io/floci/cli/commands/StatusCommand.java +++ b/src/main/java/io/floci/cli/commands/StatusCommand.java @@ -71,7 +71,7 @@ public class StatusCommand implements Callable { reachable = true; } catch (Exception ignored) {} // On stderr, so structured output stays one document. - ServerCompatibility.warning(profile, serverVersion).ifPresent(printer::warn); + ServerCompatibility.warning(profile, serverVersion, global.instanceSelector()).ifPresent(printer::warn); Map data = new LinkedHashMap<>(); data.put("container", global.container); diff --git a/src/main/java/io/floci/cli/commands/VersionCommand.java b/src/main/java/io/floci/cli/commands/VersionCommand.java index 98cdd08..b8f4000 100644 --- a/src/main/java/io/floci/cli/commands/VersionCommand.java +++ b/src/main/java/io/floci/cli/commands/VersionCommand.java @@ -54,7 +54,7 @@ public class VersionCommand implements Callable { serverEdition = info.edition(); } catch (Exception ignored) {} // On stderr, so structured output stays one document. - ServerCompatibility.warning(profile, serverVersion).ifPresent(printer::warn); + ServerCompatibility.warning(profile, serverVersion, global.instanceSelector()).ifPresent(printer::warn); // Try to read image digest from Docker (best-effort) try { 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 f086c60..92a3ebd 100644 --- a/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java +++ b/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java @@ -245,13 +245,55 @@ public class ConfigProfileCommand implements Callable { /** * The editor to run, as a command and its arguments: {@code VISUAL} first, then - * {@code EDITOR}, as other tools do. A value like {@code "code --wait"} carries arguments, so - * it is split on whitespace. Empty when neither is set. + * {@code EDITOR}, as other tools do. Empty when neither is set. */ public static List editorCommand(String visual, String editor) { String chosen = visual != null && !visual.isBlank() ? visual : editor; if (chosen == null || chosen.isBlank()) return List.of(); - return List.of(chosen.strip().split("\\s+")); + return words(chosen); + } + + /** + * {@code line} split into words the way a shell would for a simple command: whitespace + * separates, single and double quotes group (a path with spaces), and a backslash outside + * single quotes takes the next character literally. Nothing is expanded; the words go + * straight to the process, never through a shell. + */ + static List words(String line) { + List words = new ArrayList<>(); + StringBuilder word = new StringBuilder(); + boolean inWord = false; + char quote = 0; + for (int i = 0; i < line.length(); i++) { + char c = line.charAt(i); + if (quote != 0) { + if (c == quote) { + quote = 0; + } else if (c == '\\' && quote == '"' && i + 1 < line.length() + && (line.charAt(i + 1) == '"' || line.charAt(i + 1) == '\\')) { + word.append(line.charAt(++i)); + } else { + word.append(c); + } + } else if (c == '\'' || c == '"') { + quote = c; + inWord = true; + } else if (c == '\\' && i + 1 < line.length()) { + word.append(line.charAt(++i)); + inWord = true; + } else if (Character.isWhitespace(c)) { + if (inWord) { + words.add(word.toString()); + word.setLength(0); + inWord = false; + } + } else { + word.append(c); + inWord = true; + } + } + if (inWord) words.add(word.toString()); + return List.copyOf(words); } private int createProfile(Printer printer, ProfileStore store) { diff --git a/src/test/java/io/floci/cli/unit/ErgonomicsTest.java b/src/test/java/io/floci/cli/unit/ErgonomicsTest.java index 58c9878..b108832 100644 --- a/src/test/java/io/floci/cli/unit/ErgonomicsTest.java +++ b/src/test/java/io/floci/cli/unit/ErgonomicsTest.java @@ -95,6 +95,14 @@ class ErgonomicsTest { assertEquals(plain, GlobalOptions.plainGlyphs(noEmoji, terminal, noColorEnv)); } + /** The update notice is printed before any command runs, so the choice is made from the raw arguments too. */ + @ParameterizedTest + @CsvSource({"'status --no-emoji', false, true, true", "'status', false, true, false", "'status', true, true, true", + "'status', false, false, true", "'--no-emoji=true status', false, true, true"}) + void theMarksAreChosenBeforeAnyCommandRuns(String commandLine, boolean noColorEnv, boolean interactive, boolean plain) { + assertEquals(plain, FlociCli.plainGlyphs(commandLine.split(" "), noColorEnv, interactive)); + } + @Test void thePlainSetIsAsciiOnly() { for (String mark : List.of(Glyphs.ASCII.ok(), Glyphs.ASCII.warn(), Glyphs.ASCII.fail(), Glyphs.ASCII.arrow())) { @@ -168,6 +176,36 @@ class ErgonomicsTest { assertEquals("--wait " + store().existingFile("dev"), Files.readString(args).strip()); } + /** A quoted path with spaces is one word, as a shell would read it; nothing else is interpreted. */ + @ParameterizedTest + @CsvSource(delimiter = '|', quoteCharacter = '`', value = { + "\"/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code\" --wait | /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code;--wait", + "'/opt/my editor/bin/edit' -n | /opt/my editor/bin/edit;-n", + "/opt/my\\ editor/bin/edit | /opt/my editor/bin/edit", + "emacsclient -c -a \"emacs --no-splash\" | emacsclient;-c;-a;emacs --no-splash", + "vim -c 'set ft=yaml' | vim;-c;set ft=yaml", + "code --wait | code;--wait", + "nano --rcfile=\"my rc\" | nano;--rcfile=my rc", + "echo \"a \\\"quoted\\\" word\" | echo;a \"quoted\" word", + "ed '' | ed;"}) + void anEditorValueIsSplitLikeAShellWould(String value, String expected) { + assertEquals(List.of(expected.split(";", -1)), ConfigProfileCommand.editorCommand(value, null)); + } + + @Test + @DisabledOnOs(OS.WINDOWS) + void anEditorInADirectoryWithSpacesIsRun() throws Exception { + store().save(new Profile(ProductProfile.AWS, "dev")); + Path dir = Files.createDirectories(tempDir.resolve("my editors")); + Path editor = Files.writeString(dir.resolve("edit it"), "#!/bin/sh\necho 'port: 14566' >> \"$2\"\n"); + assertTrue(editor.toFile().setExecutable(true)); + + Run r = edit(Map.of("EDITOR", "\"" + editor + "\" --wait"), "dev"); + + assertEquals(0, r.exit(), r.err()); + assertEquals(14566, store().get("dev").orElseThrow().port); + } + @Test void theEditorIsVisualThenEditorSplitOnSpaces() { assertEquals(List.of("code", "--wait"), ConfigProfileCommand.editorCommand("code --wait", "vi")); @@ -230,25 +268,35 @@ class ErgonomicsTest { @ParameterizedTest @ValueSource(strings = {"1.4.9", "1.0.0", "0.9.0"}) void anAwsServerBelowTheMinimumIsWarnedAbout(String version) { - String warning = ServerCompatibility.warning(ProductProfile.AWS, version).orElseThrow(); + String warning = ServerCompatibility.warning(ProductProfile.AWS, version, "").orElseThrow(); assertTrue(warning.contains("server " + version + " is older than 1.5.0"), warning); - assertTrue(warning.contains("floci start --pull always"), warning); + assertTrue(warning.contains("docker pull floci/floci:latest"), warning); + assertTrue(warning.endsWith("then run 'floci restart'."), warning); } @ParameterizedTest @ValueSource(strings = {"1.5.0", "2.2.0", "unknown", "unavailable", "", "dev-SNAPSHOT"}) void aSupportedOrUnreadableVersionIsNotWarnedAbout(String version) { - assertTrue(ServerCompatibility.warning(ProductProfile.AWS, version).isEmpty(), version); + assertTrue(ServerCompatibility.warning(ProductProfile.AWS, version, "").isEmpty(), version); } /** The other emulators are on 0.x version lines of their own: the AWS minimum says nothing about them. */ @Test void theOtherProductsHaveNoMinimum() { for (ProductProfile product : List.of(ProductProfile.GCP, ProductProfile.AZ, ProductProfile.OCI)) { - assertTrue(ServerCompatibility.warning(product, "0.4.2").isEmpty(), product.name()); + assertTrue(ServerCompatibility.warning(product, "0.4.2", "").isEmpty(), product.name()); } - assertTrue(ServerCompatibility.warning(ProductProfile.AWS, null).isEmpty()); + assertTrue(ServerCompatibility.warning(ProductProfile.AWS, null, "").isEmpty()); + } + + /** The hint acts on the instance the version came from, and keeps that instance's settings. */ + @Test + void theHintSelectsTheSameInstance() { + String warning = ServerCompatibility.warning(ProductProfile.AWS, "1.4.0", " --profile dev").orElseThrow(); + + assertTrue(warning.endsWith("then run 'floci restart --profile dev'."), warning); + assertFalse(warning.contains("stop --remove"), warning); } @Test @@ -268,6 +316,7 @@ class ErgonomicsTest { "--endpoint", "http://127.0.0.1:" + server.getAddress().getPort()); assertTrue(r.err().contains("server 1.4.0 is older than 1.5.0"), r.err()); + assertTrue(r.err().contains("floci restart --container floci-ergonomics-none"), r.err()); assertTrue(r.out().strip().startsWith("{") && r.out().strip().endsWith("}"), r.out()); assertFalse(r.out().contains("older than"), r.out()); } finally {