From d530da0d50d0f1fd92fcabc56493e1803397ff79 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Mon, 20 Jun 2016 18:44:03 +0200 Subject: [PATCH] fix(core): Output more details about defined bars at failure --- include/utils/config.hpp | 2 ++ src/lemonbuddy.cpp | 21 ++++++++++----------- src/utils/config.cpp | 8 ++++++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/utils/config.hpp b/include/utils/config.hpp index fa62419f..bf303eca 100644 --- a/include/utils/config.hpp +++ b/include/utils/config.hpp @@ -31,6 +31,8 @@ namespace config std::string build_path(const std::string& section, const std::string& key); + std::string get_file_path(); + template T dereference_var(const std::string& ref_section, const std::string& ref_key, const std::string& var, const T ref_val) { diff --git a/src/lemonbuddy.cpp b/src/lemonbuddy.cpp index 33823fcc..ea295cf7 100644 --- a/src/lemonbuddy.cpp +++ b/src/lemonbuddy.cpp @@ -108,26 +108,25 @@ int main(int argc, char **argv) */ std::vector defined_bars; for (auto §ion : config::get_tree()) { - if (section.first.find("bar/") == 0) - defined_bars.emplace_back(section.first); + if (std::strncmp("bar/", section.first.c_str(), 4) == 0) + defined_bars.emplace_back(section.first.substr(4)); } if (defined_bars.empty()) logger->fatal("There are no bars defined in the config"); - auto config_path = "bar/"+ std::string(argv[1]); + auto bar_name = std::string(argv[1]); + auto config_path = "bar/"+ bar_name; config::set_bar_path(config_path); - if (std::find(defined_bars.begin(), defined_bars.end(), config_path) == defined_bars.end()) { - logger->error("The bar \""+ config_path.substr(4) +"\" is not defined in the config"); - logger->info("Available bars:"); - for (auto &bar : defined_bars) logger->info(" "+ bar.substr(4)); - std::exit(EXIT_FAILURE); + if (std::find(defined_bars.begin(), defined_bars.end(), bar_name) == defined_bars.end()) { + logger->error("The bar \""+ bar_name +"\" is not defined"); + logger->info("Available bars in "+ config::get_file_path() +": "+ string::join(defined_bars, ", ")); + return EXIT_FAILURE; } - if (config::get_tree().get_child_optional(config_path) == boost::none) { - logger->fatal("Bar \""+ std::string(argv[1]) +"\" does not exist"); - } + if (config::get_tree().get_child_optional(config_path) == boost::none) + logger->fatal("Bar \""+ bar_name +"\" does not exist"); /** * Dump specified config value diff --git a/src/utils/config.cpp b/src/utils/config.cpp index 672cd43f..4446566a 100644 --- a/src/utils/config.cpp +++ b/src/utils/config.cpp @@ -55,4 +55,12 @@ namespace config std::string build_path(const std::string& section, const std::string& key) { return section +"."+ key; } + + std::string get_file_path() { + const char *env_home = std::getenv("HOME"); + if (env_home != nullptr) { + return string::replace(file_path, env_home, "~"); + } + return file_path; + } }