mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
feat(profile): warn when a profile sets a key floci does not read (#37)
This commit is contained in:
1 parent
01d6133548
commit
22668a1200
7 files changed
+125
No files matched your search
@@ -697,6 +697,8 @@ So `floci start --profile probe --container other` starts `other`, and a profile
|
||||
`FLOCI_CONTAINER` exported in your shell. A field the profile leaves out changes nothing.
|
||||
|
||||
An unknown or unreadable profile is an error (exit 2), not a silent fall back to the defaults.
|
||||
A key floci does not read (a typo such as `persist_dir:`) is ignored with a warning that lists
|
||||
the keys a profile can set.
|
||||
Values are interpolated by the CLI, so `persistDir: ${env:HOME}/floci-data` expands as you would
|
||||
expect.
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.floci.cli.commands.config;
|
||||
import io.floci.cli.GlobalOptions;
|
||||
import io.floci.cli.ProductProfile;
|
||||
import io.floci.cli.config.Profile;
|
||||
import io.floci.cli.config.ProfileDefaultValueProvider;
|
||||
import io.floci.cli.config.ProfileStore;
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
@@ -148,6 +149,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
|
||||
var profile = store.get(name);
|
||||
if (profile.isEmpty()) { printer.error("Profile '" + name + "' not found."); return 1; }
|
||||
Profile p = profile.get();
|
||||
ProfileDefaultValueProvider.warnAboutUnknownKeys(store, name, p);
|
||||
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);
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package io.floci.cli.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.floci.cli.ProductProfile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Profile {
|
||||
public String name;
|
||||
@@ -15,8 +20,27 @@ public class Profile {
|
||||
public String namespace;
|
||||
public String output;
|
||||
|
||||
/** The keys a profile file may set: every field above. Pinned against them by a test. */
|
||||
public static final List<String> KNOWN_KEYS = List.of(
|
||||
"name", "endpoint", "container", "image", "port", "persistDir", "services", "namespace", "output");
|
||||
|
||||
// Unknown keys stay readable for forward compatibility, but are recorded so a misspelled one
|
||||
// ('persist_dir') can be reported instead of silently dropping the setting.
|
||||
@JsonIgnore
|
||||
private final List<String> unknownKeys = new ArrayList<>();
|
||||
|
||||
public Profile() {}
|
||||
|
||||
@JsonAnySetter
|
||||
void unknownKey(String key, Object value) {
|
||||
unknownKeys.add(key);
|
||||
}
|
||||
|
||||
/** Keys the file set that no field reads, in file order. */
|
||||
public List<String> unknownKeys() {
|
||||
return List.copyOf(unknownKeys);
|
||||
}
|
||||
|
||||
/** Seeds a new profile with the defaults of the product tree it was created under. */
|
||||
public Profile(ProductProfile product, String name) {
|
||||
this.name = name;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.cli.config;
|
||||
|
||||
import io.floci.cli.output.Ansi;
|
||||
import io.floci.cli.output.OutputFormat;
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.IDefaultValueProvider;
|
||||
@@ -9,6 +10,8 @@ import picocli.CommandLine.Model.OptionSpec;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Applies a named config profile as the default value of every option it covers.
|
||||
@@ -125,6 +128,8 @@ public class ProfileDefaultValueProvider implements IDefaultValueProvider {
|
||||
Profile profile = loaded.get();
|
||||
validate(commandLine, name, profile);
|
||||
|
||||
warnAboutUnknownKeys(store, name, profile);
|
||||
|
||||
resolvedName = name;
|
||||
resolved = profile;
|
||||
return resolved;
|
||||
@@ -150,4 +155,22 @@ public class ProfileDefaultValueProvider implements IDefaultValueProvider {
|
||||
"Profile '" + name + "' is invalid: " + detail + "\n"
|
||||
+ "Edit " + store.existingFile(name) + " and try again.");
|
||||
}
|
||||
|
||||
// A command can resolve the same profile through more than one provider (config show reads it
|
||||
// back after the parse), so the warning is printed once per profile file per run.
|
||||
private static final Set<String> WARNED = ConcurrentHashMap.newKeySet();
|
||||
|
||||
/**
|
||||
* Prints one stderr warning for keys {@code profile} sets that floci does not read. Shared by
|
||||
* every path that loads a profile ({@code --profile}, {@code config profile show}).
|
||||
*/
|
||||
public static void warnAboutUnknownKeys(ProfileStore store, String name, Profile profile) {
|
||||
if (profile.unknownKeys().isEmpty()) return;
|
||||
if (!WARNED.add(store.profilesDir().toAbsolutePath() + "/" + name)) return;
|
||||
System.err.println(Ansi.yellow("Warning: ") + "Profile '" + name + "' sets "
|
||||
+ String.join(", ", profile.unknownKeys().stream().map(k -> "'" + k + "'").toList())
|
||||
+ ", which floci does not read, so " + (profile.unknownKeys().size() == 1 ? "it is" : "they are")
|
||||
+ " ignored.\nCheck the spelling; the keys a profile can set are: "
|
||||
+ String.join(", ", Profile.KNOWN_KEYS) + ".");
|
||||
}
|
||||
}
|
||||
@@ -321,4 +321,35 @@ class ConfigCommandsTest {
|
||||
assertTrue(out.contains("\"container\" : \"floci-snap\""), out);
|
||||
assertTrue(out.contains("\"persistDir\" : \"/from-the-parse\""), out);
|
||||
}
|
||||
|
||||
/** config profile show reads the file directly; a misspelled key must still be reported. */
|
||||
@Test
|
||||
void profileShowWarnsAboutAMisspelledKey() throws Exception {
|
||||
writeProfile("typo-key", "container: floci-typo\npersist_dir: /data\n");
|
||||
|
||||
Run r = profileCmd(ProductProfile.AWS, "show", "typo-key");
|
||||
|
||||
assertEquals(0, r.exit());
|
||||
assertTrue(r.err().contains("'persist_dir'"), r.err());
|
||||
}
|
||||
|
||||
/** config show reads the profile once and reports a misspelled key exactly once. */
|
||||
@Test
|
||||
void configShowWarnsOnceAboutAMisspelledKey() throws Exception {
|
||||
writeProfile("typo-twice", "container: floci-typo\npersist_dir: /data\n");
|
||||
CommandLine cmd = FlociCli.buildCommandLine(store());
|
||||
PrintStream out = System.out;
|
||||
PrintStream err = System.err;
|
||||
ByteArrayOutputStream errBuf = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(new ByteArrayOutputStream()));
|
||||
System.setErr(new PrintStream(errBuf));
|
||||
try {
|
||||
assertEquals(0, cmd.execute("config", "show", "--profile", "typo-twice"));
|
||||
} finally {
|
||||
System.setOut(out);
|
||||
System.setErr(err);
|
||||
}
|
||||
|
||||
assertEquals(1, errBuf.toString().split("'persist_dir'", -1).length - 1, errBuf.toString());
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,23 @@ class MissingProfileTest {
|
||||
assertTrue(out.toString().contains("enforced"), out.toString()); // help wraps the image value
|
||||
}
|
||||
|
||||
/** BL-013: a misspelled key is still ignored, but no longer silently. */
|
||||
@Test
|
||||
void aMisspelledKeyIsReportedOnceAndTheProfileStillApplies() throws Exception {
|
||||
writeProfile("typo-key", "container: floci-typo\npersist_dir: /data\n");
|
||||
|
||||
int[] exit = new int[1];
|
||||
String err = captureStderr(() -> {
|
||||
cli().parseArgs("start", "--profile", "typo-key");
|
||||
return 0;
|
||||
}, exit);
|
||||
|
||||
assertEquals(0, exit[0]);
|
||||
assertTrue(err.contains("Profile 'typo-key' sets 'persist_dir'"), err);
|
||||
assertTrue(err.contains("persistDir"), err);
|
||||
assertEquals(1, err.split("Warning").length - 1, err);
|
||||
}
|
||||
|
||||
@Test
|
||||
void helpStillWorksWithABadProfile() {
|
||||
int[] exit = new int[1];
|
||||
|
||||
@@ -10,8 +10,11 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -206,4 +209,27 @@ class ProfileStoreTest {
|
||||
assertTrue(e.getMessage().contains("'..\u0000x'"), e.getMessage());
|
||||
assertFalse(e.getMessage().contains(".yaml"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownKeysAreRecordedButNeverWrittenBack() throws Exception {
|
||||
Files.createDirectories(tempDir);
|
||||
Files.writeString(tempDir.resolve("typo.yaml"), "name: typo\npersist_dir: /data\nport: 4599\n");
|
||||
|
||||
Profile p = store().get("typo").orElseThrow();
|
||||
assertEquals(List.of("persist_dir"), p.unknownKeys());
|
||||
assertEquals(4599, p.port);
|
||||
|
||||
store().save(p);
|
||||
assertFalse(Files.readString(tempDir.resolve("typo.yaml")).contains("persist_dir"));
|
||||
assertFalse(Files.readString(tempDir.resolve("typo.yaml")).contains("unknownKeys"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void knownKeysAreExactlyTheProfileFields() {
|
||||
List<String> fields = Arrays.stream(Profile.class.getFields())
|
||||
.filter(f -> !Modifier.isStatic(f.getModifiers()))
|
||||
.map(Field::getName)
|
||||
.sorted().toList();
|
||||
assertEquals(fields, Profile.KNOWN_KEYS.stream().sorted().toList());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user