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 <list.hpp>
10 :
11 : #include <algorithm>
12 : #include <iostream>
13 :
14 : #include <cmdline.hpp>
15 : #include <plugindatabase.hpp>
16 :
17 : using namespace kdb;
18 : using namespace std;
19 :
20 78 : ListCommand::ListCommand ()
21 : {
22 78 : }
23 :
24 0 : int ListCommand::execute (Cmdline const & cl)
25 : {
26 : using namespace kdb::tools;
27 :
28 0 : if (cl.arguments.size () > 1)
29 : {
30 0 : throw invalid_argument ("too many arguments, use 1 argument (provider) or no arguments (all plugins)");
31 : ;
32 : }
33 :
34 0 : ModulesPluginDatabase db;
35 0 : std::vector<std::string> plugins;
36 0 : if (cl.arguments.size () == 1)
37 : {
38 0 : std::vector<PluginSpec> pluginspecs = db.lookupAllProvides (cl.arguments[0]);
39 0 : plugins.resize (pluginspecs.size ());
40 : std::transform (pluginspecs.begin (), pluginspecs.end (), plugins.begin (),
41 0 : [](PluginSpec const & ps) { return ps.getName (); });
42 : }
43 : else
44 : {
45 0 : plugins = db.listAllPlugins ();
46 : }
47 :
48 0 : std::multimap<int, std::string> sortedPlugins;
49 0 : for (const auto & plugin : plugins)
50 : {
51 : try
52 : {
53 0 : int s = db.calculateStatus (db.lookupInfo (
54 0 : PluginSpec (plugin,
55 0 : KeySet (5,
56 0 : *Key ("system/module", KEY_VALUE, "this plugin was loaded without a config", KEY_END),
57 : KS_END)),
58 0 : "status"));
59 0 : sortedPlugins.insert (std::make_pair (s, plugin));
60 : }
61 0 : catch (std::exception const & e)
62 : {
63 0 : sortedPlugins.insert (std::make_pair (-1000000, plugin));
64 0 : if (cl.verbose)
65 : {
66 0 : std::cerr << "No status found for " << plugin << std::endl;
67 : }
68 : }
69 : }
70 :
71 0 : if (cl.verbose) cout << "number of all plugins: " << plugins.size () << endl;
72 :
73 0 : for (auto & plugin : sortedPlugins)
74 : {
75 0 : std::cout << plugin.second;
76 0 : if (cl.verbose)
77 : {
78 0 : std::cout << " " << plugin.first;
79 : }
80 :
81 0 : if (cl.null)
82 : {
83 : cout << '\0';
84 : }
85 : else
86 : {
87 : cout << endl;
88 : }
89 : }
90 :
91 0 : return 0;
92 : }
93 :
94 78 : ListCommand::~ListCommand ()
95 : {
96 7242 : }
|