Skip to content

Commit

Permalink
configOptions: add allowMissingConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Dec 31, 2023
1 parent 0d7d033 commit 1210de1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions include/hyprlang.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ namespace Hyprlang {
Return all errors instead of just the first
*/
bool throwAllErrors = false;

/*!
@since 0.2.0
Don't throw on a missing config file. Carry on as if nothing happened.
*/
bool allowMissingConfig = false;
};

/*!
Expand Down
17 changes: 14 additions & 3 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ CConfig::CConfig(const char* path, const Hyprlang::SConfigOptions& options) {
impl = new CConfigImpl;
try {
impl->path = std::filesystem::canonical(path);
} catch (std::exception& e) { throw "Couldn't open file. File does not exist"; }
} catch (std::exception& e) {
if (!options.allowMissingConfig)
throw "Couldn't open file. File does not exist";
}

if (!std::filesystem::exists(impl->path)) {
if (!options.allowMissingConfig)
throw "File does not exist";

if (!std::filesystem::exists(impl->path))
throw "File does not exist";
impl->path = "";
}

impl->envVariables.clear();
for (char** env = environ; *env; ++env) {
Expand Down Expand Up @@ -496,6 +503,10 @@ CParseResult CConfig::parse() {
applyDefaultsToCat(*sc);
}

// implies options.allowMissingConfig
if (impl->path.empty())
return CParseResult{};

CParseResult fileParseResult = parseFile(impl->path);

return fileParseResult;
Expand Down

0 comments on commit 1210de1

Please sign in to comment.