From 512c519f2566255be9388986dcead784c5b022d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ara=C3=BAjo?= Date: Sun, 1 Mar 2020 21:03:17 +0000 Subject: [PATCH] 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 --- include/utils/file.hpp | 1 + src/main.cpp | 7 +++---- src/utils/file.cpp | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/include/utils/file.hpp b/include/utils/file.hpp index 439c41ee..2fc37355 100644 --- a/include/utils/file.hpp +++ b/include/utils/file.hpp @@ -108,6 +108,7 @@ namespace file_util { bool is_fifo(const string& filename); vector glob(string pattern); const string expand(const string& path); + string get_config_path(); template decltype(auto) make_file_descriptor(Args&&... args) { diff --git a/src/main.cpp b/src/main.cpp index f0048310..c3f1d441 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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"); } diff --git a/src/utils/file.cpp b/src/utils/file.cpp index 56f6b552..97fc27d5 100644 --- a/src/utils/file.cpp +++ b/src/utils/file.cpp @@ -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