fix(completion): offer only bash and zsh, the shells a script is generated for (#59)

The help listed fish and powershell, which only produced an error; an unsupported shell is now a usage error (exit 2) with nothing on stdout. Container details are read from docker's inspect JSON instead of a pipe-delimited template, which a value containing the delimiter could shift.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-11 08:58:31 -05:00
1 parent bd4fa0f134
commit 804e355936
5 files changed
+198 -28

No files matched your search

@@ -21,7 +21,9 @@ public class CompletionCommand implements Callable<Integer> {
@Spec
Model.CommandSpec spec;
@Parameters(index = "0", description = "Shell type: bash, zsh, fish, powershell", paramLabel = "bash|zsh|fish|powershell")
// Only the shells a script is generated for: picocli writes bash completion, which zsh loads
// through bashcompinit. Listing fish and powershell here promised scripts that do not exist.
@Parameters(index = "0", description = "Shell type: bash or zsh", paramLabel = "bash|zsh")
String shell;
@Override
@@ -36,13 +38,13 @@ public class CompletionCommand implements Callable<Integer> {
case "bash" -> AutoComplete.bash("floci", root);
case "zsh" -> AutoComplete.bash("floci", root); // zsh is bash-compatible; picocli bash script works for zsh too
default -> {
printer.error("Shell '" + shell + "' not supported. Supported: bash, zsh\n" +
"For fish and powershell, track https://github.com/floci-io/floci-cli/issues");
printer.error("Shell '" + shell + "' is not supported.\nUse bash or zsh.");
yield null;
}
};
if (script == null) return 1;
// A value the command cannot accept, like any other bad argument.
if (script == null) return ExitCode.USAGE;
System.out.print(script);
return 0;
}
@@ -67,18 +67,10 @@ public class DockerClient {
private static final ObjectMapper JSON = new ObjectMapper();
public Optional<RunSettings> inspectRunSettings(String name, int containerPort) throws DockerException {
String out;
Optional<JsonNode> document = inspectDocument(name);
if (document.isEmpty()) return Optional.empty();
try {
out = run("docker", "container", "inspect", "--format", "{{json .}}", "--", name);
} catch (DockerException e) {
if (e.getMessage() != null && e.getMessage().toLowerCase().contains("no such")) {
return Optional.empty();
}
throw e;
}
if (out.isBlank()) return Optional.empty();
try {
JsonNode c = JSON.readTree(out);
JsonNode c = document.get();
String image = textOrNull(c.path("Config").path("Image"));
Integer hostPort = null;
JsonNode binding = c.path("HostConfig").path("PortBindings").path(containerPort + "/tcp");
@@ -133,26 +125,56 @@ public class DockerClient {
}
public Optional<ContainerInfo> inspectContainer(String name) throws DockerException {
Optional<JsonNode> document = inspectDocument(name);
if (document.isEmpty()) return Optional.empty();
JsonNode c = document.get();
String reported = c.path("Name").asText("").replaceFirst("^/", "");
return Optional.of(new ContainerInfo(
c.path("Id").asText(""),
reported.isBlank() ? name : reported,
c.path("Config").path("Image").asText(""),
c.path("State").path("Status").asText(""),
publishedPorts(c.path("NetworkSettings").path("Ports"))));
}
/**
* The published ports of an inspect document as {@code "14566->4566/tcp 18080->8080/tcp"}:
* host port, then the container side. A port that is exposed but not published has no
* binding and is left out.
*/
public static String publishedPorts(JsonNode ports) {
StringBuilder published = new StringBuilder();
for (var port : ports.properties()) {
JsonNode bindings = port.getValue();
if (!bindings.isArray() || bindings.isEmpty()) continue;
String hostPort = bindings.get(0).path("HostPort").asText("");
if (hostPort.isBlank()) continue;
if (!published.isEmpty()) published.append(' ');
published.append(hostPort).append("->").append(port.getKey());
}
return published.toString();
}
// The container's whole inspect document, or empty when there is no such container. One
// JSON read serves every caller; a field-by-field Go template had to be split on a
// delimiter, which a value could contain.
private Optional<JsonNode> inspectDocument(String name) throws DockerException {
String out;
try {
String out = run("docker", "container", "inspect",
"--format",
"{{.Id}}|{{.Name}}|{{.Config.Image}}|{{.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);
return Optional.of(new ContainerInfo(
parts.length > 0 ? parts[0] : "", // id
parts.length > 1 ? parts[1].replaceFirst("^/", "") : name,
parts.length > 2 ? parts[2] : "", // image
parts.length > 3 ? parts[3] : "", // status
parts.length > 4 ? parts[4].trim() : "")); // ports
out = run("docker", "container", "inspect", "--format", "{{json .}}", "--", name);
} catch (DockerException e) {
if (e.getMessage() != null && e.getMessage().toLowerCase().contains("no such")) {
return Optional.empty();
}
throw e;
}
if (out.isBlank()) return Optional.empty();
try {
return Optional.of(JSON.readTree(out));
} catch (Exception e) {
throw new DockerException("Could not read the details of container '" + name + "': " + e.getMessage()
+ "\n" + CHECK_DOCKER);
}
}
public boolean isImagePresent(String image) throws DockerException {