diff --git a/src/main/java/io/floci/cli/commands/EnvCommand.java b/src/main/java/io/floci/cli/commands/EnvCommand.java index 80c83b6..a98566e 100644 --- a/src/main/java/io/floci/cli/commands/EnvCommand.java +++ b/src/main/java/io/floci/cli/commands/EnvCommand.java @@ -36,14 +36,18 @@ public class EnvCommand implements Callable { String region; @Option(names = {"--shell"}, - description = "Shell format: bash, fish, powershell (default: bash)", + description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)", defaultValue = "bash", - paramLabel = "bash|fish|powershell") + paramLabel = "") String shell; @Override public Integer call() { Printer printer = global.printer(); + if (!ShellExport.isSupported(shell)) { + printer.error(ShellExport.unsupported(shell)); + return 2; + } String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); int port = extractPort(effectiveEndpoint); String endpointUrl = "http://" + host + ":" + port; @@ -63,7 +67,7 @@ public class EnvCommand implements Callable { printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue())); } printer.println(""); - printer.println(Ansi.gray("# Run: eval $(floci env)")); + printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci env"))); return 0; } diff --git a/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java b/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java index 53a14d4..2ee610a 100644 --- a/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java @@ -63,9 +63,9 @@ public class AzEnvCommand implements Callable { String host; @Option(names = {"--shell"}, - description = "Shell format: bash, fish, powershell (default: bash)", + description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)", defaultValue = "bash", - paramLabel = "bash|fish|powershell") + paramLabel = "") String shell; private final Path stateRoot; @@ -88,6 +88,10 @@ public class AzEnvCommand implements Callable { @Override public Integer call() { Printer printer = global.printer(); + if (!ShellExport.isSupported(shell)) { + printer.error(ShellExport.unsupported(shell)); + return 2; + } String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); int port = extractPort(effectiveEndpoint); @@ -117,7 +121,7 @@ public class AzEnvCommand implements Callable { printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_CONNECTION_STRING", connStr)); printAzCliVars(printer, azCliVars); printer.println(""); - printer.println(Ansi.gray("# Run: eval $(floci az env)")); + printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci az env"))); } else { printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_ACCOUNT", account)); printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_KEY", DEV_KEY)); @@ -126,7 +130,7 @@ public class AzEnvCommand implements Callable { } printAzCliVars(printer, azCliVars); printer.println(""); - printer.println(Ansi.gray("# Run: eval $(floci az env --format sdk-vars)")); + printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci az env --format sdk-vars"))); } return 0; diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java index 26838a1..6b8ded6 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java @@ -36,14 +36,18 @@ public class GcpEnvCommand implements Callable { String serviceFilter; @Option(names = {"--shell"}, - description = "Shell format: bash, fish, powershell (default: bash)", + description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)", defaultValue = "bash", - paramLabel = "bash|fish|powershell") + paramLabel = "") String shell; @Override public Integer call() { Printer printer = global.printer(); + if (!ShellExport.isSupported(shell)) { + printer.error(ShellExport.unsupported(shell)); + return 2; + } String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); String host = extractHost(effectiveEndpoint); int port = extractPort(effectiveEndpoint); @@ -67,7 +71,7 @@ public class GcpEnvCommand implements Callable { printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue())); } printer.println(""); - printer.println(Ansi.gray("# Run: eval $(floci gcp env)")); + printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci gcp env"))); return 0; } diff --git a/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java b/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java index ff01250..16f6b5a 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java @@ -25,14 +25,18 @@ public class OciEnvCommand implements Callable { GlobalOptions global = new GlobalOptions(ProductProfile.OCI); @Option(names = {"--shell"}, - description = "Shell format: bash, fish, powershell (default: bash)", + description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)", defaultValue = "bash", - paramLabel = "bash|fish|powershell") + paramLabel = "") String shell; @Override public Integer call() { Printer printer = global.printer(); + if (!ShellExport.isSupported(shell)) { + printer.error(ShellExport.unsupported(shell)); + return 2; + } String endpoint = global.resolvedEndpoint(new DockerClient()); Map vars = new LinkedHashMap<>(); @@ -63,7 +67,7 @@ public class OciEnvCommand implements Callable { printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue())); } printer.println(""); - printer.println(Ansi.gray("# Run: eval $(floci oci env)")); + printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci oci env"))); return 0; } } diff --git a/src/main/java/io/floci/cli/output/ShellExport.java b/src/main/java/io/floci/cli/output/ShellExport.java index 78288ba..04a2188 100644 --- a/src/main/java/io/floci/cli/output/ShellExport.java +++ b/src/main/java/io/floci/cli/output/ShellExport.java @@ -1,5 +1,7 @@ package io.floci.cli.output; +import java.util.Set; + /** * Renders {@code export KEY=VALUE} lines for the {@code env} commands of every product tree. * @@ -10,9 +12,33 @@ package io.floci.cli.output; */ public final class ShellExport { + /** The {@code --shell} values the env commands accept. */ + public static final Set SHELLS = Set.of("bash", "zsh", "sh", "fish", "powershell", "pwsh", "ps1"); + private ShellExport() { } + public static boolean isSupported(String shell) { + return shell != null && SHELLS.contains(shell.toLowerCase()); + } + + /** The error for an unsupported {@code --shell}, ending with what to pass instead. */ + public static String unsupported(String shell) { + return "Unknown shell '" + shell + "'.\nUse --shell bash, zsh, sh, fish or powershell."; + } + + /** + * How to load {@code command}'s output in the given shell: {@code eval} only works in POSIX + * shells, fish and PowerShell read the lines from a pipe instead. + */ + public static String loadHint(String shell, String command) { + return switch (shell.toLowerCase()) { + case "fish" -> command + " --shell fish | source"; + case "powershell", "pwsh", "ps1" -> command + " --shell powershell | Invoke-Expression"; + default -> "eval \"$(" + command + ")\""; + }; + } + /** 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()) { @@ -20,7 +46,7 @@ public final class ShellExport { case "fish" -> "set -x " + key + " '" + value.replace("\\", "\\\\").replace("'", "\\'") + "'"; // PowerShell single quotes: literal except '' for a quote - case "powershell", "ps1" -> "$env:" + key + " = '" + value.replace("'", "''") + "'"; + case "powershell", "pwsh", "ps1" -> "$env:" + key + " = '" + value.replace("'", "''") + "'"; // POSIX single quotes: close, escaped quote, reopen default -> "export " + key + "='" + value.replace("'", "'\\''") + "'"; }; @@ -30,7 +56,7 @@ public final class ShellExport { public static String formatUnset(String shell, String key) { return switch (shell.toLowerCase()) { case "fish" -> "set -e " + key; - case "powershell", "ps1" -> "Remove-Item Env:" + key + " -ErrorAction SilentlyContinue"; + case "powershell", "pwsh", "ps1" -> "Remove-Item Env:" + key + " -ErrorAction SilentlyContinue"; default -> "unset " + key; }; } diff --git a/src/test/java/io/floci/cli/unit/EnvShellOptionTest.java b/src/test/java/io/floci/cli/unit/EnvShellOptionTest.java new file mode 100644 index 0000000..c5e00f4 --- /dev/null +++ b/src/test/java/io/floci/cli/unit/EnvShellOptionTest.java @@ -0,0 +1,43 @@ +package io.floci.cli.unit; + +import io.floci.cli.FlociCli; +import io.floci.cli.config.ProfileStore; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.*; + +/** BL-050: every env command refuses a shell it cannot write for, instead of printing bash. */ +class EnvShellOptionTest { + + @TempDir + Path tempDir; + + @ParameterizedTest + @ValueSource(strings = {"aws", "gcp", "az", "oci"}) + void anUnknownShellExitsTwo(String product) { + PrintStream out = System.out; + PrintStream err = System.err; + ByteArrayOutputStream outBuf = new ByteArrayOutputStream(); + ByteArrayOutputStream errBuf = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outBuf)); + System.setErr(new PrintStream(errBuf)); + int exit; + try { + exit = FlociCli.buildCommandLine(new ProfileStore(tempDir)).execute(product, "env", + "--shell", "tcsh", "--endpoint", "http://localhost:1", "--container", "floci-env-test-none"); + } finally { + System.setOut(out); + System.setErr(err); + } + + assertEquals(2, exit); + assertTrue(errBuf.toString().contains("Unknown shell 'tcsh'"), errBuf.toString()); + assertEquals("", outBuf.toString()); + } +} diff --git a/src/test/java/io/floci/cli/unit/ShellExportTest.java b/src/test/java/io/floci/cli/unit/ShellExportTest.java index ae8c11b..d970e34 100644 --- a/src/test/java/io/floci/cli/unit/ShellExportTest.java +++ b/src/test/java/io/floci/cli/unit/ShellExportTest.java @@ -46,4 +46,23 @@ class ShellExportTest { assertEquals("$env:K = 'it''s $x'", ShellExport.formatExport("powershell", "K", "it's $x")); } + + /** BL-050: the hint says how to load the lines in the shell they were written for. */ + @Test + void theLoadHintMatchesTheShell() { + assertEquals("eval \"$(floci env)\"", ShellExport.loadHint("bash", "floci env")); + assertEquals("eval \"$(floci gcp env)\"", ShellExport.loadHint("zsh", "floci gcp env")); + assertEquals("floci oci env --shell fish | source", ShellExport.loadHint("fish", "floci oci env")); + assertEquals("floci az env --shell powershell | Invoke-Expression", + ShellExport.loadHint("PowerShell", "floci az env")); + } + + @Test + void onlyKnownShellsAreSupported() { + assertTrue(ShellExport.isSupported("bash")); + assertTrue(ShellExport.isSupported("Fish")); + assertTrue(ShellExport.isSupported("pwsh")); + assertFalse(ShellExport.isSupported("tcsh")); + assertFalse(ShellExport.isSupported(null)); + } }