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

@@ -0,0 +1,124 @@
package io.floci.cli.config;
import io.floci.cli.output.OutputFormat;
import picocli.CommandLine;
import picocli.CommandLine.IDefaultValueProvider;
import picocli.CommandLine.Model.ArgSpec;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Model.OptionSpec;
import java.io.IOException;
import java.util.Optional;
/**
* Applies a named config profile as the default value of every option it covers.
*
* Registered once on the root {@code CommandLine}; picocli propagates it to the whole subcommand
* hierarchy. Picocli consults a default-value provider only for options that were NOT matched on
* the command line, and only after the command's own arguments have been processed — which gives
* the precedence this CLI documents, for free:
*
* <pre>CLI flag &gt; profile &gt; environment variable &gt; product default</pre>
*
* The environment variable and product default live in the {@code GlobalOptions} field initial
* values, which picocli keeps whenever this provider returns {@code null}.
*/
public class ProfileDefaultValueProvider implements IDefaultValueProvider {
private final ProfileStore store;
// Resolved once per name: the provider is called for every unmatched argument, including
// the positionals and the parent commands, which carry no --profile of their own.
private String resolvedName;
private Profile resolved;
public ProfileDefaultValueProvider() {
this(new ProfileStore());
}
/** Test seam: a store pointed at a temporary profiles directory. */
public ProfileDefaultValueProvider(ProfileStore store) {
this.store = store;
}
@Override
public String defaultValue(ArgSpec arg) {
// Positional parameters reach the provider too; only options map to profile fields.
if (!(arg instanceof OptionSpec option)) return null;
CommandSpec command = arg.command();
// Group commands, the root, and `update` carry no GlobalOptions mixin.
OptionSpec profileOption = command.findOption("--profile");
if (profileOption == null) return null;
String name = profileOption.getValue();
if (name == null) return null;
// Defaults are applied even when -h/-V was requested, so a bad profile must not stop
// `floci start --profile typo --help` from printing its usage.
if (helpRequested(command)) return null;
Profile profile = resolve(name, command.commandLine());
for (String optionName : option.names()) {
String value = ProfileDefaults.valueFor(profile, optionName);
if (value != null) return value;
}
return null;
}
private static boolean helpRequested(CommandSpec command) {
for (CommandSpec spec = command; spec != null; spec = spec.parent()) {
for (OptionSpec option : spec.options()) {
if ((option.usageHelp() || option.versionHelp()) && Boolean.TRUE.equals(option.getValue())) {
return true;
}
}
}
return false;
}
private Profile resolve(String name, CommandLine commandLine) {
if (name.equals(resolvedName)) return resolved;
Optional<Profile> loaded;
try {
loaded = store.get(name);
} catch (IllegalArgumentException e) {
throw new ProfileNotFoundException(commandLine, e.getMessage());
} catch (IOException e) {
throw invalid(commandLine, name, e.getMessage());
}
if (loaded.isEmpty()) {
throw new ProfileNotFoundException(commandLine,
"Profile '" + name + "' not found in " + store.profilesDir() + ".\n"
+ "Run 'floci config profile list' to see available profiles, "
+ "or 'floci config profile create " + name + "' to create it.");
}
Profile profile = loaded.get();
validate(commandLine, name, profile);
resolvedName = name;
resolved = profile;
return resolved;
}
// Picocli would otherwise reject these while converting, in a message that blames an option
// the user never typed and never names the file the value actually came from.
private void validate(CommandLine commandLine, String name, Profile profile) {
if (profile.port != null && (profile.port < 1 || profile.port > 65535)) {
throw invalid(commandLine, name, "port must be between 1 and 65535, but was " + profile.port);
}
if (profile.output != null) {
try {
OutputFormat.valueOf(profile.output.toLowerCase());
} catch (IllegalArgumentException e) {
throw invalid(commandLine, name, "output must be text, json or yaml, but was '" + profile.output + "'");
}
}
}
private ProfileNotFoundException invalid(CommandLine commandLine, String name, String detail) {
return new ProfileNotFoundException(commandLine,
"Profile '" + name + "' is invalid: " + detail + "\n"
+ "Edit " + store.profileFile(name) + " and try again.");
}
}