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
@@ -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