From 66d247ca5ba26a061bf0bcfe4f2bb3b66c193ee9 Mon Sep 17 00:00:00 2001 From: Robert Griebl Date: Wed, 22 Mar 2023 15:38:30 +0100 Subject: Make it possible to specify directories to read config files from This extends the existing -c/--config-file option to also accept directories, effectively expanding the argument to all *.yaml files in the given directory. Change-Id: I83f197e8f67ba363bf27b0f511c7821402bb7033 Reviewed-by: Bernd Weimer (cherry picked from commit 6cc5230fd783802159aa3ca94c74b7ee1b27203a) --- doc/configuration.qdoc | 3 +++ src/main-lib/configuration.cpp | 13 ++++++++++++- tests/configuration/tst_configuration.cpp | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/configuration.qdoc b/doc/configuration.qdoc index d968652e..72a2dd04 100644 --- a/doc/configuration.qdoc +++ b/doc/configuration.qdoc @@ -121,6 +121,9 @@ or across multiple config files, the final value is resolved based on these rule configuration files into a cache. This cache is loaded on subsequent starts, if the exact set of config files is used, unchanged. + In case the given argument is a directory, all files with a \c{.yaml} extension will be + loaded from that directory in alphabetical order (ASCII sort order, non-recursive). + (default: \c /opt/am/config.yaml) \row \li \b --no-cache diff --git a/src/main-lib/configuration.cpp b/src/main-lib/configuration.cpp index 1e6f6d16..dceb9b64 100644 --- a/src/main-lib/configuration.cpp +++ b/src/main-lib/configuration.cpp @@ -327,7 +327,18 @@ void Configuration::parseWithArguments(const QStringList &arguments) timer.start(); #endif - QStringList configFilePaths = m_clp.values(qSL("config-file")); + const QStringList rawConfigFilePaths = m_clp.values(qSL("config-file")); + QStringList configFilePaths; + configFilePaths.reserve(rawConfigFilePaths.size()); + for (const auto &path : rawConfigFilePaths) { + if (QFileInfo(path).isDir()) { + const auto entries = QDir(path).entryInfoList({ qSL("*.yaml") }, QDir::Files, QDir::Name); + for (const auto &entry : entries) + configFilePaths << entry.filePath(); + } else { + configFilePaths << path; + } + } AbstractConfigCache::Options cacheOptions = AbstractConfigCache::MergedResult; if (noCache()) diff --git a/tests/configuration/tst_configuration.cpp b/tests/configuration/tst_configuration.cpp index 1383fcd8..278f4d78 100644 --- a/tests/configuration/tst_configuration.cpp +++ b/tests/configuration/tst_configuration.cpp @@ -272,7 +272,7 @@ void tst_Configuration::simpleConfig() void tst_Configuration::mergedConfig() { - Configuration c({ qSL(":/data/config1.yaml"), qSL(":/data/config2.yaml") }, qSL(":/build-config.yaml")); + Configuration c({ qSL(":/data/") }, qSL(":/build-config.yaml")); c.parseWithArguments({ qSL("test"), qSL("--no-cache") }); QVERIFY(c.noCache()); -- cgit v1.2.1