]> git.stg.codes - stg.git/blob - sgconf/services.cpp
Add missing include.
[stg.git] / sgconf / services.cpp
1 #include "services.h"
2
3 #include "api_action.h"
4 #include "options.h"
5 #include "config.h"
6 #include "utils.h"
7
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/service_conf.h"
11 #include "stg/common.h"
12
13 #include <iostream>
14 #include <string>
15 #include <map>
16
17 namespace
18 {
19
20 std::string Indent(size_t level, bool dash = false)
21 {
22 if (level == 0)
23     return "";
24 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
25 }
26
27 void PrintService(const STG::GetService::Info & info, size_t level = 0)
28 {
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";
33 }
34
35 std::vector<SGCONF::API_ACTION::PARAM> GetServiceParams()
36 {
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"));
41 return params;
42 }
43
44 void SimpleCallback(bool result,
45                     const std::string & reason,
46                     void * /*data*/)
47 {
48 if (!result)
49     {
50     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
51     return;
52     }
53 std::cout << "Success.\n";
54 }
55
56 void GetServicesCallback(bool result,
57                          const std::string & reason,
58                          const std::vector<STG::GetService::Info> & info,
59                          void * /*data*/)
60 {
61 if (!result)
62     {
63     std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl;
64     return;
65     }
66 std::cout << "Services:\n";
67 for (size_t i = 0; i < info.size(); ++i)
68     PrintService(info[i], 1);
69 }
70
71 void GetServiceCallback(bool result,
72                         const std::string & reason,
73                         const STG::GetService::Info & info,
74                         void * /*data*/)
75 {
76 if (!result)
77     {
78     std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl;
79     return;
80     }
81 PrintService(info);
82 }
83
84 bool GetServicesFunction(const SGCONF::CONFIG & config,
85                          const std::string & /*arg*/,
86                          const std::map<std::string, std::string> & /*options*/)
87 {
88 STG::ServConf proto(config.server.data(),
89                     config.port.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;
95 }
96
97 bool GetServiceFunction(const SGCONF::CONFIG & config,
98                         const std::string & arg,
99                         const std::map<std::string, std::string> & /*options*/)
100 {
101 STG::ServConf proto(config.server.data(),
102                     config.port.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;
108 }
109
110 bool DelServiceFunction(const SGCONF::CONFIG & config,
111                         const std::string & arg,
112                         const std::map<std::string, std::string> & /*options*/)
113 {
114 STG::ServConf proto(config.server.data(),
115                     config.port.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;
121 }
122
123 bool AddServiceFunction(const SGCONF::CONFIG & config,
124                         const std::string & arg,
125                         const std::map<std::string, std::string> & options)
126 {
127 STG::ServiceConfOpt conf;
128 conf.name = arg;
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(),
133                     config.port.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;
139 }
140
141 bool ChgServiceFunction(const SGCONF::CONFIG & config,
142                         const std::string & arg,
143                         const std::map<std::string, std::string> & options)
144 {
145 STG::ServiceConfOpt conf;
146 conf.name = arg;
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(),
151                     config.port.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;
157 }
158
159 } // namespace anonymous
160
161 void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
162 {
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");
170 }