]> git.stg.codes - stg.git/blob - projects/sgconf/services.cpp
f5d737428c8960026ef2e0558dffc0c68e1e7de0
[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
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/service_conf.h"
10 #include "stg/common.h"
11
12 #include <iostream>
13 #include <string>
14 #include <map>
15
16 namespace
17 {
18
19 std::string Indent(size_t level, bool dash = false)
20 {
21 if (level == 0)
22     return "";
23 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
24 }
25
26 void PrintService(const STG::GET_SERVICE::INFO & info, size_t level = 0)
27 {
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";
32 }
33
34 std::vector<SGCONF::API_ACTION::PARAM> GetServiceParams()
35 {
36 std::vector<SGCONF::API_ACTION::PARAM> params;
37 params.push_back(SGCONF::API_ACTION::PARAM("cost", "<cost>", "\tcost of the service"));
38 params.push_back(SGCONF::API_ACTION::PARAM("pay-day", "<month day>", "payment day"));
39 params.push_back(SGCONF::API_ACTION::PARAM("comment", "<text>", "comment"));
40 return params;
41 }
42
43 void SimpleCallback(bool result,
44                     const std::string & reason,
45                     void * /*data*/)
46 {
47 if (!result)
48     {
49     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
50     return;
51     }
52 std::cout << "Success.\n";
53 }
54
55 void GetServicesCallback(bool result,
56                          const std::string & reason,
57                          const std::vector<STG::GET_SERVICE::INFO> & info,
58                          void * /*data*/)
59 {
60 if (!result)
61     {
62     std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl;
63     return;
64     }
65 std::cout << "Services:\n";
66 for (size_t i = 0; i < info.size(); ++i)
67     PrintService(info[i], 1);
68 }
69
70 void GetServiceCallback(bool result,
71                         const std::string & reason,
72                         const STG::GET_SERVICE::INFO & info,
73                         void * /*data*/)
74 {
75 if (!result)
76     {
77     std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl;
78     return;
79     }
80 PrintService(info);
81 }
82
83 bool GetServicesFunction(const SGCONF::CONFIG & config,
84                          const std::string & /*arg*/,
85                          const std::map<std::string, std::string> & /*options*/)
86 {
87 STG::SERVCONF proto(config.server.data(),
88                     config.port.data(),
89                     config.userName.data(),
90                     config.userPass.data());
91 return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok;
92 }
93
94 bool GetServiceFunction(const SGCONF::CONFIG & config,
95                         const std::string & arg,
96                         const std::map<std::string, std::string> & /*options*/)
97 {
98 STG::SERVCONF proto(config.server.data(),
99                     config.port.data(),
100                     config.userName.data(),
101                     config.userPass.data());
102 return proto.GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
103 }
104
105 bool DelServiceFunction(const SGCONF::CONFIG & config,
106                         const std::string & arg,
107                         const std::map<std::string, std::string> & /*options*/)
108 {
109 STG::SERVCONF proto(config.server.data(),
110                     config.port.data(),
111                     config.userName.data(),
112                     config.userPass.data());
113 return proto.DelService(arg, SimpleCallback, NULL) == STG::st_ok;
114 }
115
116 bool AddServiceFunction(const SGCONF::CONFIG & config,
117                         const std::string & arg,
118                         const std::map<std::string, std::string> & /*options*/)
119 {
120 // TODO
121 std::cerr << "Unimplemented.\n";
122 return false;
123 }
124
125 bool ChgServiceFunction(const SGCONF::CONFIG & config,
126                         const std::string & arg,
127                         const std::map<std::string, std::string> & options)
128 {
129 // TODO
130 std::cerr << "Unimplemented.\n";
131 return false;
132 }
133
134 } // namespace anonymous
135
136 void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
137 {
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");
145 }