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