Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <export.hpp>
10 :
11 : #include <cmdline.hpp>
12 : #include <kdb.hpp>
13 : #include <modules.hpp>
14 : #include <toolexcept.hpp>
15 :
16 : #include <iostream>
17 :
18 : using namespace std;
19 : using namespace kdb;
20 : using namespace kdb::tools;
21 :
22 1716 : ExportCommand::ExportCommand ()
23 : {
24 858 : }
25 :
26 779 : int ExportCommand::execute (Cmdline const & cl)
27 : {
28 1558 : size_t argc = cl.arguments.size ();
29 779 : if (argc != 1 && argc != 2 && argc != 3)
30 : {
31 0 : throw invalid_argument ("need 1 to 3 arguments");
32 : }
33 :
34 1558 : Key root = cl.createKey (0);
35 :
36 779 : kdb.get (ks, root);
37 778 : printWarnings (cerr, root, cl.verbose, cl.debug);
38 :
39 3112 : KeySet part (ks.cut (root));
40 :
41 778 : if (cl.withoutElektra)
42 : {
43 272 : Key systemElektra ("system/elektra", KEY_END);
44 544 : part.cut (systemElektra);
45 : }
46 :
47 2334 : string format = cl.format;
48 1553 : if (argc > 1) format = cl.arguments[1];
49 :
50 : #ifdef _WIN32
51 : string file = "CON";
52 : #else
53 3112 : string file = "/dev/stdout";
54 : #endif
55 778 : if (argc > 2 && cl.arguments[2] != "-") file = cl.arguments[2];
56 :
57 1556 : Modules modules;
58 4668 : PluginPtr plugin = modules.load (format, cl.getPluginsConfig ());
59 :
60 1556 : Key errorKey (root);
61 2334 : errorKey.setString (file);
62 :
63 778 : plugin->set (part, errorKey);
64 :
65 778 : printWarnings (cerr, errorKey, cl.verbose, cl.debug);
66 778 : printError (cerr, errorKey, cl.verbose, cl.debug);
67 :
68 1556 : return 0;
69 : }
70 :
71 3432 : ExportCommand::~ExportCommand ()
72 : {
73 8880 : }
|