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
@@ -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) + ".");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user