]> git.stg.codes - stg.git/blob - projects/sgconf/services.cpp
Expanded more abbreviations.
[stg.git] / projects / 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::GET_SERVICE::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: " << 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::GET_SERVICE::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::GET_SERVICE::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.userName.data(),
91                     config.userPass.data());
92 return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok;
93 }
94
95 bool GetServiceFunction(const SGCONF::CONFIG & config,
96                         const std::string & arg,
97                         const std::map<std::string, std::string> & /*options*/)
98 {
99 STG::SERVCONF proto(config.server.data(),
100                     config.port.data(),
101                     config.userName.data(),
102                     config.userPass.data());
103 return proto.GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
104 }
105
106 bool DelServiceFunction(const SGCONF::CONFIG & config,
107                         const std::string & arg,
108                         const std::map<std::string, std::string> & /*options*/)
109 {
110 STG::SERVCONF proto(config.server.data(),
111                     config.port.data(),
112                     config.userName.data(),
113                     config.userPass.data());
114 return proto.DelService(arg, SimpleCallback, NULL) == STG::st_ok;
115 }
116
117 bool AddServiceFunction(const SGCONF::CONFIG & config,
118                         const std::string & arg,
119                         const std::map<std::string, std::string> & options)
120 {
121 SERVICE_CONF_RES conf;
122 conf.name = arg;
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(),
127                     config.port.data(),
128                     config.userName.data(),
129                     config.userPass.data());
130 return proto.AddService(arg, conf, SimpleCallback, NULL) == STG::st_ok;
131 }
132
133 bool ChgServiceFunction(const SGCONF::CONFIG & config,
134                         const std::string & arg,
135                         const std::map<std::string, std::string> & options)
136 {
137 SERVICE_CONF_RES conf;
138 conf.name = arg;
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(),
143                     config.port.data(),
144                     config.userName.data(),
145                     config.userPass.data());
146 return proto.ChgService(conf, SimpleCallback, NULL) == STG::st_ok;
147 }
148
149 } // namespace anonymous
150
151 void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
152 {
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), "delete service")
159       .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", params, ChgServiceFunction), "change service");
160 }