mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-30 17:05:21 -05:00
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.
83 lines
2.6 KiB
Java
83 lines
2.6 KiB
Java
package io.floci.cli.commands;
|
|
|
|
import io.floci.cli.GlobalOptions;
|
|
import io.floci.cli.docker.DockerClient;
|
|
import io.floci.cli.output.Ansi;
|
|
import io.floci.cli.output.OutputFormat;
|
|
import io.floci.cli.output.Printer;
|
|
import io.floci.cli.output.ShellExport;
|
|
import picocli.CommandLine.*;
|
|
|
|
import java.net.URI;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.concurrent.Callable;
|
|
|
|
@Command(
|
|
name = "env",
|
|
description = "Print AWS environment variables to connect to Floci AWS",
|
|
mixinStandardHelpOptions = true
|
|
)
|
|
public class EnvCommand implements Callable<Integer> {
|
|
|
|
@Mixin
|
|
GlobalOptions global;
|
|
|
|
@Option(names = {"--host"},
|
|
description = "Hostname for the AWS endpoint URL (default: localhost.floci.io)",
|
|
defaultValue = "${FLOCI_HOST:-localhost.floci.io}",
|
|
paramLabel = "<host>")
|
|
String host;
|
|
|
|
@Option(names = {"--region"},
|
|
description = "AWS region (default: us-east-1)",
|
|
defaultValue = "${AWS_DEFAULT_REGION:-us-east-1}",
|
|
paramLabel = "<region>")
|
|
String region;
|
|
|
|
@Option(names = {"--shell"},
|
|
description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)",
|
|
defaultValue = "bash",
|
|
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;
|
|
|
|
Map<String, String> vars = new LinkedHashMap<>();
|
|
vars.put("AWS_ENDPOINT_URL", endpointUrl);
|
|
vars.put("AWS_ACCESS_KEY_ID", "test");
|
|
vars.put("AWS_SECRET_ACCESS_KEY", "test");
|
|
vars.put("AWS_DEFAULT_REGION", region);
|
|
|
|
if (printer.format() != OutputFormat.text) {
|
|
printer.structured(vars);
|
|
return 0;
|
|
}
|
|
|
|
for (Map.Entry<String, String> entry : vars.entrySet()) {
|
|
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
|
|
}
|
|
printer.println("");
|
|
printer.println(Ansi.gray("# Run: " + ShellExport.loadHint(shell, "floci env")));
|
|
return 0;
|
|
}
|
|
|
|
private int extractPort(String endpoint) {
|
|
try {
|
|
int port = URI.create(endpoint).getPort();
|
|
return port == -1 ? 4566 : port;
|
|
} catch (Exception e) {
|
|
return 4566;
|
|
}
|
|
}
|
|
}
|