From: Waldemar Kozaczuk <jwkozac...@gmail.com> Committer: Nadav Har'El <n...@scylladb.com> Branch: master
cloud-init: replace boost program_options with light options utility functions Signed-off-by: Waldemar Kozaczuk <jwkozac...@gmail.com> Message-Id: <20200314011653.17792-1-jwkozac...@gmail.com> --- diff --git a/modules/cloud-init/cloud-init.cc b/modules/cloud-init/cloud-init.cc --- a/modules/cloud-init/cloud-init.cc +++ b/modules/cloud-init/cloud-init.cc @@ -171,7 +171,10 @@ void script_module::handle(const YAML::Node& doc) yaml_to_request(node, req); if (req.uri == "/open-rest-api") { should_wait = true; - t = std::thread([=] {boost::program_options::variables_map c; httpserver::global_server::run(c); }); + t = std::thread([=] { + std::map<std::string,std::vector<std::string>> c; + httpserver::global_server::run(c); + }); } else if (!req.uri.empty()) { do_api(req); } diff --git a/modules/cloud-init/main.cc b/modules/cloud-init/main.cc --- a/modules/cloud-init/main.cc +++ b/modules/cloud-init/main.cc @@ -13,16 +13,13 @@ #include "server-module.hh" #include "cassandra-module.hh" #include "monitor-agent-module.hh" -#include <boost/program_options/variables_map.hpp> -#include <boost/program_options/options_description.hpp> -#include <boost/program_options/parsers.hpp> #include <osv/debug.hh> #include <osv/exception_utils.hh> #include <osv/run.hh> +#include <osv/options.hh> using namespace std; using namespace init; -namespace po = boost::program_options; // config_disk() allows to use NoCloud VM configuration method - see // http://cloudinit.readthedocs.io/en/0.7.9/topics/datasources/nocloud.html. @@ -80,30 +77,40 @@ static bool config_disk(const char* outfile) { return false; } +static void usage() +{ + std::cout << "Allowed options:\n"; + std::cout << " --help produce help message\n"; + std::cout << " --skip-error do not stop on error\n"; + std::cout << " --force-probe force data source probing\n"; + std::cout << " --file args an init file\n"; + std::cout << " --server arg a server to read the file from. must come with a --url\n"; + std::cout << " --url arg a url at the server\n"; + std::cout << " --port arg (=80) a port at the server\n\n"; +} + +static void handle_parse_error(const std::string &message) +{ + std::cout << message << std::endl; + usage(); + exit(1); +} + int main(int argc, char* argv[]) { try { - po::options_description desc("Allowed options"); - desc.add_options() - ("help", "produce help message") - ("skip-error", "do not stop on error") - ("force-probe", "force data source probing") - ("file", po::value<std::string>(), "an init file") - ("server", po::value<std::string>(), "a server to read the file from. must come with a --url") - ("url", po::value<std::string>(), "a url at the server") - ("port", po::value<std::string>()->default_value("80"), "a port at the server") - ; - - po::variables_map config; - po::store(po::parse_command_line(argc, argv, desc), config); - po::notify(config); - - if (config.count("help")) { - std::cerr << desc << "\n"; + auto options_values = options::parse_options_values(argc - 1, argv + 1, handle_parse_error); + + if (options::extract_option_flag(options_values, "help", handle_parse_error)) { + usage(); return 1; } - osvinit init(config.count("skip-error") > 0, config.count("force-probe") > 0); + osvinit init( + options::extract_option_flag(options_values, "skip-error", handle_parse_error), + options::extract_option_flag(options_values, "force-probe", handle_parse_error) + ); + auto scripts = make_shared<script_module>(); init.add_module(scripts); init.add_module(make_shared<mount_module>()); @@ -114,18 +121,32 @@ int main(int argc, char* argv[]) init.add_module(make_shared<cassandra_module>()); init.add_module(make_shared<monitor_agent_module>()); - if (config.count("file")) { - init.load_file(config["file"].as<std::string>()); - } else if (config.count("server") > 0 && config.count("url") > 0) { - init.load_url(config["server"].as<std::string>(), - config["url"].as<std::string>(), - config["port"].as<std::string>()); + std::string port("80"); + if (options::option_value_exists(options_values, "port")) { + port = options::extract_option_value(options_values, "port"); + } + + if (options::option_value_exists(options_values, "file")) { + init.load_file(options::extract_option_value(options_values, "file")); + } else if (options::option_value_exists(options_values, "server") && + options::option_value_exists(options_values, "url")) { + init.load_url(options::extract_option_value(options_values, "server"), + options::extract_option_value(options_values, "url"), + port); } else if(config_disk("/tmp/config.yaml")) { init.load_file("/tmp/config.yaml"); } else { init.load_from_cloud(); } + if (!options_values.empty()) { + for (auto other_option : options_values) { + std::cout << "Unrecognized option: " << other_option.first << std::endl; + } + + usage(); + } + scripts->wait(); } catch (...) { std::cerr << "cloud-init failed: " << what(std::current_exception()) << "\n"; -- You received this message because you are subscribed to the Google Groups "OSv Development" group. To unsubscribe from this group and stop receiving emails from it, send an email to osv-dev+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/osv-dev/000000000000a2969105a0df9338%40google.com.