3 #include "api_action.h"
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/service_conf.h"
11 #include "stg/common.h"
20 std::string Indent(size_t level, bool dash = false)
24 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
27 void PrintService(const STG::GetService::Info & info, size_t level = 0)
29 std::cout << Indent(level, true) << "name: " << info.name << "\n"
30 << Indent(level) << "cost: " << info.cost << "\n"
31 << Indent(level) << "payment day: " << static_cast<unsigned>(info.payDay) << "\n"
32 << Indent(level) << "comment: " << info.comment << "\n";
35 std::vector<SGCONF::API_ACTION::PARAM> GetServiceParams()
37 std::vector<SGCONF::API_ACTION::PARAM> params;
38 params.push_back(SGCONF::API_ACTION::PARAM("cost", "<cost>", "\tcost of the service"));
39 params.push_back(SGCONF::API_ACTION::PARAM("pay-day", "<month day>", "payment day"));
40 params.push_back(SGCONF::API_ACTION::PARAM("comment", "<text>", "comment"));
44 void SimpleCallback(bool result,
45 const std::string & reason,
50 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
53 std::cout << "Success.\n";
56 void GetServicesCallback(bool result,
57 const std::string & reason,
58 const std::vector<STG::GetService::Info> & info,
63 std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl;
66 std::cout << "Services:\n";
67 for (size_t i = 0; i < info.size(); ++i)
68 PrintService(info[i], 1);
71 void GetServiceCallback(bool result,
72 const std::string & reason,
73 const STG::GetService::Info & info,
78 std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl;
84 bool GetServicesFunction(const SGCONF::CONFIG & config,
85 const std::string & /*arg*/,
86 const std::map<std::string, std::string> & /*options*/)
88 STG::ServConf proto(config.server.data(),
90 config.localAddress.data(),
91 config.localPort.data(),
92 config.userName.data(),
93 config.userPass.data());
94 return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok;
97 bool GetServiceFunction(const SGCONF::CONFIG & config,
98 const std::string & arg,
99 const std::map<std::string, std::string> & /*options*/)
101 STG::ServConf proto(config.server.data(),
103 config.localAddress.data(),
104 config.localPort.data(),
105 config.userName.data(),
106 config.userPass.data());
107 return proto.GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
110 bool DelServiceFunction(const SGCONF::CONFIG & config,
111 const std::string & arg,
112 const std::map<std::string, std::string> & /*options*/)
114 STG::ServConf proto(config.server.data(),
116 config.localAddress.data(),
117 config.localPort.data(),
118 config.userName.data(),
119 config.userPass.data());
120 return proto.DelService(arg, SimpleCallback, NULL) == STG::st_ok;
123 bool AddServiceFunction(const SGCONF::CONFIG & config,
124 const std::string & arg,
125 const std::map<std::string, std::string> & options)
127 STG::ServiceConfOpt conf;
129 SGCONF::MaybeSet(options, "cost", conf.cost);
130 SGCONF::MaybeSet(options, "pay-day", conf.payDay);
131 SGCONF::MaybeSet(options, "comment", conf.comment);
132 STG::ServConf proto(config.server.data(),
134 config.localAddress.data(),
135 config.localPort.data(),
136 config.userName.data(),
137 config.userPass.data());
138 return proto.AddService(arg, conf, SimpleCallback, NULL) == STG::st_ok;
141 bool ChgServiceFunction(const SGCONF::CONFIG & config,
142 const std::string & arg,
143 const std::map<std::string, std::string> & options)
145 STG::ServiceConfOpt conf;
147 SGCONF::MaybeSet(options, "cost", conf.cost);
148 SGCONF::MaybeSet(options, "pay-day", conf.payDay);
149 SGCONF::MaybeSet(options, "comment", conf.comment);
150 STG::ServConf proto(config.server.data(),
152 config.localAddress.data(),
153 config.localPort.data(),
154 config.userName.data(),
155 config.userPass.data());
156 return proto.ChgService(conf, SimpleCallback, NULL) == STG::st_ok;
159 } // namespace anonymous
161 void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
163 std::vector<API_ACTION::PARAM> params(GetServiceParams());
164 blocks.Add("Service management options")
165 .Add("get-services", SGCONF::MakeAPIAction(commands, GetServicesFunction), "\tget service list")
166 .Add("get-service", SGCONF::MakeAPIAction(commands, "<name>", GetServiceFunction), "get service")
167 .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", params, AddServiceFunction), "add service")
168 .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", DelServiceFunction), "delete service")
169 .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", params, ChgServiceFunction), "change service");