fix(profile): apply --profile to every command, not just config show (#23)

`--profile <name>` was declared on the shared GlobalOptions mixin but only
ConfigShowCommand ever read it. Every other command, `start` included, fell
through to the built-in defaults, so a profile's container, image, port,
persistDir and services were silently dropped.

The worst consequence was silent data loss: the `-v <dir>:/app/data` mount and
`FLOCI_STORAGE_MODE=persistent` were gated on the `--persist` flag alone, so a
profile with `persistDir` set produced neither. The image declares
`VOLUME /app/data`, so every start attached a fresh anonymous volume and the
server stayed in-memory.

A profile is now applied by a single picocli IDefaultValueProvider registered in
FlociCli.buildCommandLine(). Picocli consults it only for options the user did
not pass, and only after that command's arguments are processed, which gives the
documented precedence without per-command plumbing:

    command-line flag > --profile <name> > FLOCI_* env var > product default

Behaviour changes:

- An unknown, unreadable or invalid profile is now an error (exit 2) rather than
  a silent fall back to the defaults. `--help` still works with a bad profile.
- `restart --profile <name>` re-applies the profile's persistDir, image, port
  and services. It builds its StartCommand programmatically, so it carries the
  profile across by hand.
- `config show` merges instead of replacing, so an explicit --endpoint /
  --container / -o is no longer discarded when --profile resolves. It now also
  reports the profile's start-only fields.
- `config profile create` seeds the file from the tree it was run under;
  previously every tree wrote AWS values.
- Profile names are validated, closing a path traversal
  (`config profile delete ../../foo` resolved the raw name).
- Reads accept the `.yml` spelling that `config profile list` has always
  listed, and `list` backfills a missing `name:` from the file name.
- The readiness poll after `start` keeps the endpoint's host when swapping in
  the bound port, instead of always rewriting it to localhost.

Adds 64 tests, including the first coverage of ProfileStore.

Closes #22
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-13 18:23:56 -05:00
1 parent 588edc235a
commit 52c08ecec5
19 files changed
+1193 -62

No files matched your search

@@ -2,9 +2,13 @@ package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.config.Profile;
import io.floci.cli.config.ProfileStore;
import io.floci.cli.output.Printer;
import picocli.CommandLine.*;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.Callable;
@Command(
@@ -16,6 +20,8 @@ public class RestartCommand implements Callable<Integer> {
protected final ProductProfile profile;
private final ProfileStore store;
@Mixin
protected GlobalOptions global;
@@ -24,7 +30,13 @@ public class RestartCommand implements Callable<Integer> {
}
protected RestartCommand(ProductProfile profile) {
this(profile, new ProfileStore());
}
/** Test seam: a store pointed at a temporary profiles directory. */
public RestartCommand(ProductProfile profile, ProfileStore store) {
this.profile = profile;
this.store = store;
this.global = new GlobalOptions(profile);
}
@@ -42,12 +54,40 @@ public class RestartCommand implements Callable<Integer> {
// Minimal wait to avoid port-already-in-use races
try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
return buildStartCommand().call();
}
/**
* The {@code start} this restart will run. Picocli never parses this instance, so the profile
* that {@code ProfileDefaultValueProvider} applied to the parsed command line has to be
* carried across by hand — without it, {@code restart --profile x} would silently drop the
* profile's persistence directory and service list.
*/
public StartCommand buildStartCommand() {
StartCommand start = new StartCommand(profile);
start.global = global;
start.port = profile.defaultPort();
start.pull = "missing";
start.image = profile.defaultImageRef();
start.detach = false;
return start.call();
// port and image already hold the product defaults, set by the StartCommand constructor.
if (global.profile != null) {
loadProfile().ifPresent(p -> {
if (p.port != null) start.port = p.port;
if (p.image != null) start.image = p.image;
if (p.persistDir != null) start.persistDir = p.persistDir;
if (p.services != null) start.services = p.services;
});
}
return start;
}
private Optional<Profile> loadProfile() {
try {
return store.get(global.profile);
} catch (IOException | IllegalArgumentException e) {
// Parsing already resolved this name, so a failure here cannot happen in practice;
// keeping the product defaults beats aborting a restart that has already stopped.
return Optional.empty();
}
}
}