Add support for reading the config path from env

This commit adds support to reading the config base path from the
environment variable `WAYBAR_CONFIG_DIR`. If it is set, but no
configuration is found there, it falls back to the previous mechanism
of using the default paths, without erroring.
This commit is contained in:
Blallo 2022-06-25 12:16:40 +02:00
parent 4deb6d812d
commit eb017347b8
No known key found for this signature in database
GPG Key ID: 0CBE577C9B72DC3F
2 changed files with 29 additions and 5 deletions

View File

@ -14,6 +14,7 @@ namespace waybar {
class Config {
public:
static const std::vector<std::string> CONFIG_DIRS;
static const char *CONFIG_PATH_ENV;
/* Try to find any of provided names in the supported set of config directories */
static std::optional<std::string> findConfigPath(

View File

@ -4,11 +4,14 @@
#include <unistd.h>
#include <wordexp.h>
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include "util/json.hpp"
namespace fs = std::filesystem;
namespace waybar {
const std::vector<std::string> Config::CONFIG_DIRS = {
@ -16,12 +19,25 @@ const std::vector<std::string> Config::CONFIG_DIRS = {
"/etc/xdg/waybar/", SYSCONFDIR "/xdg/waybar/", "./resources/",
};
std::optional<std::string> tryExpandPath(const std::string &path) {
const char *Config::CONFIG_PATH_ENV = "WAYBAR_CONFIG_DIR";
std::optional<std::string> tryExpandPath(const std::string base, const std::string filename) {
fs::path path;
if (filename != "") {
path = fs::path(base) / fs::path(filename);
} else {
path = fs::path(base);
}
spdlog::debug("Try expanding: {}", path);
wordexp_t p;
if (wordexp(path.c_str(), &p, 0) == 0) {
if (access(*p.we_wordv, F_OK) == 0) {
std::string result = *p.we_wordv;
wordfree(&p);
spdlog::debug("Found config file: {}", path);
return result;
}
wordfree(&p);
@ -31,10 +47,17 @@ std::optional<std::string> tryExpandPath(const std::string &path) {
std::optional<std::string> Config::findConfigPath(const std::vector<std::string> &names,
const std::vector<std::string> &dirs) {
std::vector<std::string> paths;
if (const char *dir = std::getenv(Config::CONFIG_PATH_ENV)) {
for (const auto &name : names) {
if (auto res = tryExpandPath(dir, name); res) {
return res;
}
}
}
for (const auto &dir : dirs) {
for (const auto &name : names) {
if (auto res = tryExpandPath(dir + name); res) {
if (auto res = tryExpandPath(dir, name); res) {
return res;
}
}
@ -68,11 +91,11 @@ void Config::resolveConfigIncludes(Json::Value &config, int depth) {
if (includes.isArray()) {
for (const auto &include : includes) {
spdlog::info("Including resource file: {}", include.asString());
setupConfig(config, tryExpandPath(include.asString()).value_or(""), ++depth);
setupConfig(config, tryExpandPath(include.asString(), "").value_or(""), ++depth);
}
} else if (includes.isString()) {
spdlog::info("Including resource file: {}", includes.asString());
setupConfig(config, tryExpandPath(includes.asString()).value_or(""), ++depth);
setupConfig(config, tryExpandPath(includes.asString(), "").value_or(""), ++depth);
}
}