comparison irccdctl/rule_edit_cli.cpp @ 528:9daccaeedcce

Irccdctl: split cli.hpp, closes #726
author David Demelier <markand@malikania.fr>
date Thu, 16 Nov 2017 23:12:45 +0100
parents
children 7cd7b2cdf923
comparison
equal deleted inserted replaced
527:a88796ed040a 528:9daccaeedcce
1 /*
2 * rule_edit_cli.cpp -- implementation of irccdctl rule-edit
3 *
4 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <irccd/options.hpp>
20 #include <irccd/string_util.hpp>
21
22 #include "rule_edit_cli.hpp"
23
24 namespace irccd {
25
26 namespace ctl {
27
28 std::string rule_edit_cli::name() const
29 {
30 return "rule-edit";
31 }
32
33 void rule_edit_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args)
34 {
35 static const option::options options{
36 { "-a", true },
37 { "--action", true },
38 { "-c", true },
39 { "--add-channel", true },
40 { "-C", true },
41 { "--remove-channel", true },
42 { "-e", true },
43 { "--add-event", true },
44 { "-E", true },
45 { "--remove-event", true },
46 { "-p", true },
47 { "--add-plugin", true },
48 { "-P", true },
49 { "--remove-plugin", true },
50 { "-s", true },
51 { "--add-server", true },
52 { "-S", true },
53 { "--remove-server", true },
54 };
55
56 auto copy = args;
57 auto result = option::read(copy, options);
58
59 if (copy.size() < 1)
60 throw std::invalid_argument("rule-edit requires at least 1 argument");
61
62 auto json = nlohmann::json::object({
63 { "command", "rule-edit" },
64 { "channels", nlohmann::json::array() },
65 { "events", nlohmann::json::array() },
66 { "plugins", nlohmann::json::array() },
67 { "servers", nlohmann::json::array() }
68 });
69
70 for (const auto& pair : result) {
71 // Action.
72 if (pair.first == "-a" || pair.first == "--action")
73 json["action"] = pair.second;
74
75 // Additions.
76 if (pair.first == "-c" || pair.first == "--add-channel")
77 json["add-channels"].push_back(pair.second);
78 if (pair.first == "-e" || pair.first == "--add-event")
79 json["add-events"].push_back(pair.second);
80 if (pair.first == "-p" || pair.first == "--add-plugin")
81 json["add-plugins"].push_back(pair.second);
82 if (pair.first == "-s" || pair.first == "--add-server")
83 json["add-servers"].push_back(pair.second);
84
85 // Removals.
86 if (pair.first == "-C" || pair.first == "--remove-channel")
87 json["remove-channels"].push_back(pair.second);
88 if (pair.first == "-E" || pair.first == "--remove-event")
89 json["remove-events"].push_back(pair.second);
90 if (pair.first == "-P" || pair.first == "--remove-plugin")
91 json["remove-plugins"].push_back(pair.second);
92 if (pair.first == "-S" || pair.first == "--remove-server")
93 json["remove-servers"].push_back(pair.second);
94 }
95
96 // Index.
97 json["index"] = string_util::to_number<unsigned>(copy[0]);
98
99 request(ctl, json);
100 }
101
102 } // !ctl
103
104 } // !irccd