fix(cli): make env load hints loadable with a JSON profile and in every shell

A profile can set output: json, so the hint now adds -o text after --profile; pasted without it, the shell received JSON. Selector values are quoted for the shell the hint is for, since PowerShell and fish do not read the POSIX form. Addresses the review of #74.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-11-04 00:38:42 -06:00
1 parent b0d4a4eb31
commit a65bc95e41
7 files changed
+62 -10

No files matched your search

+19 -4
View File
@@ -5,6 +5,7 @@ import io.floci.cli.output.Ansi;
import io.floci.cli.output.Glyphs;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import io.floci.cli.output.ShellExport;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;
@@ -105,15 +106,29 @@ public class GlobalOptions {
* empty. Values are shell-quoted, because hints are meant to be pasted.
*/
public String instanceSelector() {
return selector(GlobalOptions::shellWord);
}
/**
* 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.
*/
public String envLoadFlags(String shell) {
String selector = selector(value -> PLAIN_WORD.matcher(value).matches() ? value : ShellExport.quoted(shell, value));
return profile != null ? selector + " -o text" : selector;
}
private String selector(UnaryOperator<String> quote) {
String selector = "";
if (profile != null) {
selector = " --profile " + shellWord(profile);
if (typed("--container")) selector += " --container " + shellWord(container);
selector = " --profile " + quote.apply(profile);
if (typed("--container")) selector += " --container " + quote.apply(container);
} else if (!product.defaultContainer().equals(container)) {
selector = " --container " + shellWord(container);
selector = " --container " + quote.apply(container);
}
if (typed("--endpoint") || (profile == null && !product.defaultEndpoint().equals(endpoint))) {
selector += " --endpoint " + shellWord(endpoint);
selector += " --endpoint " + quote.apply(endpoint);
}
return selector;
}
@@ -73,7 +73,7 @@ public class EnvCommand implements Callable<Integer> {
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
}
printer.println("");
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci env" + global.instanceSelector())));
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci env" + global.envLoadFlags(shell))));
return 0;
}
@@ -127,7 +127,7 @@ public class AzEnvCommand implements Callable<Integer> {
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_CONNECTION_STRING", connStr));
printAzCliVars(printer, azCliVars);
printer.println("");
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci az env" + global.instanceSelector())));
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci az env" + global.envLoadFlags(shell))));
} else {
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_ACCOUNT", account));
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_KEY", DEV_KEY));
@@ -136,7 +136,7 @@ public class AzEnvCommand implements Callable<Integer> {
}
printAzCliVars(printer, azCliVars);
printer.println("");
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci az env --format sdk-vars" + global.instanceSelector())));
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci az env --format sdk-vars" + global.envLoadFlags(shell))));
}
return 0;
@@ -77,7 +77,7 @@ public class GcpEnvCommand implements Callable<Integer> {
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
}
printer.println("");
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci gcp env" + global.instanceSelector())));
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci gcp env" + global.envLoadFlags(shell))));
return 0;
}
@@ -73,7 +73,7 @@ public class OciEnvCommand implements Callable<Integer> {
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
}
printer.println("");
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci oci env" + global.instanceSelector())));
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci oci env" + global.envLoadFlags(shell))));
return 0;
}
}
@@ -39,6 +39,15 @@ public final class ShellExport {
};
}
/** {@code value} as one single-quoted argument in the given shell. */
public static String quoted(String shell, String value) {
return switch (shell.toLowerCase()) {
case "fish" -> "'" + value.replace("\\", "\\\\").replace("'", "\\'") + "'";
case "powershell", "pwsh", "ps1" -> "'" + value.replace("'", "''") + "'";
default -> "'" + value.replace("'", "'\\''") + "'";
};
}
/** Renders one export line for the given shell ({@code bash}, {@code fish}, {@code powershell}). */
public static String formatExport(String shell, String key, String value) {
return switch (shell.toLowerCase()) {