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