comparison irccdctl/cli.cpp @ 488:7e273b7f4f92

Irccd: new coding style, closes #576
author David Demelier <markand@malikania.fr>
date Tue, 26 Sep 2017 17:18:47 +0200
parents acb2d4990249
children 349fe29d86d5
comparison
equal deleted inserted replaced
487:beb6c638b841 488:7e273b7f4f92
38 * ------------------------------------------------------------------ 38 * ------------------------------------------------------------------
39 */ 39 */
40 40
41 void Cli::check(const nlohmann::json &response) 41 void Cli::check(const nlohmann::json &response)
42 { 42 {
43 if (!util::json::getBool(response, "status", false)) { 43 if (!util::json::get_bool(response, "status", false)) {
44 auto error = util::json::getString(response, "error"); 44 auto error = util::json::get_string(response, "error");
45 45
46 if (error.empty()) 46 if (error.empty())
47 throw std::runtime_error("command failed with an unknown error"); 47 throw std::runtime_error("command failed with an unknown error");
48 48
49 throw std::runtime_error(error); 49 throw std::runtime_error(error);
76 76
77 irccdctl.client().onMessage.disconnect(id); 77 irccdctl.client().onMessage.disconnect(id);
78 78
79 if (!msg.is_object()) 79 if (!msg.is_object())
80 throw std::runtime_error("no response received"); 80 throw std::runtime_error("no response received");
81 if (util::json::getString(msg, "command") != m_name) 81 if (util::json::get_string(msg, "command") != m_name)
82 throw std::runtime_error("unexpected command result received"); 82 throw std::runtime_error("unexpected command result received");
83 83
84 check(msg); 84 check(msg);
85 85
86 return msg; 86 return msg;
185 throw std::invalid_argument("plugin-info requires 1 argument"); 185 throw std::invalid_argument("plugin-info requires 1 argument");
186 186
187 auto result = request(irccdctl, {{ "plugin", args[0] }}); 187 auto result = request(irccdctl, {{ "plugin", args[0] }});
188 188
189 std::cout << std::boolalpha; 189 std::cout << std::boolalpha;
190 std::cout << "Author : " << util::json::getString(result, "author") << std::endl; 190 std::cout << "Author : " << util::json::get_string(result, "author") << std::endl;
191 std::cout << "License : " << util::json::getString(result, "license") << std::endl; 191 std::cout << "License : " << util::json::get_string(result, "license") << std::endl;
192 std::cout << "Summary : " << util::json::getString(result, "summary") << std::endl; 192 std::cout << "Summary : " << util::json::get_string(result, "summary") << std::endl;
193 std::cout << "Version : " << util::json::getString(result, "version") << std::endl; 193 std::cout << "Version : " << util::json::get_string(result, "version") << std::endl;
194 } 194 }
195 195
196 /* 196 /*
197 * PluginListCli. 197 * PluginListCli.
198 * ------------------------------------------------------------------ 198 * ------------------------------------------------------------------
405 { "name", copy[0] }, 405 { "name", copy[0] },
406 { "host", copy[1] } 406 { "host", copy[1] }
407 }); 407 });
408 408
409 if (copy.size() == 3) { 409 if (copy.size() == 3) {
410 if (!util::isNumber(copy[2])) 410 if (!util::is_int(copy[2]))
411 throw std::invalid_argument("invalid port number"); 411 throw std::invalid_argument("invalid port number");
412 412
413 object["port"] = std::stoi(copy[2]); 413 object["port"] = std::stoi(copy[2]);
414 } 414 }
415 415
907 json["servers"].push_back(pair.second); 907 json["servers"].push_back(pair.second);
908 } 908 }
909 909
910 // Index. 910 // Index.
911 if (result.count("-i") > 0) 911 if (result.count("-i") > 0)
912 json["index"] = util::toNumber<unsigned>(result.find("-i")->second); 912 json["index"] = util::to_number<unsigned>(result.find("-i")->second);
913 if (result.count("--index") > 0) 913 if (result.count("--index") > 0)
914 json["index"] = util::toNumber<unsigned>(result.find("--index")->second); 914 json["index"] = util::to_number<unsigned>(result.find("--index")->second);
915 915
916 // And action. 916 // And action.
917 if (copy[0] != "accept" && copy[0] != "drop") 917 if (copy[0] != "accept" && copy[0] != "drop")
918 throw std::runtime_error("invalid action '"s + copy[0] + "'"); 918 throw std::runtime_error("invalid action '"s + copy[0] + "'");
919 919
1011 if (pair.first == "-S" || pair.first == "--remove-server") 1011 if (pair.first == "-S" || pair.first == "--remove-server")
1012 json["remove-servers"].push_back(pair.second); 1012 json["remove-servers"].push_back(pair.second);
1013 } 1013 }
1014 1014
1015 // Index. 1015 // Index.
1016 json["index"] = util::toNumber<unsigned>(copy[0]); 1016 json["index"] = util::to_number<unsigned>(copy[0]);
1017 1017
1018 check(request(irccdctl, json)); 1018 check(request(irccdctl, json));
1019 } 1019 }
1020 1020
1021 /* 1021 /*