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 <rm.hpp>
10 :
11 : #include <cmdline.hpp>
12 : #include <kdb.hpp>
13 :
14 : #include <iostream>
15 :
16 : using namespace std;
17 : using namespace kdb;
18 :
19 401 : RemoveCommand::RemoveCommand ()
20 : {
21 401 : }
22 :
23 120 : static int noKeyFound (bool verbose, bool force, std::string article)
24 : {
25 120 : if (verbose || !force)
26 : {
27 345 : cerr << "Did not find " << article << " key" << endl;
28 : }
29 120 : return force ? 0 : 11;
30 : }
31 :
32 323 : int RemoveCommand::execute (Cmdline const & cl)
33 : {
34 646 : if (cl.arguments.size () != 1) throw invalid_argument ("1 argument required");
35 :
36 646 : KeySet conf;
37 639 : Key x = cl.createKey (0);
38 :
39 316 : kdb.get (conf, x);
40 :
41 632 : KeySet savedKeys;
42 :
43 316 : if (cl.withoutElektra)
44 : {
45 0 : Key systemElektra ("system/elektra", KEY_END);
46 0 : savedKeys = conf.cut (systemElektra);
47 : }
48 :
49 316 : if (!cl.recursive)
50 : {
51 147 : Key f = conf.lookup (x, KDB_O_POP);
52 :
53 74 : if (!f)
54 : {
55 4 : return noKeyFound (cl.verbose, cl.force, "the");
56 : }
57 : }
58 : else
59 : {
60 : // do recursive removing
61 849 : KeySet ks = conf.cut (x);
62 :
63 242 : if (ks.size () == 0)
64 : {
65 476 : return noKeyFound (cl.verbose, cl.force, "any");
66 : }
67 : }
68 :
69 196 : conf.append (savedKeys);
70 :
71 196 : kdb.set (conf, x);
72 :
73 : return 0;
74 : }
75 :
76 802 : RemoveCommand::~RemoveCommand ()
77 : {
78 7565 : }
|