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