]> git.stg.codes - stg.git/blob - projects/sgconf/services.cpp
3609166da76fcf88814f7828c04631cd2d20fe35
[stg.git] / projects / sgconf / services.cpp
1 #include "services.h"
2
3 #include "config.h"
4
5 #include "stg/servconf.h"
6 #include "stg/servconf_types.h"
7 #include "stg/service_conf.h"
8 #include "stg/common.h"
9
10 #include <iostream>
11
12 namespace
13 {
14
15 std::string Indent(size_t level, bool dash = false)
16 {
17 if (level == 0)
18     return "";
19 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
20 }
21
22 void PrintService(const STG::GET_SERVICE::INFO & info, size_t level = 0)
23 {
24 std::cout << Indent(level, true) << "name: " << info.name << "\n"
25           << Indent(level)       << "cost: " << info.cost << "\n"
26           << Indent(level)       << "payment day: " << info.payDay << "\n"
27           << Indent(level)       << "comment: " << info.comment << "\n";
28 }
29
30 void SimpleCallback(bool result,
31                     const std::string & reason,
32                     void * /*data*/)
33 {
34 if (!result)
35     {
36     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
37     return;
38     }
39 std::cout << "Success.\n";
40 }
41
42 void GetServicesCallback(bool result,
43                          const std::string & reason,
44                          const std::vector<STG::GET_SERVICE::INFO> & info,
45                          void * /*data*/)
46 {
47 if (!result)
48     {
49     std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl;
50     return;
51     }
52 std::cout << "Services:\n";
53 for (size_t i = 0; i < info.size(); ++i)
54     PrintService(info[i], 1);
55 }
56
57 void GetServiceCallback(bool result,
58                         const std::string & reason,
59                         const STG::GET_SERVICE::INFO & info,
60                         void * /*data*/)
61 {
62 if (!result)
63     {
64     std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl;
65     return;
66     }
67 PrintService(info);
68 }
69
70 } // namespace anonymous
71
72 bool SGCONF::GetServicesFunction(const SGCONF::CONFIG & config,
73                                  const std::string & /*arg*/,
74                                  const std::map<std::string, std::string> & /*options*/)
75 {
76 STG::SERVCONF proto(config.server.data(),
77                     config.port.data(),
78                     config.userName.data(),
79                     config.userPass.data());
80 return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok;
81 }
82
83 bool SGCONF::GetServiceFunction(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.GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
92 }
93
94 bool SGCONF::DelServiceFunction(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.DelService(arg, SimpleCallback, NULL) == STG::st_ok;
103 }
104
105 bool SGCONF::AddServiceFunction(const SGCONF::CONFIG & config,
106                                 const std::string & arg,
107                                 const std::map<std::string, std::string> & /*options*/)
108 {
109 // TODO
110 std::cerr << "Unimplemented.\n";
111 return false;
112 }
113
114 bool SGCONF::ChgServiceFunction(const SGCONF::CONFIG & config,
115                                 const std::string & arg,
116                                 const std::map<std::string, std::string> & options)
117 {
118 // TODO
119 std::cerr << "Unimplemented.\n";
120 return false;
121 }