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 <check.hpp>
10 :
11 : #include <cmdline.hpp>
12 : #include <kdb.hpp>
13 : #include <modules.hpp>
14 : #include <plugin.hpp>
15 :
16 : #include <iostream>
17 :
18 :
19 : using namespace std;
20 : using namespace kdb;
21 : using namespace kdb::tools;
22 :
23 180 : CheckCommand::CheckCommand ()
24 : {
25 180 : }
26 :
27 0 : int printProblems (Key const & k, std::string const & action, int off)
28 : {
29 0 : bool wo = k.getMeta<const kdb::Key> ("warnings");
30 0 : bool eo = k.getMeta<const kdb::Key> ("error");
31 0 : if (wo || eo) std::cerr << "\n======\n" << action << " of kdb yield following problems:" << std::endl;
32 0 : printWarnings (cerr, k, true, true);
33 0 : printError (cerr, k, true, true);
34 0 : return (wo + eo * 2) << off;
35 : }
36 :
37 0 : int doKDBcheck (bool force)
38 : {
39 0 : Key x;
40 : try
41 : {
42 0 : int ret = 0;
43 0 : KDB kdb (x);
44 0 : ret += printProblems (x, "opening", 0);
45 :
46 0 : KeySet ks;
47 0 : Key a ("/", KEY_END);
48 : try
49 : {
50 0 : kdb.get (ks, a);
51 : }
52 0 : catch (...)
53 : {
54 : }
55 0 : ret += printProblems (a, "getting", 2);
56 :
57 0 : if (force)
58 : {
59 0 : Key b ("/", KEY_END);
60 : try
61 : {
62 0 : kdb.set (ks, b);
63 : }
64 0 : catch (...)
65 : {
66 : }
67 0 : ret += printProblems (b, "setting", 4);
68 : }
69 :
70 0 : Key y;
71 0 : kdb.close (y);
72 0 : ret += printProblems (y, "closing", 6);
73 0 : return ret;
74 : }
75 0 : catch (KDBException const & e)
76 : {
77 0 : std::cerr << "a fatal problem occurred, could not open KDB. This should not happen" << std::endl;
78 0 : return printProblems (x, "opening", 0);
79 : }
80 : }
81 :
82 102 : int CheckCommand::execute (Cmdline const & cl)
83 : {
84 204 : if (cl.arguments.size () == 0)
85 : {
86 0 : return doKDBcheck (cl.force);
87 : }
88 :
89 204 : std::string name = cl.arguments[0];
90 :
91 204 : Modules modules;
92 102 : if (cl.verbose) cout << "will try check the plugin " << name << endl;
93 :
94 204 : vector<string> warnings;
95 : try
96 : {
97 510 : KeySet ks = cl.getPluginsConfig ();
98 306 : ks.append (Key ("system/module", KEY_END));
99 204 : PluginPtr plugin = modules.load (name, ks);
100 102 : plugin->check (warnings);
101 : }
102 0 : catch (NoPlugin const & p)
103 : {
104 0 : cerr << p.what () << endl;
105 : return 1;
106 : }
107 0 : catch (PluginCheckException const & p)
108 : {
109 0 : cerr << "Plugin did not pass all checks:" << endl;
110 0 : cerr << "See description below:" << endl;
111 0 : cerr << p.what () << endl;
112 : return 2;
113 : }
114 :
115 204 : if (warnings.size () > 0)
116 : {
117 0 : cerr << "There are " << warnings.size () << " warnings for this plugin" << endl;
118 0 : cerr << "For high quality plugins there should be no warning" << endl;
119 :
120 0 : for (size_t i = 0; i < warnings.size (); ++i)
121 : {
122 0 : cerr << "Warning #" << i << ": " << warnings[i] << endl;
123 : }
124 : return 3;
125 : }
126 :
127 : return 0;
128 : }
129 :
130 180 : CheckCommand::~CheckCommand ()
131 : {
132 7344 : }
|