refactor(profile): read the parse's profile snapshot in restart and config show (#34)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-08 22:25:23 -05:00
1 parent 5c0cc339bf
commit bc59dff8c3
7 files changed
+90 -95

No files matched your search

@@ -3,9 +3,10 @@ package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.config.ProfileNotFoundException;
import io.floci.cli.config.ProfileStore;
import io.floci.cli.config.ProfileDefaultValueProvider;
import io.floci.cli.output.Printer;
import picocli.CommandLine.*;
import picocli.CommandLine.Model.CommandSpec;
import java.util.concurrent.Callable;
@@ -18,23 +19,18 @@ public class RestartCommand implements Callable<Integer> {
protected final ProductProfile profile;
private final ProfileStore store;
@Mixin
protected GlobalOptions global;
@Spec
CommandSpec spec;
public RestartCommand() {
this(ProductProfile.AWS);
}
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);
}
@@ -82,15 +78,12 @@ public class RestartCommand implements Callable<Integer> {
* directory on {@code start} and a different one on {@code restart}.
*/
public StartCommand buildStartCommand() {
StartCommand start = StartCommand.resolvedFor(profile, store, global.profile);
// The outer parse's provider: its memoized profile is the snapshot the globals below were
// resolved from, so the start settings cannot come from a newer version of the file.
StartCommand start = StartCommand.resolvedFor(profile, ProfileDefaultValueProvider.of(spec), global.profile);
// The real invocation's globals win: they already carry the profile plus any flag the
// user passed, resolved once by the outer parse.
//
// Known residual: those globals come from the outer parse's read of the profile while the
// fields above come from resolvedFor's, so a profile edited between the two would mix
// snapshots. Closing it means making the outer provider reachable from commands, which is
// a wider change than this fix; tracked as batman LAY-1.
start.global = global;
start.pull = "missing";
start.detach = false;
@@ -3,7 +3,6 @@ package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.config.ProfileDefaultValueProvider;
import io.floci.cli.config.ProfileStore;
import io.floci.cli.docker.DockerClient;
import io.floci.cli.docker.DockerException;
import io.floci.cli.output.Ansi;
@@ -71,15 +70,11 @@ public class StartCommand implements Callable<Integer> {
* interpolation. Anything that needs to know what a profile would start with must go through
* here rather than reading the {@link io.floci.cli.config.Profile} bean, or it silently
* disagrees with {@code start} on any interpolated value.
*/
public static StartCommand resolvedFor(ProductProfile product, ProfileStore store, String profileName) {
return resolvedFor(product, new ProfileDefaultValueProvider(store), profileName);
}
/**
* As above, but with the provider supplied so a caller can read back the one profile snapshot
* it resolved ({@link ProfileDefaultValueProvider#resolved()}) instead of reading the file a
* second time and risking a mix of two versions.
*
* <p>Pass the provider the outer parse used ({@link ProfileDefaultValueProvider#of}): it
* memoizes the profile it resolved, so this applies that same snapshot instead of reading the
* file again, and a caller can read the snapshot back through
* {@link ProfileDefaultValueProvider#resolved()}.
*/
public static StartCommand resolvedFor(ProductProfile product,
ProfileDefaultValueProvider provider,
@@ -6,11 +6,11 @@ import io.floci.cli.commands.StartCommand;
import io.floci.cli.config.Profile;
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.output.OutputFormat;
import io.floci.cli.output.Printer;
import picocli.CommandLine.*;
import picocli.CommandLine.Model.CommandSpec;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -26,23 +26,18 @@ public class ConfigShowCommand implements Callable<Integer> {
protected final ProductProfile profile;
private final ProfileStore store;
@Mixin
protected GlobalOptions global;
@Spec
CommandSpec spec;
public ConfigShowCommand() {
this(ProductProfile.AWS);
}
protected ConfigShowCommand(ProductProfile profile) {
this(profile, new ProfileStore());
}
/** Test seam: a store pointed at a temporary profiles directory. */
public ConfigShowCommand(ProductProfile profile, ProfileStore store) {
this.profile = profile;
this.store = store;
this.global = new GlobalOptions(profile);
}
@@ -85,14 +80,14 @@ public class ConfigShowCommand implements Callable<Integer> {
// interpolated persistDir reads here exactly as 'start' would use it. The bean is consulted
// for presence, so a product default never shows up looking like a profile value.
//
// Both come from ONE snapshot: the provider memoizes the profile it read, so presence and
// values cannot disagree if another process edits the file mid-command. If the profile has
// gone since the outer parse resolved it, resolvedFor throws and the command fails loudly,
// which beats printing half of an old profile next to half of a new one.
// All of it comes from ONE snapshot, the one the outer parse took for the global rows above:
// the provider is that parse's, and it memoizes the profile it read, so no row can come from a
// newer version of the file. Built by hand, outside FlociCli's wiring, the provider is fresh
// and reads the file here; if it has gone, resolvedFor throws and call() exits 2.
private void addStartSettings(Map<String, Object> data) {
if (global.profile == null) return;
ProfileDefaultValueProvider provider = new ProfileDefaultValueProvider(store);
ProfileDefaultValueProvider provider = ProfileDefaultValueProvider.of(spec);
StartCommand resolved = StartCommand.resolvedFor(profile, provider, global.profile);
Optional<Profile> declared = provider.resolved();
if (declared.isEmpty()) return;
@@ -32,15 +32,30 @@ public class ProfileDefaultValueProvider implements IDefaultValueProvider {
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;
}
/**
* The provider {@code FlociCli.buildCommandLine} registered, as picocli propagated it to
* {@code spec}'s command. A command that reads a profile back after parsing goes through here,
* so it reuses the store the CLI was built with and the snapshot the parse already took, never a
* second read of the file. A command built by hand, outside that wiring, gets a fresh provider
* over the default store.
*/
public static ProfileDefaultValueProvider of(CommandSpec spec) {
if (spec != null && spec.commandLine() != null
&& spec.commandLine().getDefaultValueProvider() instanceof ProfileDefaultValueProvider provider) {
return provider;
}
return new ProfileDefaultValueProvider(new ProfileStore());
}
/** The store profiles are read from. */
public ProfileStore store() {
return store;
}
/**
* The profile this provider resolved during the last parse, if any. Reading it costs nothing
* and, crucially, costs no second read of the file: a caller that needs both which keys the