Mercurial > irccd
comparison irccdctl/plugin_config_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 | 27587ff92a64 |
comparison
equal
deleted
inserted
replaced
527:a88796ed040a | 528:9daccaeedcce |
---|---|
1 /* | |
2 * plugin_config_cli.cpp -- implementation of irccdctl plugin-config | |
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/json_util.hpp> | |
20 | |
21 #include "plugin_config_cli.hpp" | |
22 | |
23 namespace irccd { | |
24 | |
25 namespace ctl { | |
26 | |
27 void plugin_config_cli::set(ctl::controller& ctl, const std::vector<std::string>&args) | |
28 { | |
29 request(ctl, { | |
30 { "plugin", args[0] }, | |
31 { "variable", args[1] }, | |
32 { "value", args[2] } | |
33 }); | |
34 } | |
35 | |
36 void plugin_config_cli::get(ctl::controller& ctl, const std::vector<std::string>& args) | |
37 { | |
38 auto json = nlohmann::json::object({ | |
39 { "plugin", args[0] }, | |
40 { "variable", args[1] } | |
41 }); | |
42 | |
43 request(ctl, std::move(json), [args] (auto result) { | |
44 if (result["variables"].is_object()) | |
45 std::cout << json_util::pretty(result["variables"][args[1]]) << std::endl; | |
46 }); | |
47 } | |
48 | |
49 void plugin_config_cli::getall(ctl::controller& ctl, const std::vector<std::string> &args) | |
50 { | |
51 request(ctl, {{ "plugin", args[0] }}, [] (auto result) { | |
52 auto variables = result["variables"]; | |
53 | |
54 for (auto v = variables.begin(); v != variables.end(); ++v) | |
55 std::cout << std::setw(16) << std::left << v.key() << " : " << json_util::pretty(v.value()) << std::endl; | |
56 }); | |
57 } | |
58 | |
59 std::string plugin_config_cli::name() const | |
60 { | |
61 return "plugin-config"; | |
62 } | |
63 | |
64 void plugin_config_cli::exec(ctl::controller& ctl, const std::vector<std::string> &args) | |
65 { | |
66 switch (args.size()) { | |
67 case 3: | |
68 set(ctl, args); | |
69 break; | |
70 case 2: | |
71 get(ctl, args); | |
72 break; | |
73 case 1: | |
74 getall(ctl, args); | |
75 break; | |
76 default: | |
77 throw std::invalid_argument("plugin-config requires at least 1 argument"); | |
78 } | |
79 } | |
80 | |
81 } // !ctl | |
82 | |
83 } // !irccd |