Exposes environment variables as config settings by mapping "section.option" to "SECTION_OPTION". Dashes and periods in the options are converted to underscores, and all characters are upper-cased assuming a US English locale.
| 31 | * upper-cased assuming a US English locale. |
| 32 | */ |
| 33 | public class EnvConfig implements Config { |
| 34 | |
| 35 | @Override |
| 36 | public Optional<List<String>> getAll(String section, String option) { |
| 37 | Require.nonNull("Section name", section); |
| 38 | Require.nonNull("Option name", option); |
| 39 | |
| 40 | String key = |
| 41 | String.format("%s_%s", section, option) |
| 42 | .toUpperCase(Locale.ENGLISH) |
| 43 | .replace("-", "_") |
| 44 | .replace(".", "_"); |
| 45 | |
| 46 | String value = System.getenv().get(key); |
| 47 | if (value == null) { |
| 48 | return Optional.empty(); |
| 49 | } |
| 50 | |
| 51 | if (value.startsWith("$")) { |
| 52 | value = System.getenv(value.substring(1)); |
| 53 | } |
| 54 | |
| 55 | return Optional.ofNullable(value).map(List::of); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Set<String> getSectionNames() { |
| 60 | return System.getenv().keySet().stream() |
| 61 | // We need at least two "_" characters |
| 62 | .filter(key -> key.split("_").length > 1) |
| 63 | .map(key -> key.substring(0, key.indexOf('_'))) |
| 64 | .map(key -> key.toLowerCase(Locale.ENGLISH)) |
| 65 | .collect(toSortedSet()); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Set<String> getOptions(String section) { |
| 70 | Require.nonNull("Section name to get options for", section); |
| 71 | |
| 72 | String prefix = String.format("%s_", section).toUpperCase(Locale.ENGLISH); |
| 73 | return System.getenv().keySet().stream() |
| 74 | .filter(key -> key.startsWith(prefix)) |
| 75 | .map(key -> key.substring(prefix.length())) |
| 76 | .map(key -> key.toLowerCase(Locale.ENGLISH)) |
| 77 | .collect(toSortedSet()); |
| 78 | } |
| 79 | } |
nothing calls this directly
no outgoing calls
no test coverage detected