mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
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:
7 files changed
+62
-10
No files matched your search
@@ -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()) {
|
||||
|
||||
@@ -95,10 +95,38 @@ class InstanceHintsTest {
|
||||
new OciEnvCommand(docker), new AzEnvCommand(temp, endpoint -> { throw new AssertionError("no cert expected"); }, docker))) {
|
||||
Run result = run(env, "--profile", "team alpha");
|
||||
assertEquals(0, result.exit(), result.err());
|
||||
assertTrue(result.out().contains("env --profile 'team alpha'"), result.out());
|
||||
assertTrue(result.out().contains("env --profile 'team alpha' -o text"), result.out());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void envLoadHintsQuoteForTheShellTheyAreFor() throws Exception {
|
||||
Files.writeString(temp.resolve("team's.yaml"), "container: other\nendpoint: http://127.0.0.1:1\n");
|
||||
for (Callable<Integer> env : List.<Callable<Integer>>of(new EnvCommand(new FakeDocker()), new GcpEnvCommand(new FakeDocker()),
|
||||
new OciEnvCommand(new FakeDocker()), new AzEnvCommand(temp, endpoint -> { throw new AssertionError("no cert expected"); }, new FakeDocker()))) {
|
||||
assertTrue(run(env, "--profile", "team's").out().contains("--profile 'team'\\''s' -o text)\""));
|
||||
}
|
||||
for (Callable<Integer> env : List.<Callable<Integer>>of(new EnvCommand(new FakeDocker()), new GcpEnvCommand(new FakeDocker()),
|
||||
new OciEnvCommand(new FakeDocker()), new AzEnvCommand(temp, endpoint -> { throw new AssertionError("no cert expected"); }, new FakeDocker()))) {
|
||||
String out = run(env, "--profile", "team's", "--shell", "powershell").out();
|
||||
assertTrue(out.contains("--profile 'team''s' -o text --shell powershell | Invoke-Expression"), out);
|
||||
}
|
||||
for (Callable<Integer> env : List.<Callable<Integer>>of(new EnvCommand(new FakeDocker()), new GcpEnvCommand(new FakeDocker()),
|
||||
new OciEnvCommand(new FakeDocker()), new AzEnvCommand(temp, endpoint -> { throw new AssertionError("no cert expected"); }, new FakeDocker()))) {
|
||||
String out = run(env, "--profile", "team's", "--shell", "fish").out();
|
||||
assertTrue(out.contains("--profile 'team\\'s' -o text --shell fish | source"), out);
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void aProfileThatAsksForJsonStillGetsALoadableHint() throws Exception {
|
||||
Files.writeString(temp.resolve("json.yaml"), "container: other\nendpoint: http://127.0.0.1:1\noutput: json\n");
|
||||
String out = run(new EnvCommand(new FakeDocker()), "--profile", "json", "-o", "text").out();
|
||||
assertTrue(out.contains("eval \"$(floci env --profile json -o text)\""), out);
|
||||
}
|
||||
@Test
|
||||
void aLoadHintWithoutAProfileStaysBare() throws Exception {
|
||||
assertTrue(run(new EnvCommand(new FakeDocker())).out().contains("eval \"$(floci env)\""));
|
||||
}
|
||||
@Test
|
||||
void doctorPassesTheSelectorToCheckFixesInStructuredOutput() throws Exception {
|
||||
for (ProductProfile product : PRODUCTS) {
|
||||
FakeDocker docker = new FakeDocker();
|
||||
|
||||
Reference in new issue
Block a user