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 <cache.hpp>
10 :
11 : #include <backends.hpp>
12 : #include <cmdline.hpp>
13 : #include <kdb.hpp>
14 : #include <modules.hpp>
15 :
16 : #include <iostream>
17 :
18 : using namespace std;
19 : using namespace kdb;
20 : using namespace kdb::tools;
21 :
22 78 : CacheCommand::CacheCommand ()
23 : {
24 78 : }
25 :
26 0 : int CacheCommand::execute (Cmdline const & cl)
27 : {
28 0 : if (cl.arguments.size () != 1) throw invalid_argument ("1 argument required");
29 :
30 0 : KeySet conf;
31 0 : Key parentKey ("system/elektra/cache", KEY_END);
32 :
33 0 : KDB kdb;
34 0 : kdb.get (conf, parentKey);
35 0 : printWarnings (cerr, parentKey, cl.verbose, cl.debug);
36 :
37 0 : string cmd = cl.arguments[0];
38 0 : Key isEnabled ("system/elektra/cache/enabled", KEY_END);
39 0 : if (cmd == "enable")
40 : {
41 : // always use the cache
42 0 : isEnabled.setString ("1");
43 0 : conf.append (isEnabled);
44 0 : kdb.set (conf, parentKey);
45 : }
46 0 : else if (cmd == "disable")
47 : {
48 : // never use the cache
49 0 : isEnabled.setString ("0");
50 0 : conf.append (isEnabled);
51 0 : kdb.set (conf, parentKey);
52 : }
53 0 : else if (cmd == "default")
54 : {
55 : // reset to default settings, use cache if available
56 0 : conf.lookup (isEnabled, KDB_O_POP);
57 0 : kdb.set (conf, parentKey);
58 : }
59 0 : else if (cmd == "clear")
60 : {
61 0 : Modules modules;
62 0 : PluginPtr plugin = modules.load ("cache", cl.getPluginsConfig ());
63 :
64 0 : KeySet ks;
65 0 : parentKey.setMeta ("cache/clear", "1");
66 0 : plugin->get (ks, parentKey);
67 : }
68 : else
69 : {
70 0 : throw invalid_argument ("not a valid subcommand");
71 : }
72 :
73 0 : printWarnings (cerr, parentKey, cl.verbose, cl.debug);
74 0 : printError (cerr, parentKey, cl.verbose, cl.debug);
75 0 : return 0;
76 : }
77 :
78 78 : CacheCommand::~CacheCommand ()
79 : {
80 7242 : }
|