From b7714a2ef0ee92facf31cd1cf17a113b4dcb57b2 Mon Sep 17 00:00:00 2001 From: cryptocode Date: Thu, 5 Sep 2019 09:10:39 +0200 Subject: [PATCH] Add --config option to rpc process (#2287) * Add --config option to rpc process * Single block so error isn't repeated --- nano/lib/rpcconfig.cpp | 22 ++++++++++++++++++---- nano/lib/rpcconfig.hpp | 3 ++- nano/nano_rpc/entry.cpp | 13 ++++++++++--- nano/node/daemonconfig.cpp | 21 ++++++++++++--------- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/nano/lib/rpcconfig.cpp b/nano/lib/rpcconfig.cpp index 90c73a89..832525ab 100644 --- a/nano/lib/rpcconfig.cpp +++ b/nano/lib/rpcconfig.cpp @@ -187,7 +187,7 @@ nano::error nano::rpc_config::deserialize_toml (nano::tomlconfig & toml) namespace nano { -nano::error read_rpc_config_toml (boost::filesystem::path const & data_path_a, nano::rpc_config & config_a) +nano::error read_rpc_config_toml (boost::filesystem::path const & data_path_a, nano::rpc_config & config_a, std::vector const & config_overrides) { nano::error error; auto json_config_path = nano::get_rpc_config_path (data_path_a); @@ -233,10 +233,24 @@ nano::error read_rpc_config_toml (boost::filesystem::path const & data_path_a, n // Parse and deserialize nano::tomlconfig toml; - // Make sure we don't create an empty toml file if it doesn't exist. Running without a toml file is the default. - if (!error && boost::filesystem::exists (toml_config_path)) + std::stringstream config_overrides_stream; + for (auto const & entry : config_overrides) { - error = toml.read (toml_config_path); + config_overrides_stream << entry << std::endl; + } + config_overrides_stream << std::endl; + + // Make sure we don't create an empty toml file if it doesn't exist. Running without a toml file is the default. + if (!error) + { + if (boost::filesystem::exists (toml_config_path)) + { + error = toml.read (config_overrides_stream, toml_config_path); + } + else + { + toml.read (config_overrides_stream); + } } if (!error) diff --git a/nano/lib/rpcconfig.hpp b/nano/lib/rpcconfig.hpp index d371c740..af0bdf81 100644 --- a/nano/lib/rpcconfig.hpp +++ b/nano/lib/rpcconfig.hpp @@ -8,6 +8,7 @@ #include #include +#include namespace nano { @@ -75,7 +76,7 @@ public: } }; -nano::error read_rpc_config_toml (boost::filesystem::path const & data_path_a, nano::rpc_config & config_a); +nano::error read_rpc_config_toml (boost::filesystem::path const & data_path_a, nano::rpc_config & config_a, std::vector const & config_overrides = std::vector ()); nano::error read_and_update_rpc_config (boost::filesystem::path const & data_path, nano::rpc_config & config_a); std::string get_default_rpc_filepath (); diff --git a/nano/nano_rpc/entry.cpp b/nano/nano_rpc/entry.cpp index 7ac76067..3613da00 100644 --- a/nano/nano_rpc/entry.cpp +++ b/nano/nano_rpc/entry.cpp @@ -35,7 +35,7 @@ void logging_init (boost::filesystem::path const & application_path_a) volatile sig_atomic_t sig_int_or_term = 0; -void run (boost::filesystem::path const & data_path) +void run (boost::filesystem::path const & data_path, std::vector const & config_overrides) { boost::filesystem::create_directories (data_path); boost::system::error_code error_chmod; @@ -43,7 +43,7 @@ void run (boost::filesystem::path const & data_path) std::unique_ptr runner; nano::rpc_config rpc_config; - auto error = nano::read_rpc_config_toml (data_path, rpc_config); + auto error = nano::read_rpc_config_toml (data_path, rpc_config, config_overrides); if (!error) { logging_init (data_path); @@ -92,6 +92,7 @@ int main (int argc, char * const * argv) // clang-format off description.add_options () ("help", "Print out options") + ("config", boost::program_options::value>()->multitoken(), "Pass RPC configuration values. This takes precedence over any values in the configuration file. This option can be repeated multiple times.") ("daemon", "Start RPC daemon") ("data_path", boost::program_options::value (), "Use the supplied path as the data directory") ("network", boost::program_options::value (), "Use the supplied network (live, beta or test)") @@ -136,7 +137,13 @@ int main (int argc, char * const * argv) boost::filesystem::path data_path ((data_path_it != vm.end ()) ? data_path_it->second.as () : nano::working_path ()); if (vm.count ("daemon") > 0) { - run (data_path); + std::vector config_overrides; + auto config (vm.find ("config")); + if (config != vm.end ()) + { + config_overrides = config->second.as> (); + } + run (data_path, config_overrides); } else if (vm.count ("version")) { diff --git a/nano/node/daemonconfig.cpp b/nano/node/daemonconfig.cpp index 97bf43ec..346efb58 100644 --- a/nano/node/daemonconfig.cpp +++ b/nano/node/daemonconfig.cpp @@ -202,21 +202,24 @@ nano::error read_node_config_toml (boost::filesystem::path const & data_path_a, // Parse and deserialize nano::tomlconfig toml; - std::stringstream config_stream; + std::stringstream config_overrides_stream; for (auto const & entry : config_overrides) { - config_stream << entry << std::endl; + config_overrides_stream << entry << std::endl; } - config_stream << std::endl; + config_overrides_stream << std::endl; // Make sure we don't create an empty toml file if it doesn't exist. Running without a toml file is the default. - if (!error && boost::filesystem::exists (toml_config_path)) + if (!error) { - toml.read (config_stream, toml_config_path); - } - else if (!error) - { - toml.read (config_stream); + if (boost::filesystem::exists (toml_config_path)) + { + error = toml.read (config_overrides_stream, toml_config_path); + } + else + { + toml.read (config_overrides_stream); + } } if (!error)