Skip to content

Commit ab0acd0

Browse files
authored
Merge pull request #3862 from emar10/config-include-wildcards
Allow using wildcards in config include paths
2 parents 4ec1218 + d1dac28 commit ab0acd0

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

include/config.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class Config {
2020
static std::optional<std::string> findConfigPath(
2121
const std::vector<std::string> &names, const std::vector<std::string> &dirs = CONFIG_DIRS);
2222

23-
static std::optional<std::string> tryExpandPath(const std::string &base,
24-
const std::string &filename);
23+
static std::vector<std::string> tryExpandPath(const std::string &base,
24+
const std::string &filename);
2525

2626
Config() = default;
2727

src/ALabel.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ ALabel::ALabel(const Json::Value& config, const std::string& name, const std::st
6868

6969
// there might be "~" or "$HOME" in original path, try to expand it.
7070
auto result = Config::tryExpandPath(menuFile, "");
71-
if (!result.has_value()) {
71+
if (result.empty()) {
7272
throw std::runtime_error("Failed to expand file: " + menuFile);
7373
}
7474

75-
menuFile = result.value();
75+
menuFile = result.front();
7676
// Read the menu descriptor file
7777
std::ifstream file(menuFile);
7878
if (!file.is_open()) {

src/config.cpp

+20-14
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const std::vector<std::string> Config::CONFIG_DIRS = {
2121

2222
const char *Config::CONFIG_PATH_ENV = "WAYBAR_CONFIG_DIR";
2323

24-
std::optional<std::string> Config::tryExpandPath(const std::string &base,
25-
const std::string &filename) {
24+
std::vector<std::string> Config::tryExpandPath(const std::string &base,
25+
const std::string &filename) {
2626
fs::path path;
2727

2828
if (!filename.empty()) {
@@ -33,33 +33,35 @@ std::optional<std::string> Config::tryExpandPath(const std::string &base,
3333

3434
spdlog::debug("Try expanding: {}", path.string());
3535

36+
std::vector<std::string> results;
3637
wordexp_t p;
3738
if (wordexp(path.c_str(), &p, 0) == 0) {
38-
if (access(*p.we_wordv, F_OK) == 0) {
39-
std::string result = *p.we_wordv;
40-
wordfree(&p);
41-
spdlog::debug("Found config file: {}", path.string());
42-
return result;
39+
for (size_t i = 0; i < p.we_wordc; i++) {
40+
if (access(p.we_wordv[i], F_OK) == 0) {
41+
results.emplace_back(p.we_wordv[i]);
42+
spdlog::debug("Found config file: {}", p.we_wordv[i]);
43+
}
4344
}
4445
wordfree(&p);
4546
}
46-
return std::nullopt;
47+
48+
return results;
4749
}
4850

4951
std::optional<std::string> Config::findConfigPath(const std::vector<std::string> &names,
5052
const std::vector<std::string> &dirs) {
5153
if (const char *dir = std::getenv(Config::CONFIG_PATH_ENV)) {
5254
for (const auto &name : names) {
53-
if (auto res = tryExpandPath(dir, name); res) {
54-
return res;
55+
if (auto res = tryExpandPath(dir, name); !res.empty()) {
56+
return res.front();
5557
}
5658
}
5759
}
5860

5961
for (const auto &dir : dirs) {
6062
for (const auto &name : names) {
61-
if (auto res = tryExpandPath(dir, name); res) {
62-
return res;
63+
if (auto res = tryExpandPath(dir, name); !res.empty()) {
64+
return res.front();
6365
}
6466
}
6567
}
@@ -92,11 +94,15 @@ void Config::resolveConfigIncludes(Json::Value &config, int depth) {
9294
if (includes.isArray()) {
9395
for (const auto &include : includes) {
9496
spdlog::info("Including resource file: {}", include.asString());
95-
setupConfig(config, tryExpandPath(include.asString(), "").value_or(""), ++depth);
97+
for (const auto &match : tryExpandPath(include.asString(), "")) {
98+
setupConfig(config, match, depth + 1);
99+
}
96100
}
97101
} else if (includes.isString()) {
98102
spdlog::info("Including resource file: {}", includes.asString());
99-
setupConfig(config, tryExpandPath(includes.asString(), "").value_or(""), ++depth);
103+
for (const auto &match : tryExpandPath(includes.asString(), "")) {
104+
setupConfig(config, match, depth + 1);
105+
}
100106
}
101107
}
102108

0 commit comments

Comments
 (0)