fix: updating with latest floci changes

This commit is contained in:
Hector Ventura committed 2026-05-15 02:34:27 -05:00
1 parent 0452664457
commit 1d7f9f81d0
16 files changed
+378 -116

No files matched your search

+3 -1
View File
@@ -14,7 +14,8 @@ import picocli.CommandLine.*;
" floci stop — stop the container%n" +
" floci status — show health and version%n" +
" floci doctor — diagnose environment issues%n" +
" floci logs — stream container logs%n",
" floci logs — stream container logs%n" +
" floci env — print AWS environment variables%n",
mixinStandardHelpOptions = true,
versionProvider = FlociCli.VersionProvider.class,
subcommands = {
@@ -27,6 +28,7 @@ import picocli.CommandLine.*;
VersionCommand.class,
ServicesCommand.class,
DoctorCommand.class,
EnvCommand.class,
ConfigCommand.class,
SnapshotCommand.class,
CompletionCommand.class,
@@ -1,10 +1,13 @@
package io.floci.cli;
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 picocli.CommandLine.Option;
import java.net.URI;
public class GlobalOptions {
@Option(names = {"--endpoint"},
@@ -46,6 +49,38 @@ public class GlobalOptions {
return new Printer(System.out, System.err, output, quiet);
}
// Inspects the container and derives the endpoint from its host port mapping.
// Falls back to the configured endpoint if the container is not found or has no mapping.
public String resolvedEndpoint(DockerClient docker) {
try {
return docker.inspectContainer(container)
.map(info -> endpointFromPorts(info.ports(), endpoint))
.orElse(endpoint);
} catch (Exception e) {
return endpoint;
}
}
// Matches the container-side port from the configured endpoint to find the actual host port.
public String endpointFromPorts(String ports, String fallback) {
if (ports == null || ports.isBlank()) return fallback;
try {
int containerPort = URI.create(fallback).getPort();
if (containerPort == -1) containerPort = 4566;
for (String mapping : ports.trim().split("\\s+")) {
int arrow = mapping.indexOf("->");
if (arrow < 0) continue;
String hostPort = mapping.substring(0, arrow);
String rest = mapping.substring(arrow + 2);
String cPort = rest.contains("/") ? rest.substring(0, rest.indexOf('/')) : rest;
if (String.valueOf(containerPort).equals(cPort)) {
return "http://localhost:" + hostPort;
}
}
} catch (Exception ignored) {}
return fallback;
}
private static boolean isStdoutTty() {
return System.console() != null;
}
@@ -4,7 +4,18 @@ import io.floci.cli.GlobalOptions;
import io.floci.cli.doctor.Check;
import io.floci.cli.doctor.CheckResult;
import io.floci.cli.doctor.CheckStatus;
import io.floci.cli.doctor.checks.*;
import io.floci.cli.docker.DockerClient;
import io.floci.cli.doctor.checks.AwsCliEndpointCheck;
import io.floci.cli.doctor.checks.AwsCliS3PathStyleCheck;
import io.floci.cli.doctor.checks.ContainerRunningCheck;
import io.floci.cli.doctor.checks.DockerDaemonCheck;
import io.floci.cli.doctor.checks.DockerInstalledCheck;
import io.floci.cli.doctor.checks.DockerSocketCheck;
import io.floci.cli.doctor.checks.DockerVersionCheck;
import io.floci.cli.doctor.checks.EndpointReachableCheck;
import io.floci.cli.doctor.checks.ImagePresentCheck;
import io.floci.cli.doctor.checks.ImageVersionCheck;
import io.floci.cli.doctor.checks.PortAvailableCheck;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
@@ -43,27 +54,30 @@ public class DoctorCommand implements Callable<Integer> {
new ContainerRunningCheck(),
new EndpointReachableCheck(),
new AwsCliEndpointCheck(),
new AwsCliS3PathStyleCheck(),
new SdkRustS3XmlCheck(),
new SdkGoEndpointCheck()
new AwsCliS3PathStyleCheck()
);
@Override
public Integer call() {
Printer printer = global.printer();
List<CheckResult> results = new ArrayList<>();
String effectiveEndpoint = global.resolvedEndpoint(new DockerClient());
printer.println(Ansi.bold("Floci Doctor") + " — checking your environment");
printer.println("");
boolean textMode = printer.format() == OutputFormat.text;
for (Check check : ALL_CHECKS) {
CheckResult result = check.run(global.endpoint, global.container);
if (checkName != null && !result.name().equals(checkName)) continue;
results.add(result);
printResult(printer, result);
if (textMode) {
printer.println(Ansi.bold("Floci Doctor") + " — checking your environment");
printer.println("");
}
if (printer.format() != OutputFormat.text) {
for (Check check : ALL_CHECKS) {
CheckResult result = check.run(effectiveEndpoint, global.container);
if (checkName != null && !result.name().equals(checkName)) continue;
results.add(result);
if (textMode) printResult(printer, result);
}
if (!textMode) {
List<Map<String, Object>> structured = new ArrayList<>();
for (CheckResult r : results) {
Map<String, Object> m = new LinkedHashMap<>();
@@ -0,0 +1,83 @@
package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
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",
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, fish, powershell (default: bash)",
defaultValue = "bash",
paramLabel = "bash|fish|powershell")
String shell;
@Override
public Integer call() {
Printer printer = global.printer();
int port = extractPort(global.endpoint);
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(formatExport(entry.getKey(), entry.getValue()));
}
printer.println("");
printer.println(Ansi.gray("# Run: eval $(floci env)"));
return 0;
}
private String formatExport(String key, String value) {
return switch (shell.toLowerCase()) {
case "fish" -> "set -x " + key + " \"" + value + "\"";
case "powershell", "ps1" -> "$env:" + key + " = \"" + value + "\"";
default -> "export " + key + "=" + value;
};
}
private int extractPort(String endpoint) {
try {
int port = URI.create(endpoint).getPort();
return port == -1 ? 4566 : port;
} catch (Exception e) {
return 4566;
}
}
}
@@ -104,6 +104,9 @@ public class StartCommand implements Callable<Integer> {
return 0;
}
// Update endpoint to match the bound host port before polling readiness
global.endpoint = "http://localhost:" + port;
// Wait for readiness
printer.println(Ansi.gray("Waiting for Floci to be ready..."));
WaitCommand wait = new WaitCommand();
@@ -33,12 +33,14 @@ public class StatusCommand implements Callable<Integer> {
String containerState = "not found";
String containerImage = "";
String containerPorts = "";
String effectiveEndpoint = global.endpoint;
try {
Optional<DockerClient.ContainerInfo> info = docker.inspectContainer(global.container);
if (info.isPresent()) {
containerState = info.get().state();
containerImage = info.get().image();
containerPorts = info.get().ports();
effectiveEndpoint = global.endpointFromPorts(containerPorts, global.endpoint);
}
} catch (DockerException e) {
containerState = "error: " + e.getMessage();
@@ -48,7 +50,7 @@ public class StatusCommand implements Callable<Integer> {
String serverVersion = "unavailable";
String serverEdition = "";
boolean reachable = false;
FlociHttpClient client = new FlociHttpClient(global.endpoint);
FlociHttpClient client = new FlociHttpClient(effectiveEndpoint);
try {
var health = client.health();
serverVersion = health.version();
@@ -61,7 +63,7 @@ public class StatusCommand implements Callable<Integer> {
data.put("state", containerState);
data.put("image", containerImage);
data.put("ports", containerPorts);
data.put("endpoint", global.endpoint);
data.put("endpoint", effectiveEndpoint);
data.put("reachable", reachable);
data.put("version", serverVersion);
data.put("edition", serverEdition);
@@ -81,7 +83,7 @@ public class StatusCommand implements Callable<Integer> {
printer.println(" Container: " + global.container + " " + stateColor);
if (!containerImage.isBlank()) printer.println(" Image: " + containerImage);
if (!containerPorts.isBlank()) printer.println(" Ports: " + containerPorts);
printer.println(" Endpoint: " + global.endpoint);
printer.println(" Endpoint: " + effectiveEndpoint);
printer.println(" Reachable: " + (reachable ? Ansi.green("yes") : Ansi.red("no")));
if (reachable) {
printer.println(" Version: " + serverVersion);
@@ -95,4 +97,5 @@ public class StatusCommand implements Callable<Integer> {
return 0;
}
}
@@ -32,7 +32,10 @@ public class VersionCommand implements Callable<Integer> {
String serverEdition = null;
String imageDigest = null;
FlociHttpClient client = new FlociHttpClient(global.endpoint);
var docker = new io.floci.cli.docker.DockerClient();
String effectiveEndpoint = global.resolvedEndpoint(docker);
FlociHttpClient client = new FlociHttpClient(effectiveEndpoint);
try {
var info = client.info();
serverVersion = info.version();
@@ -41,7 +44,6 @@ public class VersionCommand implements Callable<Integer> {
// Try to read image digest from Docker (best-effort)
try {
var docker = new io.floci.cli.docker.DockerClient();
imageDigest = docker.imageDigest("floci/floci").orElse(null);
} catch (Exception ignored) {}
@@ -3,7 +3,10 @@ package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.http.FlociHttpClient;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import java.util.Map;
import picocli.CommandLine.*;
import java.time.Duration;
@@ -30,12 +33,17 @@ public class WaitCommand implements Callable<Integer> {
public Integer call() {
Printer printer = global.printer();
long timeoutMillis = parseDuration(timeout);
FlociHttpClient client = new FlociHttpClient(global.endpoint);
String effectiveEndpoint = global.resolvedEndpoint(new io.floci.cli.docker.DockerClient());
FlociHttpClient client = new FlociHttpClient(effectiveEndpoint);
Instant deadline = Instant.now().plusMillis(timeoutMillis);
while (Instant.now().isBefore(deadline)) {
if (isReady(client, service)) {
printer.println(Ansi.green("Floci is ready") + " (" + global.endpoint + ")");
if (printer.format() != OutputFormat.text) {
printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint));
} else {
printer.println(Ansi.green("Floci is ready") + " (" + effectiveEndpoint + ")");
}
return 0;
}
printSpinner(printer, deadline);
@@ -40,7 +40,7 @@ public class DockerClient {
public Optional<ContainerInfo> inspectContainer(String name) throws DockerException {
try {
String out = run("docker", "inspect", "--format",
"{{.Id}}|{{.Name}}|{{.Config.Image}}|{{.Status}}|{{.State.Status}}|{{range $p, $b := .NetworkSettings.Ports}}{{$p}} {{end}}",
"{{.Id}}|{{.Name}}|{{.Config.Image}}|{{.State.Status}}|{{.State.Status}}|{{range $p, $b := .NetworkSettings.Ports}}{{if $b}}{{(index $b 0).HostPort}}->{{$p}} {{end}}{{end}}",
name);
if (out.isBlank()) return Optional.empty();
String[] parts = out.trim().split("\\|", -1);
@@ -52,7 +52,7 @@ public class DockerClient {
parts.length > 4 ? parts[4] : "",
parts.length > 5 ? parts[5].trim() : ""));
} catch (DockerException e) {
if (e.getMessage() != null && e.getMessage().contains("No such")) {
if (e.getMessage() != null && e.getMessage().toLowerCase().contains("no such")) {
return Optional.empty();
}
throw e;
@@ -3,21 +3,51 @@ package io.floci.cli.doctor.checks;
import io.floci.cli.doctor.Check;
import io.floci.cli.doctor.CheckResult;
import java.net.URI;
import java.net.URISyntaxException;
public class AwsCliEndpointCheck implements Check {
@Override
public CheckResult run(String endpoint, String container) {
// Check if aws CLI is installed first
if (!isAwsCliInstalled()) {
return CheckResult.ok("aws.cli.endpoint", "aws CLI not installed — skipped");
}
String envVar = System.getenv("AWS_ENDPOINT_URL");
int effectivePort = extractPort(endpoint);
if (envVar != null && !envVar.isBlank()) {
int envPort = extractPort(envVar);
if (envPort != -1 && effectivePort != -1 && envPort != effectivePort) {
String corrected = replacePort(envVar, effectivePort);
return CheckResult.warn("aws.cli.endpoint",
"AWS_ENDPOINT_URL=" + envVar + " (port mismatch — Floci is on port " + effectivePort + ")",
"export AWS_ENDPOINT_URL=" + corrected);
}
return CheckResult.ok("aws.cli.endpoint", "AWS_ENDPOINT_URL=" + envVar);
}
String suggested = "http://localhost.floci.io:" + (effectivePort != -1 ? effectivePort : 4566);
return CheckResult.warn("aws.cli.endpoint",
"AWS_ENDPOINT_URL is not set",
"export AWS_ENDPOINT_URL=" + endpoint);
"export AWS_ENDPOINT_URL=" + suggested);
}
private int extractPort(String url) {
try {
int port = URI.create(url).getPort();
return port == -1 ? 80 : port;
} catch (Exception e) {
return -1;
}
}
private String replacePort(String url, int newPort) {
try {
URI uri = URI.create(url);
return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(),
newPort, uri.getPath(), uri.getQuery(), uri.getFragment()).toString();
} catch (Exception e) {
return url;
}
}
private boolean isAwsCliInstalled() {
@@ -1,30 +0,0 @@
package io.floci.cli.doctor.checks;
import io.floci.cli.doctor.Check;
import io.floci.cli.doctor.CheckResult;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class SdkGoEndpointCheck implements Check {
@Override
public CheckResult run(String endpoint, String container) {
Path goMod = Path.of("go.mod");
if (!Files.exists(goMod)) {
return CheckResult.ok("sdk.go.endpoint", "No go.mod in CWD — skipped");
}
try {
String content = Files.readString(goMod);
if (!content.contains("aws-sdk-go-v2")) {
return CheckResult.ok("sdk.go.endpoint", "aws-sdk-go-v2 not in go.mod — skipped");
}
return CheckResult.warn("sdk.go.endpoint",
"aws-sdk-go-v2 detected — use BaseEndpoint instead of deprecated EndpointResolver",
"See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/endpoints/ for migration guide");
} catch (IOException e) {
return CheckResult.warn("sdk.go.endpoint", "Could not read go.mod", null);
}
}
}
@@ -1,30 +0,0 @@
package io.floci.cli.doctor.checks;
import io.floci.cli.doctor.Check;
import io.floci.cli.doctor.CheckResult;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class SdkRustS3XmlCheck implements Check {
@Override
public CheckResult run(String endpoint, String container) {
Path cargoToml = Path.of("Cargo.toml");
if (!Files.exists(cargoToml)) {
return CheckResult.ok("sdk.rust.s3.xml", "No Cargo.toml in CWD — skipped");
}
try {
String content = Files.readString(cargoToml);
if (!content.contains("aws-sdk-s3")) {
return CheckResult.ok("sdk.rust.s3.xml", "aws-sdk-s3 not in Cargo.toml — skipped");
}
return CheckResult.warn("sdk.rust.s3.xml",
"aws-sdk-s3 detected — known issue: XmlDecodeError on S3 error responses",
"See https://github.com/floci-io/floci/issues/11 for workaround");
} catch (IOException e) {
return CheckResult.warn("sdk.rust.s3.xml", "Could not read Cargo.toml", null);
}
}
}
@@ -29,7 +29,7 @@ public class FlociHttpClient {
JsonNode node = getJson("/_floci/health");
return new HealthInfo(
node.path("version").asText("unknown"),
node.path("edition").asText("community"),
node.path("original_edition").asText(node.path("edition").asText("community")),
node.path("services").isArray()
? MAPPER.convertValue(node.path("services"), String[].class)
: new String[0]);
@@ -47,7 +47,7 @@ public class FlociHttpClient {
JsonNode node = getJson("/_floci/info");
return new ServerInfo(
node.path("version").asText("unknown"),
node.path("edition").asText("community"));
node.path("original_edition").asText(node.path("edition").asText("community")));
}
public InitState initState() throws FlociException {