From a8808ab00244cbc84a00860ff2c45813cc3118c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Thu, 11 Feb 2021 09:25:05 +0100 Subject: [PATCH] load config from args --- .../org/xbib/content/config/ConfigLoader.java | 38 ++++++++++++++++++- gradle.properties | 2 +- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/content-config/src/main/java/org/xbib/content/config/ConfigLoader.java b/content-config/src/main/java/org/xbib/content/config/ConfigLoader.java index 478d930..3264116 100644 --- a/content-config/src/main/java/org/xbib/content/config/ConfigLoader.java +++ b/content-config/src/main/java/org/xbib/content/config/ConfigLoader.java @@ -8,6 +8,8 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -35,6 +37,14 @@ public class ConfigLoader { this.logger = logger; } + public Settings.Builder loadSettings(String[] args, + ClassLoader classLoader, + String applicationName, + String... fileNamesWithoutSuffix) throws IOException { + Settings.Builder settings = createSettingsFromArgs(args, applicationName, fileNamesWithoutSuffix); + return settings != null ? settings : loadSettings(classLoader, applicationName, fileNamesWithoutSuffix); + } + public Settings.Builder loadSettings(ClassLoader classLoader, String applicationName, String... fileNamesWithoutSuffix) throws IOException { @@ -63,6 +73,22 @@ public class ConfigLoader { Arrays.asList(fileNamesWithoutSuffix)); } + private Settings.Builder createSettingsFromArgs(String[] args, + String applicationName, + String... fileNamesWithoutSuffix) throws IOException { + for (String fileNameWithoutSuffix : fileNamesWithoutSuffix) { + for (String suffix : List.of(YML, YAML, JSON)) { + for (int i = 0; i < args.length - 1; i++) { + String arg = args[i]; + if (arg.equals("--" + applicationName + "-" + fileNameWithoutSuffix + suffix)) { + return createSettingsFromReader(new StringReader(args[i + 1]), suffix); + } + } + } + } + return null; + } + private Settings.Builder createSettingsFromStdin() throws IOException { if (System.in != null) { int numBytesWaiting = System.in.available(); @@ -112,12 +138,20 @@ public class ConfigLoader { logger.error("unable to open input stream"); return null; } + return createSettingsFromReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8), suffix); + } + + private Settings.Builder createSettingsFromReader(Reader reader, String suffix) throws IOException { + if (reader == null) { + logger.error("unable to open reader"); + return null; + } SettingsLoader settingsLoader = isYaml(suffix) ? new YamlSettingsLoader() : isJson(suffix) ? new JsonSettingsLoader() : null; if (settingsLoader != null) { Settings.Builder settings; - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { - String content = reader.lines().collect(Collectors.joining("\n")); + try (BufferedReader bufferedReader = new BufferedReader(reader)) { + String content = bufferedReader.lines().collect(Collectors.joining("\n")); settings = Settings.settingsBuilder().put(settingsLoader.load(content)); } return settings; diff --git a/gradle.properties b/gradle.properties index a07df56..20b8b84 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ group = org.xbib name = content -version = 2.6.3 +version = 2.6.4 gradle.wrapper.version = 6.6.1 xbib.net.version = 2.1.0