-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
104 lines (93 loc) · 3.15 KB
/
Copy pathmain.c
File metadata and controls
104 lines (93 loc) · 3.15 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gitward.h"
static void usage(void)
{
puts("usage: gitward <command> [args]");
puts("");
puts("commands:");
puts(" scan [path] scan a local git repository (default: .)");
puts(" inspect [path] inspect and summarize findings (default: .)");
puts(" search <path> <query> search clues in repo");
puts(" save <path> [outfile] save results to file");
puts(" version print version");
puts(" help print this help");
puts("");
puts("aliases: s=scan i=inspect f=search o=save");
}
int main(int argc, char **argv)
{
if (argc < 2) {
usage();
return 1;
}
const char *cmd = argv[1];
if (strcmp(cmd, "version") == 0) {
printf("gitward %s\n", GITWARD_VERSION);
return 0;
}
if (strcmp(cmd, "help") == 0 || strcmp(cmd, "-h") == 0 ||
strcmp(cmd, "--help") == 0) {
usage();
return 0;
}
if (strcmp(cmd, "scan") == 0 || strcmp(cmd, "s") == 0) {
const char *path = (argc >= 3) ? argv[2] : ".";
ClueList *cl = cluelist_new();
if (!cl) { fprintf(stderr, "error: out of memory\n"); return 1; }
int ret = 0;
if (scan_repo(path, cl) != 0) { ret = 1; goto done_scan; }
inspect_repo(path, cl);
done_scan:
cluelist_free(cl);
return ret;
}
if (strcmp(cmd, "inspect") == 0 || strcmp(cmd, "i") == 0) {
const char *path = (argc >= 3) ? argv[2] : ".";
ClueList *cl = cluelist_new();
if (!cl) { fprintf(stderr, "error: out of memory\n"); return 1; }
int ret = 0;
if (scan_repo(path, cl) != 0) { ret = 1; goto done_inspect; }
inspect_repo(path, cl);
done_inspect:
cluelist_free(cl);
return ret;
}
if (strcmp(cmd, "search") == 0 || strcmp(cmd, "f") == 0) {
if (argc < 4) {
fprintf(stderr, "error: search requires <path> and <query>\n");
return 1;
}
const char *path = argv[2];
const char *query = argv[3];
ClueList *cl = cluelist_new();
if (!cl) { fprintf(stderr, "error: out of memory\n"); return 1; }
int ret = 0;
if (scan_repo(path, cl) != 0) { ret = 1; goto done_search; }
search_clues(cl, query);
done_search:
cluelist_free(cl);
return ret;
}
if (strcmp(cmd, "save") == 0 || strcmp(cmd, "o") == 0) {
if (argc < 3) {
fprintf(stderr, "error: save requires <path>\n");
return 1;
}
const char *path = argv[2];
const char *outfile = (argc >= 4) ? argv[3] : "gitward.out";
ClueList *cl = cluelist_new();
if (!cl) { fprintf(stderr, "error: out of memory\n"); return 1; }
int ret = 0;
if (scan_repo(path, cl) != 0) { ret = 1; goto done_save; }
if (save_clues(cl, outfile) != 0) { ret = 1; goto done_save; }
printf("saved: %s\n", outfile);
done_save:
cluelist_free(cl);
return ret;
}
fprintf(stderr, "error: unknown command: %s\n", cmd);
usage();
return 1;
}