Repository navigation
Expand file tree
/
Copy pathhandler-google-sheets-config.cpp
More file actions
299 lines (252 loc) · 7.9 KB
/
Copy pathhandler-google-sheets-config.cpp
File metadata and controls
299 lines (252 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "handler-google-sheets-config.h"
#include <iostream>
#include <limits.h>
#include <stdlib.h>
#include <iostream>
#if defined(_WIN32) || defined(_WIN64)
#include "platform.h"
#include "utilfile.h"
#else
#include <unistd.h>
#endif
#include "argtable3/argtable3.h"
#include "utilstring.h"
#include "errorcodes.h"
#define DEF_DB_PATH "db"
#define DEF_MODE 0
#define DEF_BUFFER_SIZE 4096
#define DEF_FLAGS 0
#define DEF_QUEUE "ipc:///tmp/message.pkt2"
#define DEF_PROTO_PATH "proto"
// Google service
#define DEF_GOOGLE_JSON "cert/pkt2-sheets.json"
#define DEF_TOKEN_FILE ".token-bearer.txt"
#define DEF_SCOPE "https://www.googleapis.com/auth/spreadsheets"
#define DEF_AUDIENCE "https://www.googleapis.com/oauth2/v4/token"
#define DEF_EXPIRES 3600
void ontokenbearer
(
void *env,
const std::string &value,
int status
)
{
Config *config = (Config *) env;
if (status == 0)
{
pkt2utilstring::string2file(config->token_file, value);
std::cerr << "New token bearer: " << value << " saved in " << config->token_file << std::endl;
}
else
std::cerr << "Error " << status << " getting token bearer: " << value
<< " from " << config->token_file
<< std::endl;;
}
Config::Config
(
int argc,
char* argv[]
)
{
stop_request = 0;
lastError = parseCmd(argc, argv);
if (lastError != 0)
{
lastError = ERRCODE_COMMAND;
google_sheets = NULL;
return;
}
std::string json_google_service = pkt2utilstring::file2string(json);
std::string pub_email;
readGoogleTokenJSON(json_google_service, service_account, pemkey, pub_email);
if (pemkey.empty() || service_account.empty())
{
lastError = ERRCODE_GS_INVALID_CFG_JSON;
google_sheets = NULL;
return;
}
if (subject_email.empty())
subject_email = pub_email;
std::string last_token = pkt2utilstring::file2string(token_file);
google_sheets = new GoogleSheets(
spreadsheet,
last_token,
service_account,
subject_email,
pemkey,
scope,
audience,
&ontokenbearer,
(void *) this,
verbosity
);
}
Config::~Config()
{
if (google_sheets)
delete google_sheets;
}
int Config::error()
{
return lastError;
}
/**
* Parse command line into struct ClientConfig
* Return 0- success
* 1- show help and exit, or command syntax error
* 2- output file does not exists or can not open to write
**/
int Config::parseCmd
(
int argc,
char* argv[]
)
{
struct arg_str *a_proto_path = arg_str0("p", "protos", "<path>", "proto file directory. Default " DEF_PROTO_PATH);
struct arg_str *a_message_url = arg_str0("i", "input", "<queue url>", "Default ipc:///tmp/message.pkt2");
struct arg_str *a_allowed_messages = arg_strn("a", "allow", "<packet.message>", 0, 128, "Allowed message packet.name. Default any.");
struct arg_int *a_retries = arg_int0("r", "repeat", "<n>", "Restart listen. Default 0.");
struct arg_int *a_retry_delay = arg_int0("y", "delay", "<seconds>", "Delay on restart in seconds. Default 60.");
struct arg_lit *a_daemonize = arg_lit0("d", "daemonize", "Start as daemon/service");
struct arg_lit *a_verbosity = arg_litn("v", "verbosity", 0, 3, "Verbosity level. 3- debug");
struct arg_int *a_mode = arg_int0("m", "mode", "<number>", "Reserved. Default 0");
// Google service auth
// JSON contains all others
struct arg_file *a_json = arg_file0("g", "google", "<file>", "JSON service file name. Default " DEF_GOOGLE_JSON);
struct arg_file *a_token_file = arg_file0("B", "bearer", "<file>", "save token bearer. Default " DEF_TOKEN_FILE);
struct arg_str *a_subject_email = arg_str1("e", "sub", "<email>", "subject email.");
struct arg_str *a_spreadsheet = arg_str1("s", "spreadsheet", "<id>", "Google spreadsheet id");
struct arg_str *a_sheet = arg_str1("t", "sheet", "<name>", "Sheet name");
struct arg_int *a_format_number = arg_int0(NULL, "format", "<number>", "Default 0");
struct arg_int *a_buffer_size = arg_int0("b", "buffer", "<size>", "Receiver buffer size. Default 4096");
struct arg_str *a_table_alias = arg_strn("T", "table-alias", "<alias=message>", 0, 100, "set table alias for message");
struct arg_str *a_field_alias = arg_strn("F", "field-alias", "<alias=field>", 0, 100, "set field alias");
struct arg_str *a_properties = arg_strn("P", "properties", "<property=name>", 0, 100, "set property");
struct arg_lit *a_help = arg_lit0("h", "help", "Show this help");
struct arg_end *a_end = arg_end(20);
void* argtable[] = {
a_proto_path,
a_message_url, a_allowed_messages,
a_retries, a_retry_delay,
// OAuth, Google sheets
a_json, a_token_file, a_subject_email, a_spreadsheet, a_sheet,
a_format_number,
a_daemonize, a_verbosity, a_mode, a_buffer_size,
a_table_alias, a_field_alias, a_properties,
a_help, a_end
};
int nerrors;
// verify the argtable[] entries were allocated successfully
if (arg_nullcheck(argtable) != 0)
{
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return 1;
}
// Parse the command line as defined by argtable[]
nerrors = arg_parse(argc, argv, argtable);
// special case: '--help' takes precedence over error reporting
if ((a_help->count) || nerrors)
{
if (nerrors)
arg_print_errors(stderr, a_end, PROGRAM_NAME);
printf("Usage: %s\n", PROGRAM_NAME);
arg_print_syntax(stdout, argtable, "\n");
printf("%s\n", PROGRAM_DESCRIPTION);
arg_print_glossary(stdout, argtable, " %-25s %s\n");
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return ERRCODE_PARSE_COMMAND;
}
if (a_proto_path->count)
proto_path = *a_proto_path->sval;
else
proto_path = DEF_PROTO_PATH;
// get real path
char b[PATH_MAX];
char *pp = realpath(proto_path.c_str(), b);
if (pp)
proto_path = std::string(pp);
else {
std::cerr << ERR_INVALID_PROTO_PATH << std::endl;
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return ERRCODE_INVALID_PROTO_PATH;
}
if (a_message_url->count)
message_url = *a_message_url->sval;
else
message_url = DEF_QUEUE;
for (int i = 0; i < a_allowed_messages->count; i++)
{
allowed_messages.push_back(a_allowed_messages->sval[i]);
}
if (a_retries->count)
retries = *a_retries->ival;
else
retries = 0;
if (a_retry_delay->count)
retry_delay = *a_retry_delay->ival;
else
retry_delay = 60;
verbosity = a_verbosity->count;
daemonize = a_daemonize->count > 0;
if (a_mode->count)
mode = *a_mode->ival;
else
mode = DEF_MODE;
if (a_format_number->count)
format_number = *a_format_number->ival;
else
format_number = 0;
// OAuth, Google sheets
if (a_spreadsheet->count)
spreadsheet = *a_spreadsheet->sval;
if (a_sheet->count)
sheet = *a_sheet->sval;
if (a_buffer_size->count)
buffer_size = *a_buffer_size->ival;
else
buffer_size = DEF_BUFFER_SIZE;
if (a_json->count)
json = *a_json->filename;
else
json = DEF_GOOGLE_JSON;
if (a_token_file->count)
token_file = *a_token_file->filename;
else
token_file = DEF_TOKEN_FILE;
if (a_subject_email->count)
subject_email = *a_subject_email->sval;
scope = DEF_SCOPE;
audience = DEF_AUDIENCE;
expires = DEF_EXPIRES;
char wd[PATH_MAX];
path = getcwd(wd, PATH_MAX);
for (int i = 0; i < a_table_alias->count; i++) {
std::string line = a_table_alias->sval[i];
size_t p = line.find('=');
if (p == std::string::npos)
continue;
std::string n = line.substr(0, p);
std::string v = line.substr(p + 1);
tableAliases[n] = v;
}
for (int i = 0; i < a_field_alias->count; i++) {
std::string line = a_field_alias->sval[i];
size_t p = line.find('=');
if (p == std::string::npos)
continue;
std::string n = line.substr(0, p);
std::string v = line.substr(p + 1);
fieldAliases[n] = v;
}
for (int i = 0; i < a_properties->count; i++) {
std::string line = a_properties->sval[i];
size_t p = line.find('=');
if (p == std::string::npos)
continue;
std::string n = line.substr(0, p);
std::string v = line.substr(p + 1);
properties[n] = v;
}
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return ERR_OK;
}