mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
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:
1 parent
588edc235a
commit
52c08ecec5
19 files changed
+1193
-62
No files matched your search
@@ -8,6 +8,9 @@ import io.floci.cli.commands.oci.OciCommand;
|
||||
import io.floci.cli.commands.config.ConfigCommand;
|
||||
import io.floci.cli.commands.snapshot.SnapshotCommand;
|
||||
import io.floci.cli.config.GlobalConfigStore;
|
||||
import io.floci.cli.config.ProfileDefaultValueProvider;
|
||||
import io.floci.cli.config.ProfileNotFoundException;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.update.UpdateNotifier;
|
||||
import picocli.CommandLine;
|
||||
@@ -102,10 +105,7 @@ public class FlociCli implements Runnable {
|
||||
UpdateNotifier.refreshInBackground();
|
||||
}
|
||||
|
||||
int exitCode = new CommandLine(new FlociCli())
|
||||
.setExecutionExceptionHandler(new ExceptionHandler())
|
||||
.setCaseInsensitiveEnumValuesAllowed(true)
|
||||
.execute(effectiveArgs);
|
||||
int exitCode = buildCommandLine(new ProfileStore()).execute(effectiveArgs);
|
||||
|
||||
if (notify) {
|
||||
// Fast commands would otherwise exit before the background check persists
|
||||
@@ -115,6 +115,37 @@ public class FlociCli implements Runnable {
|
||||
System.exit(exitCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* The fully configured root command. Shared by {@link #main(String[])} and the tests so that
|
||||
* profile resolution, the exception handlers and enum leniency cannot drift apart.
|
||||
*
|
||||
* The default-value provider is registered programmatically on purpose: the annotation form
|
||||
* ({@code @Command(defaultValueProvider = ...)}) makes picocli instantiate it reflectively,
|
||||
* which would need a native-image reflect-config entry.
|
||||
*/
|
||||
public static CommandLine buildCommandLine(ProfileStore store) {
|
||||
CommandLine cmd = new CommandLine(new FlociCli())
|
||||
.setExecutionExceptionHandler(new ExceptionHandler())
|
||||
.setCaseInsensitiveEnumValuesAllowed(true)
|
||||
.setDefaultValueProvider(new ProfileDefaultValueProvider(store));
|
||||
return cmd.setParameterExceptionHandler(profileAwareParameterExceptionHandler(cmd));
|
||||
}
|
||||
|
||||
/**
|
||||
* A bad {@code --profile} is a user error, not a usage error: print the message on its own
|
||||
* and skip picocli's usage dump. Everything else keeps picocli's default handling.
|
||||
*/
|
||||
static IParameterExceptionHandler profileAwareParameterExceptionHandler(CommandLine cmd) {
|
||||
IParameterExceptionHandler fallback = cmd.getParameterExceptionHandler();
|
||||
return (ex, args) -> {
|
||||
if (ex instanceof ProfileNotFoundException) {
|
||||
System.err.println(Ansi.red("Error: ") + ex.getMessage());
|
||||
return ExitCode.USAGE;
|
||||
}
|
||||
return fallback.handleParseException(ex, args);
|
||||
};
|
||||
}
|
||||
|
||||
// Returns true when routing should NOT apply: explicit product subgroup,
|
||||
// or a product-independent command like "config" or "completion".
|
||||
private static boolean isExplicitProduct(String[] args) {
|
||||
|
||||
Reference in new issue
Block a user