fix(core): Output more details about defined bars at failure

This commit is contained in:
Michael Carlberg 2016-06-20 18:44:03 +02:00
parent 3adcb5fd61
commit d530da0d50
3 changed files with 20 additions and 11 deletions

View File

@ -31,6 +31,8 @@ namespace config
std::string build_path(const std::string& section, const std::string& key); std::string build_path(const std::string& section, const std::string& key);
std::string get_file_path();
template<typename T> template<typename T>
T dereference_var(const std::string& ref_section, const std::string& ref_key, const std::string& var, const T ref_val) T dereference_var(const std::string& ref_section, const std::string& ref_key, const std::string& var, const T ref_val)
{ {

View File

@ -108,26 +108,25 @@ int main(int argc, char **argv)
*/ */
std::vector<std::string> defined_bars; std::vector<std::string> defined_bars;
for (auto &section : config::get_tree()) { for (auto &section : config::get_tree()) {
if (section.first.find("bar/") == 0) if (std::strncmp("bar/", section.first.c_str(), 4) == 0)
defined_bars.emplace_back(section.first); defined_bars.emplace_back(section.first.substr(4));
} }
if (defined_bars.empty()) if (defined_bars.empty())
logger->fatal("There are no bars defined in the config"); 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); config::set_bar_path(config_path);
if (std::find(defined_bars.begin(), defined_bars.end(), config_path) == defined_bars.end()) { if (std::find(defined_bars.begin(), defined_bars.end(), bar_name) == defined_bars.end()) {
logger->error("The bar \""+ config_path.substr(4) +"\" is not defined in the config"); logger->error("The bar \""+ bar_name +"\" is not defined");
logger->info("Available bars:"); logger->info("Available bars in "+ config::get_file_path() +": "+ string::join(defined_bars, ", "));
for (auto &bar : defined_bars) logger->info(" "+ bar.substr(4)); return EXIT_FAILURE;
std::exit(EXIT_FAILURE);
} }
if (config::get_tree().get_child_optional(config_path) == boost::none) { if (config::get_tree().get_child_optional(config_path) == boost::none)
logger->fatal("Bar \""+ std::string(argv[1]) +"\" does not exist"); logger->fatal("Bar \""+ bar_name +"\" does not exist");
}
/** /**
* Dump specified config value * Dump specified config value

View File

@ -55,4 +55,12 @@ namespace config
std::string build_path(const std::string& section, const std::string& key) { std::string build_path(const std::string& section, const std::string& key) {
return section +"."+ 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;
}
} }