3 #include "api_action.h"
9 #include "stg/servconf.h"
10 #include "stg/servconf_types.h"
11 #include "stg/service_conf.h"
12 #include "stg/common.h"
21 std::string Indent(size_t level, bool dash = false)
25 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
28 void PrintService(const STG::GetService::Info & info, size_t level = 0)
30 std::cout << Indent(level, true) << "name: " << info.name << "\n"
31 << Indent(level) << "cost: " << info.cost << "\n"
32 << Indent(level) << "payment day: " << static_cast<unsigned>(info.payDay) << "\n"
33 << Indent(level) << "comment: " << info.comment << "\n";
36 std::vector<SGCONF::API_ACTION::PARAM> GetServiceParams()
38 std::vector<SGCONF::API_ACTION::PARAM> params;
39 params.push_back(SGCONF::API_ACTION::PARAM("cost", "<cost>", "\tcost of the service"));
40 params.push_back(SGCONF::API_ACTION::PARAM("pay-day", "<month day>", "payment day"));
41 params.push_back(SGCONF::API_ACTION::PARAM("comment", "<text>", "comment"));
45 void SimpleCallback(bool result,
46 const std::string & reason,
51 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
54 std::cout << "Success.\n";
57 void GetServicesCallback(bool result,
58 const std::string & reason,
59 const std::vector<STG::GetService::Info> & info,
64 std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl;
67 std::cout << "Services:\n";
68 for (size_t i = 0; i < info.size(); ++i)
69 PrintService(info[i], 1);
72 void GetServiceCallback(bool result,
73 const std::string & reason,
74 const STG::GetService::Info & info,
79 std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl;
85 bool GetServicesFunction(const SGCONF::CONFIG & config,
86 const std::string & /*arg*/,
87 const std::map<std::string, std::string> & /*options*/)
89 return makeProto(config).GetServices(GetServicesCallback, NULL) == STG::st_ok;
92 bool GetServiceFunction(const SGCONF::CONFIG & config,
93 const std::string & arg,
94 const std::map<std::string, std::string> & /*options*/)
96 return makeProto(config).GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
99 bool DelServiceFunction(const SGCONF::CONFIG & config,
100 const std::string & arg,
101 const std::map<std::string, std::string> & /*options*/)
103 return makeProto(config).DelService(arg, SimpleCallback, NULL) == STG::st_ok;
106 bool AddServiceFunction(const SGCONF::CONFIG & config,
107 const std::string & arg,
108 const std::map<std::string, std::string> & options)
110 STG::ServiceConfOpt conf;
112 SGCONF::MaybeSet(options, "cost", conf.cost);
113 SGCONF::MaybeSet(options, "pay-day", conf.payDay);
114 SGCONF::MaybeSet(options, "comment", conf.comment);
115 return makeProto(config).AddService(arg, conf, SimpleCallback, NULL) == STG::st_ok;
118 bool ChgServiceFunction(const SGCONF::CONFIG & config,
119 const std::string & arg,
120 const std::map<std::string, std::string> & options)
122 STG::ServiceConfOpt conf;
124 SGCONF::MaybeSet(options, "cost", conf.cost);
125 SGCONF::MaybeSet(options, "pay-day", conf.payDay);
126 SGCONF::MaybeSet(options, "comment", conf.comment);
127 return makeProto(config).ChgService(conf, SimpleCallback, NULL) == STG::st_ok;
130 } // namespace anonymous
132 void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
134 std::vector<API_ACTION::PARAM> params(GetServiceParams());
135 blocks.Add("Service management options")
136 .Add("get-services", SGCONF::MakeAPIAction(commands, GetServicesFunction), "\tget service list")
137 .Add("get-service", SGCONF::MakeAPIAction(commands, "<name>", GetServiceFunction), "get service")
138 .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", params, AddServiceFunction), "add service")
139 .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", DelServiceFunction), "delete service")
140 .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", params, ChgServiceFunction), "change service");