Skip to content

Commit

Permalink
Path: simplify path checking
Browse files Browse the repository at this point in the history
  • Loading branch information
fufexan committed Sep 10, 2024
1 parent 82816f0 commit 01f69fa
Showing 1 changed file with 22 additions and 35 deletions.
57 changes: 22 additions & 35 deletions src/path/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <hyprutils/string/VarList.hpp>
#include <hyprutils/debug/Log.hpp>
#include <filesystem>
#include <set>

using namespace Hyprutils;

Expand All @@ -10,10 +11,6 @@ namespace Hyprutils::Path {
return basePath + "/hypr/" + programName + ".conf";
}

bool checkConfigExists(std::string basePath, std::string programName) {
return std::filesystem::exists(fullConfigPath(basePath, programName));
}

std::optional<std::string> getHome() {
static const auto homeDir = getenv("HOME");

Expand Down Expand Up @@ -51,46 +48,36 @@ namespace Hyprutils::Path {

using T = std::optional<std::string>;
std::pair<T, T> findConfig(std::string programName) {
std::string configPath;
std::string configPath;
std::set<std::string> paths;

bool xdgConfigHomeExists = false;
static const auto xdgConfigHome = getXdgConfigHome();
if (xdgConfigHome.has_value()) {
xdgConfigHomeExists = true;
configPath = fullConfigPath(xdgConfigHome.value(), programName);
if (std::filesystem::exists(configPath))
return std::make_pair(configPath, xdgConfigHome);
Debug::log(LOG, "No config file {}", configPath);
}
static const auto xdgConfigHome = getXdgConfigHome();
if (xdgConfigHome.has_value())
paths.insert(xdgConfigHome.value());

bool homeExists = false;
static const auto home = getHome();
if (home.has_value()) {
homeExists = true;
configPath = fullConfigPath(home.value(), programName);
if (std::filesystem::exists(configPath))
return std::make_pair(configPath, home);
Debug::log(LOG, "No config file {}", configPath);
}
static const auto home = getHome();
if (home.has_value())
paths.insert(home.value());

static const auto xdgConfigDirs = getXdgConfigDirs();
if (xdgConfigDirs.has_value()) {
for (auto dir : xdgConfigDirs.value()) {
configPath = fullConfigPath(dir, programName);
if (std::filesystem::exists(configPath))
return std::make_pair(configPath, std::nullopt);
Debug::log(LOG, "No config file {}", configPath);
}
for (auto dir : xdgConfigDirs.value())
paths.insert(dir);
}

configPath = fullConfigPath("/etc/xdg", programName);
if (std::filesystem::exists(configPath))
return std::make_pair(configPath, std::nullopt);
Debug::log(LOG, "No config file {}", configPath);
paths.insert("/etc/xdg");

for (auto path : paths) {
configPath = fullConfigPath(path, programName);
if (std::filesystem::exists(configPath))
return std::make_pair(configPath, path);
else
Debug::log(LOG, "No config found {}", configPath);
}

if (xdgConfigHomeExists)
if (xdgConfigHome.has_value())
return std::make_pair(std::nullopt, xdgConfigHome);
else if (homeExists)
else if (home.has_value())
return std::make_pair(std::nullopt, home);

Debug::log(ERR, "No config file could be found. Check previous logs for clues");
Expand Down

0 comments on commit 01f69fa

Please sign in to comment.