mirror of
https://github.com/hyprwm/hyprutils.git
synced 2024-11-17 04:55:59 +01:00
Path: simplify path checking
This commit is contained in:
parent
82816f0a77
commit
01f69fab15
1 changed files with 22 additions and 35 deletions
|
@ -2,6 +2,7 @@
|
||||||
#include <hyprutils/string/VarList.hpp>
|
#include <hyprutils/string/VarList.hpp>
|
||||||
#include <hyprutils/debug/Log.hpp>
|
#include <hyprutils/debug/Log.hpp>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
using namespace Hyprutils;
|
using namespace Hyprutils;
|
||||||
|
|
||||||
|
@ -10,10 +11,6 @@ namespace Hyprutils::Path {
|
||||||
return basePath + "/hypr/" + programName + ".conf";
|
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() {
|
std::optional<std::string> getHome() {
|
||||||
static const auto homeDir = getenv("HOME");
|
static const auto homeDir = getenv("HOME");
|
||||||
|
|
||||||
|
@ -51,46 +48,36 @@ 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;
|
std::string configPath;
|
||||||
|
std::set<std::string> paths;
|
||||||
|
|
||||||
bool xdgConfigHomeExists = false;
|
static const auto xdgConfigHome = getXdgConfigHome();
|
||||||
static const auto xdgConfigHome = getXdgConfigHome();
|
if (xdgConfigHome.has_value())
|
||||||
if (xdgConfigHome.has_value()) {
|
paths.insert(xdgConfigHome.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool homeExists = false;
|
static const auto home = getHome();
|
||||||
static const auto home = getHome();
|
if (home.has_value())
|
||||||
if (home.has_value()) {
|
paths.insert(home.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 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())
|
||||||
configPath = fullConfigPath(dir, programName);
|
paths.insert(dir);
|
||||||
if (std::filesystem::exists(configPath))
|
|
||||||
return std::make_pair(configPath, std::nullopt);
|
|
||||||
Debug::log(LOG, "No config file {}", configPath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
configPath = fullConfigPath("/etc/xdg", programName);
|
paths.insert("/etc/xdg");
|
||||||
if (std::filesystem::exists(configPath))
|
|
||||||
return std::make_pair(configPath, std::nullopt);
|
|
||||||
Debug::log(LOG, "No config file {}", configPath);
|
|
||||||
|
|
||||||
if (xdgConfigHomeExists)
|
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 (xdgConfigHome.has_value())
|
||||||
return std::make_pair(std::nullopt, xdgConfigHome);
|
return std::make_pair(std::nullopt, xdgConfigHome);
|
||||||
else if (homeExists)
|
else if (home.has_value())
|
||||||
return std::make_pair(std::nullopt, home);
|
return std::make_pair(std::nullopt, home);
|
||||||
|
|
||||||
Debug::log(ERR, "No config file could be found. Check previous logs for clues");
|
Debug::log(ERR, "No config file could be found. Check previous logs for clues");
|
||||||
|
|
Loading…
Reference in a new issue