Line data Source code
1 : #include <stdlib.h>
2 :
3 : #include <iostream>
4 :
5 : #include <boost/program_options.hpp>
6 :
7 : #include <boost/property_tree/info_parser.hpp>
8 : #include <boost/property_tree/json_parser.hpp>
9 : #include <boost/property_tree/xml_parser.hpp>
10 :
11 : #include "conffwk/Configuration.hpp"
12 :
13 : using namespace dunedaq::conffwk;
14 :
15 : int
16 0 : main(int argc, char *argv[])
17 : {
18 0 : std::string output_file, db_name, classes, format("json");
19 0 : bool direct_only(false);
20 :
21 0 : boost::program_options::options_description desc("Export conffwk schema using boost property tree.\n\nOptions/Arguments");
22 :
23 0 : try
24 : {
25 0 : desc.add_options()
26 0 : (
27 : "database,d",
28 0 : boost::program_options::value<std::string>(&db_name)->required(),
29 : "database specification in format plugin-name:parameters"
30 : )
31 0 : (
32 : "classes,c",
33 0 : boost::program_options::value<std::string>(&classes),
34 : "regex defining class names; ignore if empty"
35 : )
36 0 : (
37 : "direct-only,r",
38 : "print direct properties"
39 : )
40 0 : (
41 : "output,o",
42 0 : boost::program_options::value<std::string>(&output_file),
43 : "output file name; print to standard out, if not defined"
44 : )
45 0 : (
46 : "format,t",
47 0 : boost::program_options::value<std::string>(&format)->default_value(format),
48 : "output format (\"json\", \"xml\" or \"info\")"
49 : )
50 0 : (
51 : "help,h",
52 : "Print help message"
53 : );
54 :
55 0 : boost::program_options::variables_map vm;
56 0 : boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
57 :
58 0 : if (vm.count("help"))
59 : {
60 0 : std::cout << desc << std::endl;
61 0 : return EXIT_SUCCESS;
62 : }
63 :
64 0 : if (vm.count("direct-only"))
65 0 : direct_only = true;
66 :
67 0 : boost::program_options::notify(vm);
68 :
69 0 : auto valid_formats = {"json", "xml", "info"};
70 0 : if (std::none_of(valid_formats.begin(), valid_formats.end(), [&format](auto p){ return p == format; }))
71 0 : throw std::runtime_error("unsupported format \"" + format + '\"');
72 0 : }
73 0 : catch (std::exception& ex)
74 : {
75 0 : std::cerr << "command line error: " << ex.what() << std::endl;
76 0 : return EXIT_FAILURE;
77 0 : }
78 :
79 0 : try
80 : {
81 0 : Configuration db(db_name);
82 :
83 0 : boost::property_tree::ptree pt;
84 :
85 0 : db.export_schema(pt, classes, direct_only);
86 :
87 0 : std::ostringstream buf;
88 0 : if (format == "json")
89 0 : boost::property_tree::json_parser::write_json(buf, pt);
90 0 : else if (format == "xml")
91 0 : boost::property_tree::xml_parser::write_xml(buf, pt, boost::property_tree::xml_writer_make_settings<std::string>(' ', 4));
92 : else
93 0 : boost::property_tree::info_parser::write_info(buf, pt, boost::property_tree::info_writer_make_settings(' ', 4));
94 :
95 0 : if (!output_file.empty())
96 : {
97 0 : std::ofstream f(output_file);
98 0 : f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
99 0 : f << buf.str();
100 0 : f.close();
101 0 : }
102 : else
103 0 : std::cout << buf.str();
104 :
105 0 : return EXIT_SUCCESS;
106 0 : }
107 0 : catch (const dunedaq::conffwk::Exception &ex)
108 : {
109 0 : std::cout << "conffwk error: " << ex << std::endl;
110 0 : }
111 0 : catch (const boost::property_tree::json_parser_error &ex)
112 : {
113 0 : std::cout << "ptree json error: " << ex.what() << std::endl;
114 0 : }
115 0 : catch (const std::exception &ex)
116 : {
117 0 : std::cout << "error: " << ex.what() << std::endl;
118 0 : }
119 :
120 : return EXIT_FAILURE;
121 0 : }
|