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 <info.hpp>
10 :
11 : #include <cmdline.hpp>
12 : #include <kdb.hpp>
13 : #include <modules.hpp>
14 : #include <plugin.hpp>
15 :
16 : #include <iostream>
17 :
18 : using namespace std;
19 : using namespace kdb;
20 : using namespace kdb::tools;
21 :
22 :
23 507 : InfoCommand::InfoCommand ()
24 : {
25 507 : }
26 :
27 429 : int InfoCommand::execute (Cmdline const & cl)
28 : {
29 858 : std::string subkey;
30 858 : if (cl.arguments.size () < 1 || cl.arguments.size () > 2)
31 : {
32 0 : throw invalid_argument ("Need 1 or 2 argument(s)");
33 : }
34 :
35 429 : if (cl.arguments.size () == 2)
36 : {
37 858 : subkey = cl.arguments[1];
38 : }
39 1287 : std::string name = cl.arguments[0];
40 :
41 858 : KeySet conf;
42 2574 : Key parentKey (std::string ("system/elektra/modules/") + name, KEY_END);
43 :
44 429 : if (!cl.load)
45 : {
46 858 : KDB kdb;
47 429 : kdb.get (conf, parentKey);
48 : }
49 :
50 1287 : if (!conf.lookup (parentKey))
51 : {
52 413 : if (!cl.load)
53 : {
54 826 : cerr << "Module does not seem to be loaded." << endl;
55 413 : cerr << "Now in fallback code. Will directly load config from plugin." << endl;
56 : }
57 :
58 826 : Modules modules;
59 2065 : KeySet ks = cl.getPluginsConfig ();
60 826 : PluginPtr plugin;
61 413 : if (ks.size () == 0)
62 : {
63 413 : plugin = modules.load (name);
64 : }
65 : else
66 : {
67 0 : plugin = modules.load (name, ks);
68 : }
69 :
70 : // fix name for virtual plugins
71 1239 : if (name != plugin->name ())
72 : {
73 7 : std::cerr << "Will use name " << plugin->name () << " for virtual plugin named " << name << std::endl;
74 2 : name = plugin->name ();
75 : }
76 1652 : conf.append (plugin->getInfo ());
77 : }
78 :
79 3003 : Key root (std::string ("system/elektra/modules/") + name + "/exports", KEY_END);
80 :
81 429 : if (!subkey.empty ())
82 : {
83 3003 : root.setName (std::string ("system/elektra/modules/") + name + "/infos/" + subkey);
84 858 : Key k = conf.lookup (root);
85 429 : if (k)
86 : {
87 1716 : cout << k.getString () << std::endl;
88 429 : return 0;
89 : }
90 : else
91 : {
92 0 : cerr << "clause not found" << std::endl;
93 : return 1;
94 : }
95 : }
96 :
97 0 : root.setName (std::string ("system/elektra/modules/") + name + "/exports");
98 0 : Key k = conf.lookup (root);
99 :
100 0 : if (k)
101 : {
102 0 : cout << "Exported symbols: ";
103 0 : while ((k = conf.next ()) && k.isBelow (root))
104 : {
105 0 : cout << k.getBaseName () << " ";
106 : }
107 : cout << endl;
108 : }
109 : else
110 0 : cout << "no exported symbols found" << endl;
111 :
112 0 : root.setName (std::string ("system/elektra/modules/") + name + "/infos");
113 0 : k = conf.lookup (root);
114 :
115 0 : if (k)
116 : {
117 0 : while ((k = conf.next ()) && k.isBelow (root))
118 : {
119 0 : cout << getStdColor (ANSI_COLOR::BOLD) << k.getBaseName () << ": " << getStdColor (ANSI_COLOR::RESET)
120 0 : << k.getString () << endl;
121 : }
122 : }
123 : else
124 0 : cout << "no information found" << endl;
125 :
126 0 : return 0;
127 : }
128 :
129 507 : InfoCommand::~InfoCommand ()
130 : {
131 7671 : }
|