1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-25 00:53:43 +00:00

Clean up code to handle failed attempts at loading backend plugins.

This commit is contained in:
King_DuckZ 2016-06-02 21:16:19 +02:00
parent 7de71943ad
commit 61b0b586cf
7 changed files with 74 additions and 63 deletions

View file

@ -28,7 +28,7 @@ namespace dinlib {
dindb::BackendPlugin backend_plugin; 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 } //namespace dinlib
#endif #endif

View file

@ -20,35 +20,32 @@
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
#include <ciso646> #include <ciso646>
#include <wordexp.h> #include <wordexp.h>
#include <stdexcept>
#include <sstream>
namespace dinlib { namespace dinlib {
namespace { namespace {
std::string expand ( const char* parString ); std::string expand ( const char* parString );
std::string find_plugin_by_name ( const std::string& parName ); 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 } //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); 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"]) { if (not settings["backend_name"]) {
return false; throw std::runtime_error("No backend_name given in the config file");
}
parOut.backend_name = settings["backend_name"].as<std::string>();
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;
}
} }
catch (const std::exception&) { parOut.backend_name = settings["backend_name"].as<std::string>();
return false; 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 { namespace {
@ -73,5 +70,29 @@ namespace dinlib {
assert(dindb::backend_name(path) == parName); assert(dindb::backend_name(path) == parName);
return path; 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 } //unnamed namespace
} //namespace dinlib } //namespace dinlib

View file

@ -61,16 +61,14 @@ int main (int parArgc, char* parArgv[]) {
} }
dinlib::Settings settings; dinlib::Settings settings;
{ try {
const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); dinlib::load_settings(CONFIG_FILE_PATH, settings);
if (not loaded) { }
std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n"; catch (const std::runtime_error& err) {
return 1; 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")) { if (not vm.count("groupid")) {
std::cerr << "No IDs specified\n"; std::cerr << "No IDs specified\n";

View file

@ -71,16 +71,14 @@ int main (int parArgc, char* parArgv[]) {
} }
dinlib::Settings settings; dinlib::Settings settings;
{ try {
const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); dinlib::load_settings(CONFIG_FILE_PATH, settings);
if (not loaded) { }
std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n"; catch (const std::runtime_error& err) {
return 1; 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(); auto& db = settings.backend_plugin.backend();
if (vm.count("set")) { if (vm.count("set")) {

View file

@ -26,7 +26,6 @@
#include <ciso646> #include <ciso646>
#include <string> #include <string>
#include <vector> #include <vector>
#include <cassert>
#include <boost/range/algorithm/copy.hpp> #include <boost/range/algorithm/copy.hpp>
namespace { namespace {
@ -52,16 +51,14 @@ int main (int parArgc, char* parArgv[]) {
} }
dinlib::Settings settings; dinlib::Settings settings;
{ try {
const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); dinlib::load_settings(CONFIG_FILE_PATH, settings);
if (not loaded) { }
std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n"; catch (const std::runtime_error& err) {
return 1; 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()); do_navigation(settings.backend_plugin.backend());
return 0; return 0;

View file

@ -73,16 +73,15 @@ int main (int parArgc, char* parArgv[]) {
#endif #endif
dinlib::Settings settings; dinlib::Settings settings;
{ try {
const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); if (din::parse_commandline(parArgc, parArgv, vm)) {
if (not loaded) { return 0;
std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n";
return 1;
} }
} }
//TODO: throw if plugin loading failed catch (const std::invalid_argument& err) {
assert(settings.backend_plugin.name() == settings.backend_name); std::cerr << err.what() << "\nUse --help for help" << std::endl;
assert(settings.backend_plugin.is_loaded()); return 2;
}
bool ignore_read_errors = (vm.count("ignore-errors") > 0); bool ignore_read_errors = (vm.count("ignore-errors") > 0);
const std::string search_path(vm["search-path"].as<std::string>()); const std::string search_path(vm["search-path"].as<std::string>());

View file

@ -147,15 +147,13 @@ int main (int parArgc, char* parArgv[]) {
assert(id_mode xor glob_mode); assert(id_mode xor glob_mode);
dinlib::Settings settings; dinlib::Settings settings;
{ try {
const bool loaded = dinlib::load_settings(CONFIG_FILE_PATH, settings); dinlib::load_settings(CONFIG_FILE_PATH, settings);
if (not loaded) { }
std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ", quitting\n"; catch (const std::runtime_error& err) {
return 1; std::cerr << "Can't load settings from " << CONFIG_FILE_PATH << ":\n";
} std::cerr << err.what() << '\n';
//TODO: throw if plugin loading failed return 1;
assert(settings.backend_plugin.name() == settings.backend_name);
assert(settings.backend_plugin.is_loaded());
} }
const auto master_tags_string = vm["tags"].as<std::string>(); const auto master_tags_string = vm["tags"].as<std::string>();