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::GET_SERVICE::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: " << 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::GET_SERVICE::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::GET_SERVICE::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.userName.data(),
91 config.userPass.data());
92 return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok;
95 bool GetServiceFunction(const SGCONF::CONFIG & config,
96 const std::string & arg,
97 const std::map<std::string, std::string> & /*options*/)
99 STG::SERVCONF proto(config.server.data(),
101 config.userName.data(),
102 config.userPass.data());
103 return proto.GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
106 bool DelServiceFunction(const SGCONF::CONFIG & config,
107 const std::string & arg,
108 const std::map<std::string, std::string> & /*options*/)
110 STG::SERVCONF proto(config.server.data(),
112 config.userName.data(),
113 config.userPass.data());
114 return proto.DelService(arg, SimpleCallback, NULL) == STG::st_ok;
117 bool AddServiceFunction(const SGCONF::CONFIG & config,
118 const std::string & arg,
119 const std::map<std::string, std::string> & options)
121 SERVICE_CONF_RES conf;
123 SGCONF::MaybeSet(options, "cost", conf.cost);
124 SGCONF::MaybeSet(options, "pay-day", conf.payDay);
125 SGCONF::MaybeSet(options, "comment", conf.comment);
126 STG::SERVCONF proto(config.server.data(),
128 config.userName.data(),
129 config.userPass.data());
130 return proto.AddService(arg, conf, SimpleCallback, NULL) == STG::st_ok;
133 bool ChgServiceFunction(const SGCONF::CONFIG & config,
134 const std::string & arg,
135 const std::map<std::string, std::string> & options)
137 SERVICE_CONF_RES conf;
139 SGCONF::MaybeSet(options, "cost", conf.cost);
140 SGCONF::MaybeSet(options, "pay-day", conf.payDay);
141 SGCONF::MaybeSet(options, "comment", conf.comment);
142 STG::SERVCONF proto(config.server.data(),
144 config.userName.data(),
145 config.userPass.data());
146 return proto.ChgService(conf, SimpleCallback, NULL) == STG::st_ok;
149 } // namespace anonymous
151 void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
153 std::vector<API_ACTION::PARAM> params(GetServiceParams());
154 blocks.Add("Service management options")
155 .Add("get-services", SGCONF::MakeAPIAction(commands, GetServicesFunction), "\tget service list")
156 .Add("get-service", SGCONF::MakeAPIAction(commands, "<name>", GetServiceFunction), "get service")
157 .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", params, AddServiceFunction), "add service")
158 .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", DelServiceFunction), "del service")
159 .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", params, ChgServiceFunction), "change service");