feat(cli): honour --json and -o json in config profile, config default-product and the snapshot commands (#68)

* feat(cli): honour --json and -o json in config profile, config default-product and the snapshot commands

config profile show, create, edit and delete, config default-product and snapshot save, load and delete accepted --json and printed text. They now print one document: show uses the same keys as a row of list, and the action commands report what they did and to which file or endpoint.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* feat(cli): keep an editor's output off stdout when config profile edit prints JSON

The editor shared the command's stdout, so anything it or its wrapper printed landed in front of the result document. With structured output its stdout now goes to stderr. The README no longer lists env among the text-only commands: it prints a map in JSON and YAML.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* docs(readme): say which commands print a document when they exit 1

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-11 21:26:22 -05:00
1 parent c65987abba
commit 5bcbc0988e
9 files changed
+303 -13

No files matched your search

@@ -3,11 +3,14 @@ package io.floci.cli.commands.config;
import io.floci.cli.GlobalOptions;
import io.floci.cli.config.GlobalConfigStore;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import io.floci.cli.util.Causes;
import picocli.CommandLine.*;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
@Command(
@@ -20,6 +23,17 @@ public class ConfigDefaultProductCommand implements Callable<Integer> {
@Mixin
GlobalOptions global;
private final GlobalConfigStore store;
public ConfigDefaultProductCommand() {
this(new GlobalConfigStore());
}
/** Test seam: a store pointed at a temporary config file. */
public ConfigDefaultProductCommand(GlobalConfigStore store) {
this.store = store;
}
@Parameters(index = "0", description = "Product to use by default: aws, az, gcp, oci", paramLabel = "aws|az|gcp|oci")
String product;
@@ -30,9 +44,15 @@ public class ConfigDefaultProductCommand implements Callable<Integer> {
printer.error("Unknown product '" + product + "'.\nUse one of: aws, az, gcp, oci.");
return ExitCode.USAGE; // a value the command cannot accept, like any other bad argument
}
GlobalConfigStore store = new GlobalConfigStore();
try {
store.setDefaultProduct(product);
if (printer.format() != OutputFormat.text) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("defaultProduct", product);
result.put("file", store.configFilePath().toString());
printer.structured(result);
return 0;
}
printer.println(Ansi.green("Default product set to: ") + Ansi.bold(product));
printer.println(Ansi.gray("'floci start' will now run: floci " + product + " start"));
printer.println(Ansi.gray("Config saved to: " + store.configFilePath()));
@@ -120,16 +120,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
if (printer.format() != OutputFormat.text) {
List<Map<String, Object>> out = new ArrayList<>();
for (Profile p : profiles) {
Map<String, Object> row = new LinkedHashMap<>();
row.put("name", p.name);
row.put("container", p.container);
row.put("port", p.port);
row.put("endpoint", p.endpoint);
row.put("image", p.image);
row.put("persistDir", p.persistDir);
row.put("services", p.services);
row.put("namespace", p.namespace);
out.add(row);
out.add(row(p));
}
printer.structured(out);
return 0;
@@ -156,6 +147,30 @@ public class ConfigProfileCommand implements Callable<Integer> {
}
}
// One profile as structured output: the same keys from list and show, null where unset.
private static Map<String, Object> row(Profile p) {
Map<String, Object> row = new LinkedHashMap<>();
row.put("name", p.name);
row.put("container", p.container);
row.put("port", p.port);
row.put("endpoint", p.endpoint);
row.put("image", p.image);
row.put("persistDir", p.persistDir);
row.put("services", p.services);
row.put("namespace", p.namespace);
return row;
}
// What create, edit and delete report in structured output: the profile, what was done to it
// and the file or files involved.
private static Map<String, Object> done(String name, String action, List<Path> files) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("name", name);
result.put("action", action);
result.put("files", files.stream().map(Path::toString).toList());
return result;
}
private int showProfile(Printer printer, ProfileStore store) {
if (name == null) { printer.error(nameRequired()); return ExitCode.USAGE; }
try {
@@ -166,6 +181,10 @@ public class ConfigProfileCommand implements Callable<Integer> {
}
Profile p = profile.get();
ProfileDefaultValueProvider.warnAboutUnknownKeys(store, name, p);
if (printer.format() != OutputFormat.text) {
printer.structured(row(p));
return 0;
}
printer.println(Ansi.bold("Profile: ") + p.name);
if (p.endpoint != null) printer.println(" endpoint: " + p.endpoint);
if (p.container != null) printer.println(" container: " + p.container);
@@ -207,7 +226,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
command.add(file.toString());
int exit;
try {
exit = new ProcessBuilder(command).inheritIO().start().waitFor();
exit = editorProcess(command, printer.format() != OutputFormat.text).start().waitFor();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
printer.error("Interrupted while " + editor.get(0) + " was open.\nRe-run '" + prefix + "edit " + name + "'.");
@@ -236,10 +255,32 @@ public class ConfigProfileCommand implements Callable<Integer> {
+ "Re-run '" + prefix + "edit " + name + "' to fix it.");
return 1;
}
if (printer.format() != OutputFormat.text) {
printer.structured(done(name, "edited", List.of(file)));
return 0;
}
printer.println("Profile '" + name + "' saved " + Ansi.gray("(" + file + ")"));
return 0;
}
/**
* How the editor is started. It shares this command's terminal, so an interactive editor
* works. With structured output its stdout goes to stderr instead: stdout carries the one
* result document, and anything an editor or its wrapper prints there would break it.
*/
public static ProcessBuilder editorProcess(List<String> command, boolean structured) {
ProcessBuilder editor = new ProcessBuilder(command).inheritIO();
if (structured) {
// /dev/stderr is this process's own stderr, still the terminal when only stdout is
// captured. Where there is no such file (Windows), the editor's output is dropped.
Path stderr = Path.of("/dev/stderr");
editor.redirectOutput(Files.isWritable(stderr)
? ProcessBuilder.Redirect.appendTo(stderr.toFile())
: ProcessBuilder.Redirect.DISCARD);
}
return editor;
}
/**
* The editor to run, as a command and its arguments: {@code VISUAL} first, then
* {@code EDITOR}, as other tools do. Empty when neither is set.
@@ -325,6 +366,10 @@ public class ConfigProfileCommand implements Callable<Integer> {
printer.warn(warning);
}
store.save(p);
if (printer.format() != OutputFormat.text) {
printer.structured(done(name, "created", List.of(store.profileFile(name))));
return 0;
}
printer.println(Ansi.green("Created") + " profile '" + name + "' at " + store.profileFile(name));
printer.println(Ansi.gray("Start it with: " + profile.commandPrefix() + " start --profile " + name));
return 0;
@@ -398,6 +443,10 @@ public class ConfigProfileCommand implements Callable<Integer> {
String files = removed.size() > 1
? " (" + removed.get(0).getFileName() + " and " + removed.get(1).getFileName() + ")"
: "";
if (printer.format() != OutputFormat.text) {
printer.structured(done(name, "deleted", removed));
return 0;
}
printer.println(Ansi.green("Deleted") + " profile '" + name + "'" + files);
return 0;
} catch (IOException e) {
@@ -6,9 +6,12 @@ import io.floci.cli.docker.DockerClient;
import io.floci.cli.http.FlociException;
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 picocli.CommandLine.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
@Command(
@@ -49,6 +52,14 @@ public class SnapshotDeleteCommand implements Callable<Integer> {
FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile);
try {
client.deleteSnapshot(name);
if (printer.format() != OutputFormat.text) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("snapshot", name);
result.put("action", "deleted");
result.put("endpoint", effectiveEndpoint);
printer.structured(result);
return 0;
}
printer.println(Ansi.green("Snapshot deleted:") + " " + name);
return 0;
} catch (FlociException e) {
@@ -6,9 +6,12 @@ import io.floci.cli.docker.DockerClient;
import io.floci.cli.http.FlociException;
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 picocli.CommandLine.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
@Command(
@@ -49,6 +52,14 @@ public class SnapshotLoadCommand implements Callable<Integer> {
FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile);
try {
client.loadSnapshot(name);
if (printer.format() != OutputFormat.text) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("snapshot", name);
result.put("action", "loaded");
result.put("endpoint", effectiveEndpoint);
printer.structured(result);
return 0;
}
printer.println(Ansi.green("Snapshot loaded:") + " " + name);
return 0;
} catch (FlociException e) {
@@ -6,9 +6,12 @@ import io.floci.cli.docker.DockerClient;
import io.floci.cli.http.FlociException;
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 picocli.CommandLine.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
@Command(
@@ -56,6 +59,14 @@ public class SnapshotSaveCommand implements Callable<Integer> {
}
try {
client.postSnapshot(name);
if (printer.format() != OutputFormat.text) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("snapshot", name);
result.put("action", "saved");
result.put("endpoint", effectiveEndpoint);
printer.structured(result);
return 0;
}
printer.println(Ansi.green("Snapshot saved:") + " " + name);
return 0;
} catch (FlociException e) {