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 <validation.hpp>
10 :
11 : #include <cmdline.hpp>
12 : #include <kdb.hpp>
13 :
14 : #include <iostream>
15 : #include <string>
16 :
17 : using namespace std;
18 : using namespace kdb;
19 :
20 79 : ValidationCommand::ValidationCommand ()
21 : {
22 79 : }
23 :
24 1 : int ValidationCommand::execute (Cmdline const & cl)
25 : {
26 2 : size_t argc = cl.arguments.size ();
27 1 : if (argc != 3 && argc != 4)
28 : {
29 0 : throw invalid_argument ("need 3 or 4 arguments");
30 : }
31 :
32 2 : KeySet conf;
33 2 : Key parentKey = cl.createKey (0);
34 2 : string keyname = parentKey.getName ();
35 1 : kdb.get (conf, parentKey);
36 2 : Key k = conf.lookup (keyname);
37 :
38 1 : if (!k)
39 : {
40 4 : k = Key (keyname, KEY_END);
41 : conf.append (k);
42 : }
43 :
44 1 : if (!k.isValid ())
45 : {
46 0 : throw invalid_argument ("keyname not valid");
47 : }
48 :
49 3 : string value = cl.arguments[1];
50 3 : string validationregex = cl.arguments[2];
51 2 : string validationmessage;
52 1 : if (argc == 4)
53 2 : validationmessage = cl.arguments[3];
54 : else
55 0 : validationmessage = "Regular expression " + validationregex + " does not match the supplied value";
56 :
57 3 : k.setString (value);
58 5 : k.setMeta<string> ("check/validation", validationregex);
59 7 : k.setMeta<string> ("check/validation/match", "LINE");
60 5 : k.setMeta<string> ("check/validation/message", validationmessage);
61 :
62 1 : kdb.set (conf, parentKey);
63 :
64 2 : return 0;
65 : }
66 :
67 158 : ValidationCommand::~ValidationCommand ()
68 : {
69 7243 : }
|