diff --git a/include/dindexer-common/settings.hpp b/include/dindexer-common/settings.hpp index 11f295f..d115c9a 100644 --- a/include/dindexer-common/settings.hpp +++ b/include/dindexer-common/settings.hpp @@ -28,7 +28,7 @@ namespace dinlib { dindb::BackendPlugin backend_plugin; }; - bool load_settings ( const std::string& parPath, Settings& parOut, bool parExpand=true ); + void load_settings ( const std::string& parPath, Settings& parOut, bool parExpand=true ); } //namespace dinlib #endif diff --git a/src/common/settings.cpp b/src/common/settings.cpp index fa65122..23eeb7d 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -20,35 +20,32 @@ #include #include #include +#include +#include namespace dinlib { namespace { std::string expand ( const char* parString ); std::string find_plugin_by_name ( const std::string& parName ); + void throw_if_plugin_failed ( const dindb::BackendPlugin& parPlugin, const std::string& parPluginPath, const std::string& parIntendedName ); } //unnamed namespace - bool load_settings (const std::string& parPath, dinlib::Settings& parOut, bool parExpand) { + void load_settings (const std::string& parPath, dinlib::Settings& parOut, bool parExpand) { const std::string path = (parExpand ? expand(parPath.c_str()) : parPath); - try { - auto settings = YAML::LoadFile(path); + auto settings = YAML::LoadFile(path); - if (not settings["backend_name"]) { - return false; - } - parOut.backend_name = settings["backend_name"].as(); - const std::string backend_settings_section = parOut.backend_name + "_settings"; - if (settings[backend_settings_section]) { - auto settings_node = settings[backend_settings_section]; - parOut.backend_plugin = dindb::BackendPlugin(find_plugin_by_name(parOut.backend_name), &settings_node); - return true; - } + if (not settings["backend_name"]) { + throw std::runtime_error("No backend_name given in the config file"); } - catch (const std::exception&) { - return false; + parOut.backend_name = settings["backend_name"].as(); + const std::string backend_settings_section = parOut.backend_name + "_settings"; + if (settings[backend_settings_section]) { + auto settings_node = settings[backend_settings_section]; + const std::string plugin_path = find_plugin_by_name(parOut.backend_name); + parOut.backend_plugin = dindb::BackendPlugin(plugin_path, &settings_node); + throw_if_plugin_failed(parOut.backend_plugin, plugin_path, parOut.backend_name); } - - return false; } namespace { @@ -73,5 +70,29 @@ namespace dinlib { assert(dindb::backend_name(path) == parName); return path; } + + void throw_if_plugin_failed (const dindb::BackendPlugin& parPlugin, const std::string& parPluginPath, const std::string& parIntendedName) { + if (not parPlugin.is_loaded()) { + std::ostringstream oss; + oss << "Unable to load plugin \"" << parIntendedName << + "\" found at path \"" << parPluginPath << '"'; + throw std::runtime_error(oss.str()); + } + if (parPlugin.name() != parIntendedName) { + std::ostringstream oss; + oss << "Plugin \"" << parIntendedName << "\" not found." << + " Plugin at path \"" << parPluginPath << "\" reports \"" << + parPlugin.name() << "\" as its name"; + throw std::runtime_error(oss.str()); + } + if (parPlugin.max_supported_interface_version() < parPlugin.backend_interface_version()) { + std::ostringstream oss; + oss << "Plugin \"" << parPlugin.name() << "\" at path \"" << + parPluginPath << "\" uses interface version " << parPlugin.backend_interface_version() << + " but the maximum supported interface version is " << + parPlugin.max_supported_interface_version(); + throw std::runtime_error(oss.str()); + } + } } //unnamed namespace } //namespace dinlib diff --git a/src/delete/main.cpp b/src/delete/main.cpp index 0c42fb2..1e458ab 100644 --- a/src/delete/main.cpp +++ b/src/delete/main.cpp @@ -61,16 +61,14 @@ int main (int parArgc, char* parArgv[]) { } dinlib::Settings settings; - { - const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); - if (not loaded) { - std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n"; - return 1; - } + try { + dinlib::load_settings(CONFIG_FILE_PATH, settings); + } + catch (const std::runtime_error& err) { + std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ":\n"; + std::cerr << err.what() << '\n'; + return 1; } - //TODO: throw if plugin loading failed - assert(settings.backend_plugin.name() == settings.backend_name); - assert(settings.backend_plugin.is_loaded()); if (not vm.count("groupid")) { std::cerr << "No IDs specified\n"; diff --git a/src/locate/main.cpp b/src/locate/main.cpp index 34edb87..860a7a5 100644 --- a/src/locate/main.cpp +++ b/src/locate/main.cpp @@ -71,16 +71,14 @@ int main (int parArgc, char* parArgv[]) { } dinlib::Settings settings; - { - const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); - if (not loaded) { - std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n"; - return 1; - } + try { + dinlib::load_settings(CONFIG_FILE_PATH, settings); + } + catch (const std::runtime_error& err) { + std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ":\n"; + std::cerr << err.what() << '\n'; + return 1; } - //TODO: throw if plugin loading failed - assert(settings.backend_plugin.name() == settings.backend_name); - assert(settings.backend_plugin.is_loaded()); auto& db = settings.backend_plugin.backend(); if (vm.count("set")) { diff --git a/src/navigate/main.cpp b/src/navigate/main.cpp index fdcb835..c0436d5 100644 --- a/src/navigate/main.cpp +++ b/src/navigate/main.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include namespace { @@ -52,16 +51,14 @@ int main (int parArgc, char* parArgv[]) { } dinlib::Settings settings; - { - const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); - if (not loaded) { - std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n"; - return 1; - } + try { + dinlib::load_settings(CONFIG_FILE_PATH, settings); + } + catch (const std::runtime_error& err) { + std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ":\n"; + std::cerr << err.what() << '\n'; + return 1; } - //TODO: throw if plugin loading failed - assert(settings.backend_plugin.name() == settings.backend_name); - assert(settings.backend_plugin.is_loaded()); do_navigation(settings.backend_plugin.backend()); return 0; diff --git a/src/scan/main.cpp b/src/scan/main.cpp index d739066..e500e02 100644 --- a/src/scan/main.cpp +++ b/src/scan/main.cpp @@ -73,16 +73,15 @@ int main (int parArgc, char* parArgv[]) { #endif dinlib::Settings settings; - { - const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); - if (not loaded) { - std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n"; - return 1; + try { + if (din::parse_commandline(parArgc, parArgv, vm)) { + return 0; } } - //TODO: throw if plugin loading failed - assert(settings.backend_plugin.name() == settings.backend_name); - assert(settings.backend_plugin.is_loaded()); + catch (const std::invalid_argument& err) { + std::cerr << err.what() << "\nUse --help for help" << std::endl; + return 2; + } bool ignore_read_errors = (vm.count("ignore-errors") > 0); const std::string search_path(vm["search-path"].as()); diff --git a/src/tag/main.cpp b/src/tag/main.cpp index 5195640..454bc43 100644 --- a/src/tag/main.cpp +++ b/src/tag/main.cpp @@ -147,15 +147,13 @@ int main (int parArgc, char* parArgv[]) { assert(id_mode xor glob_mode); dinlib::Settings settings; - { - const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); - if (not loaded) { - std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n"; - return 1; - } - //TODO: throw if plugin loading failed - assert(settings.backend_plugin.name() == settings.backend_name); - assert(settings.backend_plugin.is_loaded()); + try { + dinlib::load_settings(CONFIG_FILE_PATH, settings); + } + catch (const std::runtime_error& err) { + std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ":\n"; + std::cerr << err.what() << '\n'; + return 1; } const auto master_tags_string = vm["tags"].as();