Merge pull request #4373 from Arkoniak/4354-wildcard-config-regression
bugfix: expanding wildcards in config includes (#4354)
This commit is contained in:
@ -35,7 +35,8 @@ class Config {
|
||||
void setupConfig(Json::Value &dst, const std::string &config_file, int depth);
|
||||
void resolveConfigIncludes(Json::Value &config, int depth);
|
||||
void mergeConfig(Json::Value &a_config_, Json::Value &b_config_);
|
||||
static std::optional<std::string> findIncludePath(const std::string &name);
|
||||
static std::vector<std::string> findIncludePath(
|
||||
const std::string &name, const std::vector<std::string> &dirs = CONFIG_DIRS);
|
||||
|
||||
std::string config_file_;
|
||||
|
||||
|
@ -106,12 +106,24 @@ void Config::setupConfig(Json::Value &dst, const std::string &config_file, int d
|
||||
mergeConfig(dst, tmp_config);
|
||||
}
|
||||
|
||||
std::optional<std::string> Config::findIncludePath(const std::string &name) {
|
||||
std::vector<std::string> Config::findIncludePath(const std::string &name,
|
||||
const std::vector<std::string> &dirs) {
|
||||
auto match1 = tryExpandPath(name, "");
|
||||
if (!match1.empty()) {
|
||||
return match1.front();
|
||||
return match1;
|
||||
}
|
||||
return findConfigPath({name});
|
||||
if (const char *dir = std::getenv(Config::CONFIG_PATH_ENV)) {
|
||||
if (auto res = tryExpandPath(dir, name); !res.empty()) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
for (const auto &dir : dirs) {
|
||||
if (auto res = tryExpandPath(dir, name); !res.empty()) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void Config::resolveConfigIncludes(Json::Value &config, int depth) {
|
||||
@ -119,18 +131,22 @@ void Config::resolveConfigIncludes(Json::Value &config, int depth) {
|
||||
if (includes.isArray()) {
|
||||
for (const auto &include : includes) {
|
||||
spdlog::info("Including resource file: {}", include.asString());
|
||||
auto match = findIncludePath(include.asString());
|
||||
if (match.has_value()) {
|
||||
setupConfig(config, match.value(), depth + 1);
|
||||
auto matches = findIncludePath(include.asString());
|
||||
if (!matches.empty()) {
|
||||
for (const auto &match : matches) {
|
||||
setupConfig(config, match, depth + 1);
|
||||
}
|
||||
} else {
|
||||
spdlog::warn("Unable to find resource file: {}", include.asString());
|
||||
}
|
||||
}
|
||||
} else if (includes.isString()) {
|
||||
spdlog::info("Including resource file: {}", includes.asString());
|
||||
auto match = findIncludePath(includes.asString());
|
||||
if (match.has_value()) {
|
||||
setupConfig(config, match.value(), depth + 1);
|
||||
auto matches = findIncludePath(includes.asString());
|
||||
if (!matches.empty()) {
|
||||
for (const auto &match : matches) {
|
||||
setupConfig(config, match, depth + 1);
|
||||
}
|
||||
} else {
|
||||
spdlog::warn("Unable to find resource file: {}", includes.asString());
|
||||
}
|
||||
|
@ -84,6 +84,33 @@ TEST_CASE("Load simple config with include", "[config]") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Load simple config with wildcard include", "[config]") {
|
||||
waybar::Config conf;
|
||||
conf.load("test/config/include-wildcard.json");
|
||||
|
||||
auto& data = conf.getConfig();
|
||||
SECTION("validate cpu include file") { REQUIRE(data["cpu"]["format"].asString() == "goo"); }
|
||||
SECTION("validate memory include file") { REQUIRE(data["memory"]["format"].asString() == "foo"); }
|
||||
}
|
||||
|
||||
TEST_CASE("Load config using relative paths and wildcards", "[config]") {
|
||||
waybar::Config conf;
|
||||
|
||||
const char* old_config_path = std::getenv(waybar::Config::CONFIG_PATH_ENV);
|
||||
setenv(waybar::Config::CONFIG_PATH_ENV, "test/config", 1);
|
||||
|
||||
conf.load("test/config/include-relative-path.json");
|
||||
|
||||
auto& data = conf.getConfig();
|
||||
SECTION("validate cpu include file") { REQUIRE(data["cpu"]["format"].asString() == "goo"); }
|
||||
SECTION("validate memory include file") { REQUIRE(data["memory"]["format"].asString() == "foo"); }
|
||||
|
||||
if (old_config_path)
|
||||
setenv(waybar::Config::CONFIG_PATH_ENV, old_config_path, 1);
|
||||
else
|
||||
unsetenv(waybar::Config::CONFIG_PATH_ENV);
|
||||
}
|
||||
|
||||
TEST_CASE("Load multiple bar config with include", "[config]") {
|
||||
waybar::Config conf;
|
||||
conf.load("test/config/include-multi.json");
|
||||
|
5
test/config/include-relative-path.json
Normal file
5
test/config/include-relative-path.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"include": ["modules/*.jsonc"],
|
||||
"position": "top",
|
||||
"nullOption": null
|
||||
}
|
5
test/config/include-wildcard.json
Normal file
5
test/config/include-wildcard.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"include": ["test/config/modules/*.jsonc"],
|
||||
"position": "top",
|
||||
"nullOption": null
|
||||
}
|
6
test/config/modules/cpu.jsonc
Normal file
6
test/config/modules/cpu.jsonc
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"cpu": {
|
||||
"interval": 2,
|
||||
"format": "goo"
|
||||
}
|
||||
}
|
6
test/config/modules/memory.jsonc
Normal file
6
test/config/modules/memory.jsonc
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"memory": {
|
||||
"interval": 2,
|
||||
"format": "foo",
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user