Path: simplify

This commit is contained in:
Mihai Fufezan 2024-09-10 19:59:07 +03:00
parent 4fb47606be
commit 360fb14385
Signed by: fufexan
SSH key fingerprint: SHA256:SdnKmEpJrDu1+2UO1QpB/Eg4HKcdDi6n+xSRqFNJVpg

View file

@ -51,36 +51,42 @@ namespace Hyprutils::Path {
using T = std::optional<std::string>; using T = std::optional<std::string>;
std::pair<T, T> findConfig(std::string programName) { std::pair<T, T> findConfig(std::string programName) {
std::string configPath;
bool xdgConfigHomeExists = false; bool xdgConfigHomeExists = false;
static const auto xdgConfigHome = getXdgConfigHome(); static const auto xdgConfigHome = getXdgConfigHome();
if (xdgConfigHome.has_value()) { if (xdgConfigHome.has_value()) {
xdgConfigHomeExists = true; xdgConfigHomeExists = true;
if (checkConfigExists(xdgConfigHome.value(), programName)) configPath = fullConfigPath(xdgConfigHome.value(), programName);
return std::make_pair(fullConfigPath(xdgConfigHome.value(), programName), xdgConfigHome); if (std::filesystem::exists(configPath))
Debug::log(INFO, "No config file could be found in {}/{}", xdgConfigHome.value(), programName); return std::make_pair(configPath, xdgConfigHome);
Debug::log(INFO, "No config file {}", configPath);
} }
bool homeExists = false; bool homeExists = false;
static const auto home = getHome(); static const auto home = getHome();
if (home.has_value()) { if (home.has_value()) {
homeExists = true; homeExists = true;
if (checkConfigExists(home.value(), programName)) configPath = fullConfigPath(home.value(), programName);
return std::make_pair(fullConfigPath(home.value(), programName), home); if (std::filesystem::exists(configPath))
Debug::log(INFO, "No config file could be found in {}/{}", home.value(), programName); return std::make_pair(configPath, home);
Debug::log(INFO, "No config file {}", configPath);
} }
static const auto xdgConfigDirs = getXdgConfigDirs(); static const auto xdgConfigDirs = getXdgConfigDirs();
if (xdgConfigDirs.has_value()) { if (xdgConfigDirs.has_value()) {
for (auto dir : xdgConfigDirs.value()) { for (auto dir : xdgConfigDirs.value()) {
if (checkConfigExists(dir, programName)) configPath = fullConfigPath(dir, programName);
return std::make_pair(fullConfigPath(dir, programName), std::nullopt); if (std::filesystem::exists(configPath))
Debug::log(INFO, "No config file could be found in {}/{}", dir, programName); return std::make_pair(configPath, std::nullopt);
Debug::log(INFO, "No config file {}", configPath);
} }
} }
if (checkConfigExists("/etc/xdg", programName)) configPath = fullConfigPath("/etc/xdg", programName);
return std::make_pair(fullConfigPath("/etc/xdg", programName), std::nullopt); if (std::filesystem::exists(configPath))
Debug::log(INFO, "No config file could be found in /etc/xdg/{}", programName); return std::make_pair(configPath, std::nullopt);
Debug::log(INFO, "No config file {}", configPath);
if (xdgConfigHomeExists) if (xdgConfigHomeExists)
return std::make_pair(std::nullopt, xdgConfigHome); return std::make_pair(std::nullopt, xdgConfigHome);