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:
parent
7de71943ad
commit
61b0b586cf
7 changed files with 74 additions and 63 deletions
|
@ -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
|
||||
|
|
|
@ -20,35 +20,32 @@
|
|||
#include <yaml-cpp/yaml.h>
|
||||
#include <ciso646>
|
||||
#include <wordexp.h>
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
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<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;
|
||||
}
|
||||
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<std::string>();
|
||||
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
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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")) {
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
#include <ciso646>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <boost/range/algorithm/copy.hpp>
|
||||
|
||||
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;
|
||||
|
|
|
@ -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<std::string>());
|
||||
|
|
|
@ -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<std::string>();
|
||||
|
|
Loading…
Reference in a new issue