3 #include "api_action.h"
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/service_conf.h"
10 #include "stg/common.h"
19 std::string Indent(size_t level, bool dash = false)
23 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
26 void PrintService(const STG::GET_SERVICE::INFO & info, size_t level = 0)
28 std::cout << Indent(level, true) << "name: " << info.name << "\n"
29 << Indent(level) << "cost: " << info.cost << "\n"
30 << Indent(level) << "payment day: " << info.payDay << "\n"
31 << Indent(level) << "comment: " << info.comment << "\n";
34 std::vector<SGCONF::API_ACTION::PARAM> GetServiceParams()
36 std::vector<SGCONF::API_ACTION::PARAM> params;
37 params.push_back({"cost", "<cost>", "\tcost of the service"});
38 params.push_back({"pay-day", "<month day>", "payment day"});
39 params.push_back({"comment", "<text>", "comment"});
43 void SimpleCallback(bool result,
44 const std::string & reason,
49 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
52 std::cout << "Success.\n";
55 void GetServicesCallback(bool result,
56 const std::string & reason,
57 const std::vector<STG::GET_SERVICE::INFO> & info,
62 std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl;
65 std::cout << "Services:\n";
66 for (size_t i = 0; i < info.size(); ++i)
67 PrintService(info[i], 1);
70 void GetServiceCallback(bool result,
71 const std::string & reason,
72 const STG::GET_SERVICE::INFO & info,
77 std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl;
83 bool GetServicesFunction(const SGCONF::CONFIG & config,
84 const std::string & /*arg*/,
85 const std::map<std::string, std::string> & /*options*/)
87 STG::SERVCONF proto(config.server.data(),
89 config.userName.data(),
90 config.userPass.data());
91 return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok;
94 bool GetServiceFunction(const SGCONF::CONFIG & config,
95 const std::string & arg,
96 const std::map<std::string, std::string> & /*options*/)
98 STG::SERVCONF proto(config.server.data(),
100 config.userName.data(),
101 config.userPass.data());
102 return proto.GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
105 bool DelServiceFunction(const SGCONF::CONFIG & config,
106 const std::string & arg,
107 const std::map<std::string, std::string> & /*options*/)
109 STG::SERVCONF proto(config.server.data(),
111 config.userName.data(),
112 config.userPass.data());
113 return proto.DelService(arg, SimpleCallback, NULL) == STG::st_ok;
116 bool AddServiceFunction(const SGCONF::CONFIG & config,
117 const std::string & arg,
118 const std::map<std::string, std::string> & /*options*/)
121 std::cerr << "Unimplemented.\n";
125 bool ChgServiceFunction(const SGCONF::CONFIG & config,
126 const std::string & arg,
127 const std::map<std::string, std::string> & options)
130 std::cerr << "Unimplemented.\n";
134 } // namespace anonymous
136 void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
138 std::vector<API_ACTION::PARAM> params(GetServiceParams());
139 blocks.Add("Service management options")
140 .Add("get-services", SGCONF::MakeAPIAction(commands, GetServicesFunction), "\tget service list")
141 .Add("get-service", SGCONF::MakeAPIAction(commands, "<name>", GetServiceFunction), "get service")
142 .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", params, AddServiceFunction), "add service")
143 .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", DelServiceFunction), "del service")
144 .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", params, ChgServiceFunction), "change service");