]> git.stg.codes - stg.git/blob - projects/sgconf/services.cpp
719435c7fea638dad9926ab80ee12d40786d1cf2
[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 void SimpleCallback(bool result,
35                     const std::string & reason,
36                     void * /*data*/)
37 {
38 if (!result)
39     {
40     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
41     return;
42     }
43 std::cout << "Success.\n";
44 }
45
46 void GetServicesCallback(bool result,
47                          const std::string & reason,
48                          const std::vector<STG::GET_SERVICE::INFO> & info,
49                          void * /*data*/)
50 {
51 if (!result)
52     {
53     std::cerr << "Failed to get service list. Reason: '" << reason << "'." << std::endl;
54     return;
55     }
56 std::cout << "Services:\n";
57 for (size_t i = 0; i < info.size(); ++i)
58     PrintService(info[i], 1);
59 }
60
61 void GetServiceCallback(bool result,
62                         const std::string & reason,
63                         const STG::GET_SERVICE::INFO & info,
64                         void * /*data*/)
65 {
66 if (!result)
67     {
68     std::cerr << "Failed to get service. Reason: '" << reason << "'." << std::endl;
69     return;
70     }
71 PrintService(info);
72 }
73
74 bool GetServicesFunction(const SGCONF::CONFIG & config,
75                          const std::string & /*arg*/,
76                          const std::map<std::string, std::string> & /*options*/)
77 {
78 STG::SERVCONF proto(config.server.data(),
79                     config.port.data(),
80                     config.userName.data(),
81                     config.userPass.data());
82 return proto.GetServices(GetServicesCallback, NULL) == STG::st_ok;
83 }
84
85 bool GetServiceFunction(const SGCONF::CONFIG & config,
86                         const std::string & arg,
87                         const std::map<std::string, std::string> & /*options*/)
88 {
89 STG::SERVCONF proto(config.server.data(),
90                     config.port.data(),
91                     config.userName.data(),
92                     config.userPass.data());
93 return proto.GetService(arg, GetServiceCallback, NULL) == STG::st_ok;
94 }
95
96 bool DelServiceFunction(const SGCONF::CONFIG & config,
97                         const std::string & arg,
98                         const std::map<std::string, std::string> & /*options*/)
99 {
100 STG::SERVCONF proto(config.server.data(),
101                     config.port.data(),
102                     config.userName.data(),
103                     config.userPass.data());
104 return proto.DelService(arg, SimpleCallback, NULL) == STG::st_ok;
105 }
106
107 bool AddServiceFunction(const SGCONF::CONFIG & config,
108                         const std::string & arg,
109                         const std::map<std::string, std::string> & /*options*/)
110 {
111 // TODO
112 std::cerr << "Unimplemented.\n";
113 return false;
114 }
115
116 bool ChgServiceFunction(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 } // namespace anonymous
126
127 void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
128 {
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>", GetServiceFunction), "get service")
132       .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", AddServiceFunction), "add service")
133       .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", DelServiceFunction), "del service")
134       .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", ChgServiceFunction), "change service");
135 }