Join config definition and config parser

pull/1186/head
Stephen Shelton 4 years ago
parent 653a650cf9
commit 4c00c6238e
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -293,7 +293,7 @@ namespace llarp
} }
*/ */
(void)conf; (void)conf;
throw std::runtime_error("FIXME"); // throw std::runtime_error("FIXME");
} }
void void
@ -301,7 +301,7 @@ namespace llarp
{ {
// routers.emplace_back(val.begin(), val.end()); // routers.emplace_back(val.begin(), val.end());
(void)conf; (void)conf;
throw std::runtime_error("FIXME"); // throw std::runtime_error("FIXME");
} }
void void
@ -309,7 +309,7 @@ namespace llarp
{ {
// services.emplace_back(str(key), str(val)); // str()'s here for gcc 5 compat // services.emplace_back(str(key), str(val)); // str()'s here for gcc 5 compat
(void)conf; (void)conf;
throw std::runtime_error("FIXME"); // throw std::runtime_error("FIXME");
} }
void void
@ -382,7 +382,7 @@ namespace llarp
} }
*/ */
(void)conf; (void)conf;
throw std::runtime_error("FIXME"); // throw std::runtime_error("FIXME");
} }
void void
@ -447,72 +447,83 @@ namespace llarp
*/ */
} }
template < typename Section > bool
Section Config::Load(const char *fname)
find_section(const ConfigParser &, const std::string &)
{ {
throw std::runtime_error("FIXME"); // TODO: DRY
/* try
Section section;
auto visitor = [&](const ConfigParser::SectionValues_t& sectionValues) {
return section.parseSectionValues(parser, sectionValues);
};
// TODO: exceptions, please. fuck.
// parser.VisitSection just passes-through the return value of our
// lambda from above
if(parser.VisitSection(name.c_str(), visitor))
{ {
return section; Configuration conf;
} initializeConfig(conf);
return {}; ConfigParser parser;
*/ if(!parser.LoadFile(fname))
} {
return false;
}
bool parser.IterAll([&](string_view section, const SectionValues_t& values) {
Config::Load(const char* fname) for (const auto& pair : values)
{ {
ConfigParser parser; conf.addConfigValue(section, pair.first, pair.second);
if (!parser.LoadFile(fname)) }
});
// TODO: hand parsed data to conf, pull out values
return true;
}
catch(const std::exception& e)
{ {
LogError("Error trying to init and parse config from file: ", e.what());
return false; return false;
} }
return parse(parser);
} }
bool bool
Config::LoadFromStr(string_view str) Config::LoadFromStr(string_view str)
{ {
ConfigParser parser; // TODO: DRY
if (!parser.LoadFromStr(str)) try
{
Configuration conf;
initializeConfig(conf);
ConfigParser parser;
if(!parser.LoadFromStr(str))
{
return false;
}
// TODO: hand parsed data to conf, pull out values
return true;
}
catch(const std::exception& e)
{ {
LogError("Error trying to init and parse config from string: ", e.what());
return false; return false;
} }
return parse(parser);
} }
bool void
Config::parse(const ConfigParser& parser) Config::initializeConfig(Configuration& conf)
{ {
if (Lokinet_INIT()) // TODO: this seems like a random place to put this, should this be closer
return false; // to main() ?
router = find_section<RouterConfig>(parser, "router"); if(Lokinet_INIT())
network = find_section<NetworkConfig>(parser, "network"); throw std::runtime_error("Can't initializeConfig() when Lokinet_INIT() == true");
connect = find_section<ConnectConfig>(parser, "connect");
netdb = find_section<NetdbConfig>(parser, "netdb"); router.defineConfigOptions(conf);
dns = find_section<DnsConfig>(parser, "dns"); network.defineConfigOptions(conf);
links = find_section<LinksConfig>(parser, "bind"); connect.defineConfigOptions(conf);
services = find_section<ServicesConfig>(parser, "services"); netdb.defineConfigOptions(conf);
system = find_section<SystemConfig>(parser, "system"); dns.defineConfigOptions(conf);
api = find_section<ApiConfig>(parser, "api"); links.defineConfigOptions(conf);
lokid = find_section<LokidConfig>(parser, "lokid"); services.defineConfigOptions(conf);
bootstrap = find_section<BootstrapConfig>(parser, "bootstrap"); system.defineConfigOptions(conf);
logging = find_section<LoggingConfig>(parser, "logging"); api.defineConfigOptions(conf);
return true; lokid.defineConfigOptions(conf);
bootstrap.defineConfigOptions(conf);
logging.defineConfigOptions(conf);
} }
fs::path fs::path

@ -236,9 +236,9 @@ namespace llarp
struct Config struct Config
{ {
public: private:
bool void
parse(const ConfigParser& parser); initializeConfig(Configuration& conf);
public: public:
RouterConfig router; RouterConfig router;

Loading…
Cancel
Save