fix(cli): keep values in env load hints from running as shell code

The hint is a comment in the output people feed to eval, so a line break in a selector value ended the comment and ran the rest. Such a value now prints as a placeholder. Fish and PowerShell always get quotes, because the plain-word rule is POSIX and @team is a splat in PowerShell. Addresses the review of #74.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-11-04 01:45:09 -06:00
1 parent 8c46a546b7
commit b8b58ca59e
3 files changed
+58 -1

No files matched your search

+10 -1
View File
@@ -113,9 +113,18 @@ public class GlobalOptions {
* The flags for a hint that loads {@code env} in {@code shell}: the selector, quoted for that
* shell, and {@code -o text} after a profile, which may set {@code output: json} that no shell
* can load.
*
* <p>This hint is a comment in the output people feed to {@code eval}, so a line break in a
* value would end the comment and run the rest as code. No quoting survives that: such a value
* becomes a placeholder. PLAIN_WORD is a POSIX rule ({@code @team} is a splat in PowerShell),
* so other shells always get quotes.
*/
public String envLoadFlags(String shell) {
String selector = selector(value -> PLAIN_WORD.matcher(value).matches() ? value : ShellExport.quoted(shell, value));
String selector = selector(value -> {
if (ShellExport.breaksLine(value)) return "<value>";
return ShellExport.isPosix(shell) && PLAIN_WORD.matcher(value).matches()
? value : ShellExport.quoted(shell, value);
});
return profile != null ? selector + " -o text" : selector;
}
@@ -39,6 +39,21 @@ public final class ShellExport {
};
}
/** Whether {@code shell} is one of the POSIX family, as opposed to fish or PowerShell. */
public static boolean isPosix(String shell) {
return switch (shell.toLowerCase()) {
case "fish", "powershell", "pwsh", "ps1" -> false;
default -> true;
};
}
/** Whether {@code value} could end the comment line it is printed in, in any shell. */
public static boolean breaksLine(String value) {
return value.chars().anyMatch(c -> Character.isISOControl(c)
|| Character.getType(c) == Character.LINE_SEPARATOR
|| Character.getType(c) == Character.PARAGRAPH_SEPARATOR);
}
/** {@code value} as one single-quoted argument in the given shell. */
public static String quoted(String shell, String value) {
return switch (shell.toLowerCase()) {