-
Notifications
You must be signed in to change notification settings - Fork 0
/
worklog_main.cc
533 lines (414 loc) · 14.5 KB
/
worklog_main.cc
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "atl/colors.h"
#include "atl/file.h"
#include "atl/status.h"
#include "atl/statusor.h"
#include "atl/string.h"
#include "atl/time.h"
#include "command.h"
#include "filter.h"
#include "serializer.h"
#include "utils.h"
#include "worklog.h"
#include "storage.h"
int CommandNewWorklog(const worklog::CommandContext& ctx) {
atl::TempFile tmp_file;
if (!tmp_file) {
std::cerr << "Error: Failed to create temp file at: " << tmp_file.path()
<< "\n";
return -1;
}
tmp_file.stream() << Template();
tmp_file.stream().flush();
auto content = worklog::ContentFromEditor(tmp_file.path());
if (!content) {
std::cerr << "Error: Failed to obtain content from the editor "
<< "which is stored in the file: " << tmp_file.path() << "\n";
return -1;
}
worklog::HumanSerializer hs;
worklog::Log log = hs.Unserialize(content.value());
worklog::Storage store(ctx.config);
atl::Status status = store.Save(log);
if (!status.ok()) {
std::cerr << "Failed to save the work log. Reason: "
<< status.error_message()
<< ". Your work log is backed up here: " << tmp_file.path()
<< "\n";
return -1;
}
return 0;
}
int CommandEditWorklog(const worklog::CommandContext& ctx) {
if (ctx.args.size() < 3) {
std::cerr << "Error: Please specify a work log id\n";
return -1;
}
const std::string& worklog_id = ctx.args[2];
worklog::Storage store(ctx.config);
atl::Optional<int> id = NumberFromString(worklog_id);
if (!id) {
std::cerr << "Error: Failed to convert worklog id to numeric value: "
<< worklog_id << "\n";
return -1;
}
atl::StatusOr<worklog::Log> status = store.LoadById(id.value());
if (!status.ok()) {
std::cerr << "Error: Failed to load worklog by id " << worklog_id << ". "
<< status.status().error_message() << "\n";
return -1;
}
auto log = status.ValueOrDie();
worklog::HumanSerializer hs;
atl::TempFile tmp_file;
if (!tmp_file) {
std::cerr << "Error: Failed to create temp file at: " << tmp_file.path()
<< "\n";
return -1;
}
tmp_file.stream() << hs.Serialize(log);
tmp_file.stream().flush();
auto content = worklog::ContentFromEditor(tmp_file.path());
if (!content) {
std::cerr << "Error: Failed to obtain content from the editor "
<< "which is stored in the file: " << tmp_file.path() << "\n";
return -1;
}
worklog::Log updatedLog = hs.Unserialize(content.value());
updatedLog.id = log.id;
atl::Status saveStatus = store.Update(updatedLog);
if (!saveStatus.ok()) {
std::cerr << "Failed to save the work log. Reason: "
<< saveStatus.error_message()
<< ". Your work log is backed up here: " << tmp_file.path()
<< "\n";
return -1;
}
return 0;
}
int CommandViewWorklog(const worklog::CommandContext& ctx) {
if (ctx.args.size() < 3) {
std::cerr << "Error: Please specify a work log id\n";
return -1;
}
const std::string& worklog_id = ctx.args[2];
worklog::Storage store(ctx.config);
atl::Optional<int> id = NumberFromString(worklog_id);
if (!id) {
std::cerr << "Error: Failed to convert worklog id to numeric value: "
<< worklog_id << "\n";
return -1;
}
atl::StatusOr<worklog::Log> status = store.LoadById(id.value());
if (!status.ok()) {
std::cerr << "Error: Failed to load worklog by id " << worklog_id << ". "
<< status.status().error_message() << "\n";
return -1;
}
auto log = status.ValueOrDie();
worklog::HumanSerializer hs;
std::cout << hs.Serialize(log) << "\n";
return 0;
}
int CommandInitWorklog(const worklog::CommandContext& ctx) {
atl::Status status = worklog::MaybeSetupWorklogSpace(ctx.config);
if (!status.ok()) {
std::cerr << "Failed to setup worklog space: " << status.error_message()
<< "\n";
return -1;
}
return 0;
}
int CommandDeleteWorklog(const worklog::CommandContext& ctx) {
if (ctx.args.size() < 3) {
std::cerr << "Error: Please specify a work log id\n";
return -1;
}
int id = 0;
try {
id = std::stoi(ctx.args[2]);
} catch (const std::exception& e) {
std::cerr << "Error: cannot extract id from parameter (not a number?): "
<< e.what() << "\n";
return -1;
}
std::string log_path =
atl::JoinStr("/", ctx.config.logs_dir, std::to_string(id));
if (!atl::FileExists(log_path)) {
return 0;
}
atl::Remove(log_path);
return 0;
}
int CommandListBroken(const worklog::CommandContext& ctx) {
auto index = IndexFromDir(ctx.config.logs_dir);
worklog::ApplyFilter(worklog::OnlyInvalidFilter(), &index);
for (const auto& log : index) {
PrintWorklog(log);
}
return 0;
}
int CommandListAll(const worklog::CommandContext& ctx) {
auto index = IndexFromDir(ctx.config.logs_dir);
worklog::ApplyFilter(worklog::OnlyValidFilter(), &index);
for (const auto& log : index) {
PrintWorklog(log);
}
return 0;
}
int SubCommandTagsListAll(const worklog::CommandContext& ctx) {
auto index = IndexFromDir(ctx.config.logs_dir);
worklog::ApplyFilter(worklog::OnlyValidFilter(), &index);
std::vector<std::string> tags;
std::unordered_map<std::string, int> counts;
for (const auto& log : index) {
for (const auto& tag : log.tags) {
auto found = counts.find(tag);
if (found == counts.end()) {
tags.push_back(tag);
}
counts[tag]++;
}
}
std::sort(tags.begin(), tags.end(),
[&counts](const std::string& a, const std::string& b) {
return counts[a] > counts[b];
});
for (const auto& tag : tags) {
std::cout << counts[tag] << " " << tag << "\n";
}
return 0;
}
int SubCommandTagsRemove(const worklog::CommandContext& ctx) {
const std::string& tag = ctx.args[3];
const std::string& worklog_id = ctx.args[4];
worklog::Storage store(ctx.config);
atl::Optional<int> id = NumberFromString(worklog_id);
if (!id) {
std::cerr << "Error: Failed to convert worklog id to numeric value: "
<< worklog_id << "\n";
return -1;
}
atl::StatusOr<worklog::Log> status = store.LoadById(id.value());
if (!status.ok()) {
std::cerr << "Error: Failed to load worklog by id " << worklog_id << ". "
<< status.status().error_message() << "\n";
return -1;
}
auto log = status.ValueOrDie();
log.tags.erase(tag);
atl::Status updateStatus = store.Update(log);
if (!updateStatus.ok()) {
std::cerr << "Error: Failed to update log with id: " << worklog_id << ". "
<< updateStatus.error_message() << "\n";
return -1;
}
return 0;
}
int SubCommandTagsAdd(const worklog::CommandContext& ctx) {
const std::string& tag = ctx.args[3];
const std::string& worklog_id = ctx.args[4];
worklog::Storage store(ctx.config);
atl::Optional<int> id = NumberFromString(worklog_id);
if (!id) {
std::cerr << "Error: Failed to convert worklog id to numeric value: "
<< worklog_id << "\n";
return -1;
}
atl::StatusOr<worklog::Log> status = store.LoadById(id.value());
if (!status.ok()) {
std::cerr << "Error: Failed to load worklog by id " << worklog_id << ". "
<< status.status().error_message() << "\n";
return -1;
}
auto log = status.ValueOrDie();
log.tags.insert(tag);
atl::Status updateStatus = store.Update(log);
if (!updateStatus.ok()) {
std::cerr << "Error: Failed to update log with id: " << worklog_id << ". "
<< updateStatus.error_message() << "\n";
return -1;
}
return 0;
}
int CommandTags(const worklog::CommandContext& ctx) {
if (ctx.args.size() < 3) {
std::cerr << "Missing arguments. Please specify if you want to "
<< "add, remove or list tags.\n"
<< "Examples: \n\n"
<< ctx.args[0] << " tag add php <id> "
<< "adds php tag to worklog\n"
<< ctx.args[0] << " tag remove php <id> "
<< "removes php tag from worklog\n"
<< ctx.args[0] << " tag list "
<< "lists all available tags\n";
return -1;
}
if (ctx.args[2] == "list" || ctx.args[2] == "all") {
return SubCommandTagsListAll(ctx);
} else if (ctx.args[2] == "add") {
if (ctx.args.size() < 5) {
std::cerr << "Please specify a tag and worklog id.\n"
<< "Enter: " << ctx.args[0] << " tag for further help\n";
return -1;
}
return SubCommandTagsAdd(ctx);
} else if (ctx.args[2] == "del" || ctx.args[2] == "remove" ||
ctx.args[2] == "rm") {
if (ctx.args.size() < 5) {
std::cerr << "Please specify a tag and worklog id.\n"
<< "Enter: " << ctx.args[0] << " tag for further help\n";
return -1;
}
return SubCommandTagsRemove(ctx);
}
return 0;
auto index = IndexFromDir(ctx.config.logs_dir);
worklog::ApplyFilter(worklog::OnlyValidFilter(), &index);
}
int CommandYearly(const worklog::CommandContext& ctx) {
auto index = IndexFromDir(ctx.config.logs_dir);
worklog::ApplyFilter(worklog::OnlyValidFilter(), &index);
int prev_year = 0;
for (const auto& log : index) {
int year = std::stoi(atl::FormatTime(log.created_at, "%Y"));
if (prev_year != year) {
if (prev_year != 0) {
std::cout << "\n";
}
std::cout << atl::console::style::bold << atl::console::fg::cyan << year
<< ":" << atl::console::fg::reset << atl::console::style::reset
<< "\n";
prev_year = year;
}
PrintWorklog(log);
}
return 0;
}
int CommandSearch(const worklog::CommandContext& ctx) {
if (ctx.args.size() < 3) {
std::cerr << "Error: Please specify a search query or use the 'list' "
"command if you want to list all entries\n";
return 1;
}
// Concat the args so we have a string for the ParseFilter
int first_tag_idx = 2;
std::ostringstream text;
for (unsigned int i = first_tag_idx; i < ctx.args.size(); i++) {
text << ctx.args[i] << " ";
}
const worklog::Filter& filter = worklog::ParseFilter(text.str());
std::vector<worklog::Log> index = IndexFromDir(ctx.config.logs_dir);
worklog::ApplyFilter(worklog::OnlyValidFilter(), &index);
if (filter.tags.size() > 0 || filter.tags_negative.size() > 0) {
worklog::ApplyFilter(worklog::TagsFilter(filter), &index);
}
if (filter.subject.length() > 0) {
worklog::ApplyFilter(worklog::SubjectFilter(filter), &index);
}
for (const auto& log : index) {
PrintWorklog(log);
}
return 0;
}
// Forward declaring this function because we need it only in CommandRepeat but
// it is defined below:
int ParseAndExecute(const std::vector<std::string>& args);
int CommandRepeat(const worklog::CommandContext& ctx,
const worklog::CommandParser& cp) {
if (ctx.args.size() < 4) {
std::cerr << "Error: Invalid input for repetition. Format example: "
<< ctx.args[0] << " rep 1,3,4 view\n";
return -1;
}
const std::string selector_part = ctx.args[2];
const std::string rep_command = ctx.args[3];
std::vector<std::string> ids = atl::Split(selector_part, ",", true);
int batch_exit_code = 0;
for (const auto& id : ids) {
std::vector<std::string> args = {ctx.args[0], rep_command, id};
std::cerr << "Executed: " << atl::Join(args, " ")
<< ". Potential output:\n";
int exit_code = ParseAndExecute(args);
std::cerr << "... returned with exit code: " << exit_code << "\n";
// if it has an optional separator then we print it:
if (ctx.args.size() == 5) {
std::cout << ctx.args[4] << "\n";
}
if (exit_code != 0) {
// Overwriting the exit code only if the code is non zero.
batch_exit_code = exit_code;
}
}
return batch_exit_code;
}
worklog::CommandParser MakeCommandParser() {
using worklog::Command;
using worklog::CommandParser;
CommandParser cp;
cp.Add(Command("init", "initializes a worklog space", &CommandInitWorklog));
cp.Add(Command("new", "add a new work log",
MustBeInWorkspace(&CommandNewWorklog)));
cp.Add(Command("edit",
"edit a work log. An additional id parameter is required.",
MustBeInWorkspace(&CommandEditWorklog)));
cp.Add(Command("view",
"view a work log. An additional id parameter is required.",
MustBeInWorkspace(&CommandViewWorklog)));
cp.Add(Command("rm",
"removes a work log. An additional id parameter is required.",
MustBeInWorkspace(&CommandDeleteWorklog)));
cp.Add(Command("list", "lists all logs", MustBeInWorkspace(&CommandListAll)));
cp.Add(Command("broken", "lists all invalid logs",
MustBeInWorkspace(&CommandListBroken)));
cp.Add(Command("tag", "add, remove or list tags",
MustBeInWorkspace(&CommandTags)));
cp.Add(Command("search",
"search the work logs by a filter: tag:php -tag:javascript",
MustBeInWorkspace(&CommandSearch)));
// TODO(an): make it 'stats yearly':
cp.Add(Command("yearly", "shows a breakdown report by year",
MustBeInWorkspace(&CommandYearly)));
cp.Add(Command("rep",
"repeats a command. Example: ./tool rep 1,3,7 view "
"[\"separator string\"] (shows 1, 3 & 7 in a loop)",
[&cp](const worklog::CommandContext& ctx) -> int {
return CommandRepeat(ctx, cp);
}));
cp.Add(Command("help", "shows this help",
[&cp](const worklog::CommandContext& ctx) -> int {
cp.PrintHelp();
return 0;
}));
return cp;
}
int ParseAndExecute(const std::vector<std::string>& args) {
worklog::CommandParser cp = MakeCommandParser();
atl::StatusOr<worklog::Command::Action> parsing = cp.Parse(args);
if (!parsing.ok()) {
std::cout << parsing.status() << "\n\n";
cp.PrintHelp();
return -1;
}
worklog::CommandContext ctx;
ctx.args = args;
ctx.config = worklog::Config();
auto cmd = parsing.ValueOrDie();
return cmd(ctx);
}
int main(int argc, char** argv) {
if (getenv("EDITOR") == nullptr) {
std::cerr << "Error: Please set the environment variable "
<< "$EDITOR to your favourite editor (ie. vim). "
<< "(the value is currently empty!)\n";
return -1;
}
std::vector<std::string> args(argv, argv + argc);
return ParseAndExecute(args);
}