mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
status no longer probes the default instance for a named container with no known endpoint, and env, services, snapshot and az setup exit 1 with the next step instead of acting on it. wait keeps looking for the container until --timeout, and doctor and version still run. A profile's port is used before its container starts, and an endpoint from --endpoint, FLOCI_ENDPOINT or another host beats a local container's published port. BREAKING CHANGE: in status -o json, endpoint is null when the selected container has no known endpoint. env, services, snapshot and az setup exit 1 in that case, where they used to use the default endpoint. An --endpoint or FLOCI_ENDPOINT on the default port is now honoured even when a local container publishes another port. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
135 lines
4.5 KiB
Java
135 lines
4.5 KiB
Java
package io.floci.cli.commands.gcp;
|
|
|
|
import io.floci.cli.GlobalOptions;
|
|
import io.floci.cli.ProductProfile;
|
|
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.ArrayList;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.concurrent.Callable;
|
|
|
|
@Command(
|
|
name = "env",
|
|
description = "Print GCP environment variables to connect to Floci GCP",
|
|
mixinStandardHelpOptions = true
|
|
)
|
|
public class GcpEnvCommand implements Callable<Integer> {
|
|
|
|
private static final Set<String> ALL_SERVICES =
|
|
Set.of("gcs", "pubsub", "firestore", "datastore", "secretmanager", "iam");
|
|
|
|
@Mixin
|
|
GlobalOptions global = new GlobalOptions(ProductProfile.GCP);
|
|
|
|
@Option(names = {"--service"},
|
|
description = "Comma-separated services: gcs,pubsub,firestore,datastore,secretmanager,iam",
|
|
paramLabel = "<csv>")
|
|
String serviceFilter;
|
|
|
|
@Option(names = {"--shell"},
|
|
description = "Shell format: bash, zsh, sh, fish, powershell (default: bash)",
|
|
defaultValue = "bash",
|
|
paramLabel = "<shell>")
|
|
String shell;
|
|
|
|
private final DockerClient docker;
|
|
|
|
public GcpEnvCommand() { this(new DockerClient()); }
|
|
|
|
public GcpEnvCommand(DockerClient docker) { this.docker = docker; }
|
|
|
|
@Override
|
|
public Integer call() {
|
|
Printer printer = global.printer();
|
|
if (!ShellExport.isSupported(shell)) {
|
|
printer.error(ShellExport.unsupported(shell));
|
|
return 2;
|
|
}
|
|
String effectiveEndpoint = global.resolvedEndpoint(docker, printer::warn);
|
|
String host = extractHost(effectiveEndpoint);
|
|
int port = extractPort(effectiveEndpoint);
|
|
String hostPort = host + ":" + port;
|
|
|
|
List<String> requestedServices = resolveServices();
|
|
|
|
Map<String, String> vars = new LinkedHashMap<>();
|
|
for (String svc : requestedServices) {
|
|
String name = envVarName(svc);
|
|
String value = buildValue(svc, host, port, hostPort);
|
|
vars.put(name, value);
|
|
}
|
|
|
|
if (printer.format() != OutputFormat.text) {
|
|
printer.structured(new LinkedHashMap<>(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 gcp env" + global.envLoadFlags(shell))));
|
|
|
|
return 0;
|
|
}
|
|
|
|
private List<String> resolveServices() {
|
|
if (serviceFilter != null && !serviceFilter.isBlank()) {
|
|
List<String> result = new ArrayList<>();
|
|
for (String s : serviceFilter.split(",")) {
|
|
String svc = s.trim().toLowerCase();
|
|
if (ALL_SERVICES.contains(svc)) result.add(svc);
|
|
}
|
|
return result;
|
|
}
|
|
return List.of("gcs", "pubsub", "firestore", "datastore", "secretmanager");
|
|
}
|
|
|
|
private String envVarName(String service) {
|
|
return switch (service) {
|
|
case "gcs" -> "STORAGE_EMULATOR_HOST";
|
|
case "pubsub" -> "PUBSUB_EMULATOR_HOST";
|
|
case "firestore" -> "FIRESTORE_EMULATOR_HOST";
|
|
case "datastore" -> "DATASTORE_EMULATOR_HOST";
|
|
case "secretmanager" -> "SECRET_MANAGER_EMULATOR_HOST";
|
|
case "iam" -> "IAM_EMULATOR_HOST";
|
|
default -> service.toUpperCase().replace('-', '_') + "_EMULATOR_HOST";
|
|
};
|
|
}
|
|
|
|
private String buildValue(String service, String host, int port, String hostPort) {
|
|
// GCS SDK expects a full URL; the rest expect host:port
|
|
if ("gcs".equals(service)) {
|
|
return "http://" + hostPort;
|
|
}
|
|
return hostPort;
|
|
}
|
|
|
|
private String extractHost(String endpoint) {
|
|
try {
|
|
String h = URI.create(endpoint).getHost();
|
|
return h != null ? h : "localhost";
|
|
} catch (Exception e) {
|
|
return "localhost";
|
|
}
|
|
}
|
|
|
|
private int extractPort(String endpoint) {
|
|
try {
|
|
int p = URI.create(endpoint).getPort();
|
|
return p == -1 ? 4588 : p;
|
|
} catch (Exception e) {
|
|
return 4588;
|
|
}
|
|
}
|
|
}
|