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 <mv.hpp>
10 :
11 : #include <cmdline.hpp>
12 : #include <kdb.hpp>
13 : #include <keysetio.hpp>
14 : #include <rename.hpp>
15 :
16 : #include <helper/keyhelper.hpp>
17 :
18 : #include <iostream>
19 :
20 : using namespace std;
21 : using namespace kdb;
22 :
23 83 : MvCommand::MvCommand ()
24 : {
25 83 : }
26 :
27 5 : int MvCommand::execute (Cmdline const & cl)
28 : {
29 10 : if (cl.arguments.size () != 2)
30 : {
31 0 : throw invalid_argument ("wrong number of arguments, 2 needed");
32 : }
33 :
34 10 : KeySet conf;
35 10 : Key sourceKey = cl.createKey (0, false);
36 :
37 10 : Key destKey = cl.createKey (1, false);
38 10 : string newDirName = destKey.getName ();
39 :
40 25 : Key root = tools::helper::commonKeyName (sourceKey, destKey);
41 5 : if (cl.verbose) std::cout << "using common basename: " << root.getName () << std::endl;
42 5 : kdb.get (conf, root);
43 10 : KeySet tmpConf = conf;
44 10 : KeySet oldConf;
45 :
46 25 : oldConf.append (tmpConf.cut (sourceKey));
47 10 : std::string sourceName = sourceKey.getName ();
48 :
49 5 : if (!oldConf.size ())
50 : {
51 0 : std::cerr << "No key to copy found below '" << sourceName << "'" << std::endl;
52 : return 11;
53 : }
54 :
55 6 : KeySet newConf;
56 :
57 10 : Key k;
58 5 : oldConf.rewind ();
59 :
60 5 : if (cl.recursive)
61 : {
62 0 : while ((k = oldConf.next ()))
63 : {
64 0 : newConf.append (rename_key (k, sourceName, newDirName, cl.verbose));
65 : }
66 : }
67 : else
68 : {
69 : // just rename one key
70 15 : k = oldConf.next ();
71 5 : if (k != sourceKey)
72 : {
73 0 : cerr << "First key found " << k.getName () << " does not exactly match given key " << sourceKey.getName ()
74 0 : << ", aborting (use -r to move hierarchy)\n";
75 0 : return 11;
76 : }
77 15 : newConf.append (rename_key (k, sourceName, newDirName, cl.verbose));
78 : }
79 5 : newConf.append (tmpConf); // these are unrelated keys
80 : // drop the original configuration
81 :
82 5 : newConf.rewind ();
83 5 : if (cl.verbose)
84 : {
85 0 : cout << "Will write out:" << endl;
86 0 : cout << newConf;
87 : }
88 :
89 5 : kdb.set (newConf, root);
90 4 : printWarnings (cerr, root, cl.verbose, cl.debug);
91 :
92 : return 0;
93 : }
94 :
95 166 : MvCommand::~MvCommand ()
96 : {
97 7247 : }
|