From abc83ee7180e2ea4c5d65689dca48bfe88023862 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 12 Oct 2025 17:30:51 +0200 Subject: feat: application registration --- .../java/moe/nea/prickly/config/ConfigCompat.java | 33 ++++++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'src/main/java/moe/nea/prickly/config/ConfigCompat.java') diff --git a/src/main/java/moe/nea/prickly/config/ConfigCompat.java b/src/main/java/moe/nea/prickly/config/ConfigCompat.java index d574759..6c94947 100644 --- a/src/main/java/moe/nea/prickly/config/ConfigCompat.java +++ b/src/main/java/moe/nea/prickly/config/ConfigCompat.java @@ -5,8 +5,8 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Map; -import java.util.Properties; +import java.util.*; +import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.jspecify.annotations.Nullable; @@ -30,11 +30,32 @@ public class ConfigCompat { return (Map) properties; } - public static Map DOTENV_FALLBACK = loadDotenv(); + public static Map DOTENV_FALLBACK = Collections.unmodifiableMap(loadDotenv()); + public static NavigableMap ENV; + + static { + var env = new TreeMap(); + env.putAll(DOTENV_FALLBACK); + env.putAll(System.getenv()); + ENV = Collections.unmodifiableNavigableMap(env); + } public static @Nullable String getEnv(String key) { - var value = System.getenv(key); - if (value != null) return value; - return DOTENV_FALLBACK.get(key); + return ENV.get(key); + } + + public static Map getAllEnvStartingWith(String prefix) { + return ENV.subMap(prefix + "_", prefix + (char) ((int) '_' + 1)); + } + + public static Set getAllDirectChildren(String prefix) { + int start = prefix.length() + 1; + return getAllEnvStartingWith(prefix).keySet().stream() + .map(it -> { + int nextUnderscore = it.indexOf('_', start); + if (nextUnderscore < 0) return it.substring(start); + else return it.substring(start, nextUnderscore); + }) + .collect(Collectors.toSet()); } } -- cgit