Files
floci-cli/src/main/java/io/floci/cli/commands/LogsCommand.java
T
Hector Ventura 882c0ea7be fix(cli): keep the selected instance in the commands that hints suggest (#74)
Commands printed by errors, env and doctor now carry --profile, --container and
an explicit --endpoint, so pasting one acts on the same instance. After a failed
pull, the suggested start also keeps the --port, --image, --persist, --services
and --namespace you typed.

The env load hint adds -o text after a profile, which may set output: json, and
quotes values for the shell it is for. A value containing a line break prints as a
placeholder, because the hint is a comment in the output people feed to eval.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
2026-11-04 01:48:30 -06:00

86 lines
2.8 KiB
Java

package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.docker.DockerClient;
import io.floci.cli.docker.DockerException;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import picocli.CommandLine.*;
import java.util.concurrent.Callable;
@Command(
name = "logs",
description = "Fetch logs from the Floci AWS container",
mixinStandardHelpOptions = true
)
public class LogsCommand implements Callable<Integer> {
protected final ProductProfile profile;
@Mixin
protected GlobalOptions global;
public LogsCommand() {
this(ProductProfile.AWS);
}
protected LogsCommand(ProductProfile profile) {
this(profile, new DockerClient());
}
public LogsCommand(ProductProfile profile, DockerClient docker) {
this.docker = docker;
this.profile = profile;
this.global = new GlobalOptions(profile);
}
@Option(names = {"--follow", "-f"}, description = "Follow log output")
boolean follow;
@Option(names = {"--tail"}, description = "Number of lines from the end (0 = all)", defaultValue = "100", paramLabel = "<n>")
int tail;
@Option(names = {"--since"}, description = "Show logs since duration (e.g. 5m, 1h) or timestamp", paramLabel = "<duration>")
String since;
@Option(names = {"--service"}, description = "Filter to a specific service (not yet supported; requires server-side filtering)", paramLabel = "<name>")
String service;
private final DockerClient docker;
@Override
public Integer call() {
Printer printer = global.printer();
try {
var info = docker.inspectContainer(global.container);
if (info.isEmpty()) {
printer.error("Container '" + global.container + "' not found.\nRun '" + profile.commandPrefix() + " start" + global.instanceSelector() + "' to launch one.");
return 1;
}
} catch (DockerException e) {
printer.error("Failed to inspect container: " + e.withNextStep(DockerClient.CHECK_DOCKER));
return 1;
}
if (printer.format() != OutputFormat.text) {
// Logs are the container's own output, line by line; there is no document to build.
printer.warn("logs prints the container's output as plain text; -o " + printer.format() + " is ignored.");
}
if (service != null) {
printer.warn("--service filtering is not yet supported. Showing all container logs.");
}
try {
docker.streamLogs(global.container, follow, tail, since);
return 0;
} catch (DockerException e) {
printer.error("Failed to fetch logs: " + e.withNextStep(DockerClient.CHECK_DOCKER));
return 1;
}
}
}