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 void SimpleCallback(bool result,
35 const std::string & reason,
40 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
43 std::cout << "Success.\n";
46 void GetServicesCallback(bool result,
47 const std::string & reason,
48 const std::vector<STG::GET_SERVICE::INFO> & info,
53 std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl;
56 std::cout << "Services:\n";
57 for (size_t i = 0; i < info.size(); ++i)
58 PrintService(info[i], 1);
61 void GetServiceCallback(bool result,
62 const std::string & reason,
63 const STG::GET_SERVICE::INFO & info,
68 std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl;
74 bool GetServicesFunction(const SGCONF::CONFIG & config,
75 const std::string & /*arg*/,
76 const std::map<std::string, std::string> & /*options*/)
78 STG::SERVCONF proto(config.server.data(),
80 config.userName.data(),
81 config.userPass.data());
82 return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok;
85 bool GetServiceFunction(const SGCONF::CONFIG & config,
86 const std::string & arg,
87 const std::map<std::string, std::string> & /*options*/)
89 STG::SERVCONF proto(config.server.data(),
91 config.userName.data(),
92 config.userPass.data());
93 return proto.GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
96 bool DelServiceFunction(const SGCONF::CONFIG & config,
97 const std::string & arg,
98 const std::map<std::string, std::string> & /*options*/)
100 STG::SERVCONF proto(config.server.data(),
102 config.userName.data(),
103 config.userPass.data());
104 return proto.DelService(arg, SimpleCallback, NULL) == STG::st_ok;
107 bool AddServiceFunction(const SGCONF::CONFIG & config,
108 const std::string & arg,
109 const std::map<std::string, std::string> & /*options*/)
112 std::cerr << "Unimplemented.\n";
116 bool ChgServiceFunction(const SGCONF::CONFIG & config,
117 const std::string & arg,
118 const std::map<std::string, std::string> & options)
121 std::cerr << "Unimplemented.\n";
125 } // namespace anonymous
127 void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
129 blocks.Add("Service management options")
130 .Add("get-services", SGCONF::MakeAPIAction(commands, GetServicesFunction), "\tget service list")
131 .Add("get-service", SGCONF::MakeAPIAction(commands, "<name>", true, GetServiceFunction), "get service")
132 .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", true, AddServiceFunction), "add service")
133 .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", true, DelServiceFunction), "del service")
134 .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", true, ChgServiceFunction), "change service");