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;
|
||||
|
||||
@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 = "<shell>")
|
||||
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<Integer> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,9 +63,9 @@ public class AzEnvCommand implements Callable<Integer> {
|
||||
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 = "<shell>")
|
||||
String shell;
|
||||
|
||||
private final Path stateRoot;
|
||||
@@ -88,6 +88,10 @@ public class AzEnvCommand implements Callable<Integer> {
|
||||
@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<Integer> {
|
||||
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<Integer> {
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -36,14 +36,18 @@ public class GcpEnvCommand implements Callable<Integer> {
|
||||
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 = "<shell>")
|
||||
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<Integer> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -25,14 +25,18 @@ public class OciEnvCommand implements Callable<Integer> {
|
||||
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 = "<shell>")
|
||||
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<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("");
|
||||
printer.println(Ansi.gray("# Run: eval $(floci oci env)"));
|
||||
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci oci env")));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user