mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-23 14:10:50 -05:00
fix(env): show how to load the variables in the chosen shell and reject unknown shells (#46)
The closing hint of floci env (all four products) now uses source for fish and Invoke-Expression for PowerShell instead of always suggesting eval, and an unknown --shell exits 2 with the accepted values instead of silently printing bash syntax. zsh, sh and pwsh are accepted by name.
This commit is contained in:
1 parent
030d468fd3
commit
fbdc3ef713
7 files changed
+119
-15
No files matched your search
@@ -36,14 +36,18 @@ public class EnvCommand implements Callable<Integer> {
|
|||||||
String region;
|
String region;
|
||||||
|
|
||||||
@Option(names = {"--shell"},
|
@Option(names = {"--shell"},
|
||||||
description = "Shell format: bash, fish, powershell (default: bash)",
|
description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)",
|
||||||
defaultValue = "bash",
|
defaultValue = "bash",
|
||||||
paramLabel = "bash|fish|powershell")
|
paramLabel = "<shell>")
|
||||||
String shell;
|
String shell;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer call() {
|
public Integer call() {
|
||||||
Printer printer = global.printer();
|
Printer printer = global.printer();
|
||||||
|
if (!ShellExport.isSupported(shell)) {
|
||||||
|
printer.error(ShellExport.unsupported(shell));
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
||||||
int port = extractPort(effectiveEndpoint);
|
int port = extractPort(effectiveEndpoint);
|
||||||
String endpointUrl = "http://" + host + ":" + port;
|
String endpointUrl = "http://" + host + ":" + port;
|
||||||
@@ -63,7 +67,7 @@ public class EnvCommand implements Callable<Integer> {
|
|||||||
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
|
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
|
||||||
}
|
}
|
||||||
printer.println("");
|
printer.println("");
|
||||||
printer.println(Ansi.gray("# Run: eval $(floci env)"));
|
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci env")));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -63,9 +63,9 @@ public class AzEnvCommand implements Callable<Integer> {
|
|||||||
String host;
|
String host;
|
||||||
|
|
||||||
@Option(names = {"--shell"},
|
@Option(names = {"--shell"},
|
||||||
description = "Shell format: bash, fish, powershell (default: bash)",
|
description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)",
|
||||||
defaultValue = "bash",
|
defaultValue = "bash",
|
||||||
paramLabel = "bash|fish|powershell")
|
paramLabel = "<shell>")
|
||||||
String shell;
|
String shell;
|
||||||
|
|
||||||
private final Path stateRoot;
|
private final Path stateRoot;
|
||||||
@@ -88,6 +88,10 @@ public class AzEnvCommand implements Callable<Integer> {
|
|||||||
@Override
|
@Override
|
||||||
public Integer call() {
|
public Integer call() {
|
||||||
Printer printer = global.printer();
|
Printer printer = global.printer();
|
||||||
|
if (!ShellExport.isSupported(shell)) {
|
||||||
|
printer.error(ShellExport.unsupported(shell));
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
||||||
int port = extractPort(effectiveEndpoint);
|
int port = extractPort(effectiveEndpoint);
|
||||||
|
|
||||||
@@ -117,7 +121,7 @@ public class AzEnvCommand implements Callable<Integer> {
|
|||||||
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_CONNECTION_STRING", connStr));
|
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_CONNECTION_STRING", connStr));
|
||||||
printAzCliVars(printer, azCliVars);
|
printAzCliVars(printer, azCliVars);
|
||||||
printer.println("");
|
printer.println("");
|
||||||
printer.println(Ansi.gray("# Run: eval $(floci az env)"));
|
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci az env")));
|
||||||
} else {
|
} else {
|
||||||
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_ACCOUNT", account));
|
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_ACCOUNT", account));
|
||||||
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_KEY", DEV_KEY));
|
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_KEY", DEV_KEY));
|
||||||
@@ -126,7 +130,7 @@ public class AzEnvCommand implements Callable<Integer> {
|
|||||||
}
|
}
|
||||||
printAzCliVars(printer, azCliVars);
|
printAzCliVars(printer, azCliVars);
|
||||||
printer.println("");
|
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;
|
return 0;
|
||||||
|
|||||||
@@ -36,14 +36,18 @@ public class GcpEnvCommand implements Callable<Integer> {
|
|||||||
String serviceFilter;
|
String serviceFilter;
|
||||||
|
|
||||||
@Option(names = {"--shell"},
|
@Option(names = {"--shell"},
|
||||||
description = "Shell format: bash, fish, powershell (default: bash)",
|
description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)",
|
||||||
defaultValue = "bash",
|
defaultValue = "bash",
|
||||||
paramLabel = "bash|fish|powershell")
|
paramLabel = "<shell>")
|
||||||
String shell;
|
String shell;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer call() {
|
public Integer call() {
|
||||||
Printer printer = global.printer();
|
Printer printer = global.printer();
|
||||||
|
if (!ShellExport.isSupported(shell)) {
|
||||||
|
printer.error(ShellExport.unsupported(shell));
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
|
||||||
String host = extractHost(effectiveEndpoint);
|
String host = extractHost(effectiveEndpoint);
|
||||||
int port = extractPort(effectiveEndpoint);
|
int port = extractPort(effectiveEndpoint);
|
||||||
@@ -67,7 +71,7 @@ public class GcpEnvCommand implements Callable<Integer> {
|
|||||||
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
|
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
|
||||||
}
|
}
|
||||||
printer.println("");
|
printer.println("");
|
||||||
printer.println(Ansi.gray("# Run: eval $(floci gcp env)"));
|
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci gcp env")));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,14 +25,18 @@ public class OciEnvCommand implements Callable<Integer> {
|
|||||||
GlobalOptions global = new GlobalOptions(ProductProfile.OCI);
|
GlobalOptions global = new GlobalOptions(ProductProfile.OCI);
|
||||||
|
|
||||||
@Option(names = {"--shell"},
|
@Option(names = {"--shell"},
|
||||||
description = "Shell format: bash, fish, powershell (default: bash)",
|
description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)",
|
||||||
defaultValue = "bash",
|
defaultValue = "bash",
|
||||||
paramLabel = "bash|fish|powershell")
|
paramLabel = "<shell>")
|
||||||
String shell;
|
String shell;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer call() {
|
public Integer call() {
|
||||||
Printer printer = global.printer();
|
Printer printer = global.printer();
|
||||||
|
if (!ShellExport.isSupported(shell)) {
|
||||||
|
printer.error(ShellExport.unsupported(shell));
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
String endpoint = global.resolvedEndpoint(new DockerClient());
|
String endpoint = global.resolvedEndpoint(new DockerClient());
|
||||||
|
|
||||||
Map<String, String> vars = new LinkedHashMap<>();
|
Map<String, String> vars = new LinkedHashMap<>();
|
||||||
@@ -63,7 +67,7 @@ public class OciEnvCommand implements Callable<Integer> {
|
|||||||
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
|
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
|
||||||
}
|
}
|
||||||
printer.println("");
|
printer.println("");
|
||||||
printer.println(Ansi.gray("# Run: eval $(floci oci env)"));
|
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci oci env")));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package io.floci.cli.output;
|
package io.floci.cli.output;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders {@code export KEY=VALUE} lines for the {@code env} commands of every product tree.
|
* 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 {
|
public final class ShellExport {
|
||||||
|
|
||||||
|
/** The {@code --shell} values the env commands accept. */
|
||||||
|
public static final Set<String> SHELLS = Set.of("bash", "zsh", "sh", "fish", "powershell", "pwsh", "ps1");
|
||||||
|
|
||||||
private ShellExport() {
|
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}). */
|
/** Renders one export line for the given shell ({@code bash}, {@code fish}, {@code powershell}). */
|
||||||
public static String formatExport(String shell, String key, String value) {
|
public static String formatExport(String shell, String key, String value) {
|
||||||
return switch (shell.toLowerCase()) {
|
return switch (shell.toLowerCase()) {
|
||||||
@@ -20,7 +46,7 @@ public final class ShellExport {
|
|||||||
case "fish" -> "set -x " + key + " '"
|
case "fish" -> "set -x " + key + " '"
|
||||||
+ value.replace("\\", "\\\\").replace("'", "\\'") + "'";
|
+ value.replace("\\", "\\\\").replace("'", "\\'") + "'";
|
||||||
// PowerShell single quotes: literal except '' for a quote
|
// 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
|
// POSIX single quotes: close, escaped quote, reopen
|
||||||
default -> "export " + key + "='" + value.replace("'", "'\\''") + "'";
|
default -> "export " + key + "='" + value.replace("'", "'\\''") + "'";
|
||||||
};
|
};
|
||||||
@@ -30,7 +56,7 @@ public final class ShellExport {
|
|||||||
public static String formatUnset(String shell, String key) {
|
public static String formatUnset(String shell, String key) {
|
||||||
return switch (shell.toLowerCase()) {
|
return switch (shell.toLowerCase()) {
|
||||||
case "fish" -> "set -e " + key;
|
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;
|
default -> "unset " + key;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,4 +46,23 @@ class ShellExportTest {
|
|||||||
assertEquals("$env:K = 'it''s $x'",
|
assertEquals("$env:K = 'it''s $x'",
|
||||||
ShellExport.formatExport("powershell", "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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user