config: Check if config path exists (#2026)

Closes: #2016

* update: Checks if the configuration file exists

* Update: Removing the logic of the config file search from main.cpp
This commit is contained in:
Lucas Araújo 2020-03-01 21:03:17 +00:00 committed by GitHub
parent f02fb67020
commit 512c519f25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 4 deletions

View File

@ -108,6 +108,7 @@ namespace file_util {
bool is_fifo(const string& filename);
vector<string> glob(string pattern);
const string expand(const string& path);
string get_config_path();
template <typename... Args>
decltype(auto) make_file_descriptor(Args&&... args) {

View File

@ -106,11 +106,10 @@ int main(int argc, char** argv) {
if (cli->has("config")) {
confpath = cli->get("config");
} else if (env_util::has("XDG_CONFIG_HOME")) {
confpath = env_util::get("XDG_CONFIG_HOME") + "/polybar/config";
} else if (env_util::has("HOME")) {
confpath = env_util::get("HOME") + "/.config/polybar/config";
} else {
confpath = file_util::get_config_path();
}
if (confpath.empty()) {
throw application_error("Define configuration using --config=PATH");
}

View File

@ -274,6 +274,26 @@ namespace file_util {
}
return ret;
}
/*
* Search for polybar config and returns the path if found
*/
string get_config_path() {
string confpath;
if (env_util::has("XDG_CONFIG_HOME")) {
confpath = env_util::get("XDG_CONFIG_HOME") + "/polybar/config";
if (exists(confpath)) {
return confpath;
}
}
if (env_util::has("HOME")) {
confpath = env_util::get("HOME") + "/.config/polybar/config";
if (exists(confpath)) {
return confpath;
}
}
return "";
}
} // namespace file_util
POLYBAR_NS_END