-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgcli.c
More file actions
382 lines (327 loc) · 10.6 KB
/
gcli.c
File metadata and controls
382 lines (327 loc) · 10.6 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
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
/*
* Copyright Nico Sonack <nsonack@herrhotzenplotz.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#ifdef HAVE_GETOPT_h
#include <getopt.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <gcli/cmd/attachments.h>
#include <gcli/cmd/ci.h>
#include <gcli/cmd/cmd.h>
#include <gcli/cmd/cmdconfig.h>
#include <gcli/cmd/comment.h>
#include <gcli/cmd/config.h>
#include <gcli/cmd/forks.h>
#include <gcli/cmd/gists.h>
#include <gcli/cmd/issues.h>
#include <gcli/cmd/labels.h>
#include <gcli/cmd/milestones.h>
#include <gcli/cmd/pipelines.h>
#include <gcli/cmd/pulls.h>
#include <gcli/cmd/releases.h>
#include <gcli/cmd/repos.h>
#include <gcli/cmd/snippets.h>
#include <gcli/cmd/status.h>
#include <gcli/port/string.h>
static void usage(void);
static int
subcommand_version(int argc, char *argv[])
{
(void) argc;
(void) argv;
longversion();
copyright();
return EXIT_SUCCESS;
}
static struct subcommand {
char const *cmd_name;
char const *docstring;
int (*fn)(int, char **);
} default_subcommands[] = {
{ .cmd_name = "ci",
.fn = subcommand_ci,
.docstring = "GitHub CI status info" },
{ .cmd_name = "comment",
.fn = subcommand_comment,
.docstring = "Comment under issues and PRs" },
{ .cmd_name = "config",
.fn = subcommand_config,
.docstring = "Configure forges" },
{ .cmd_name = "forks",
.fn = subcommand_forks,
.docstring = "Create, delete and list repository forks" },
{ .cmd_name = "gists",
.fn = subcommand_gists,
.docstring = "Create, fetch and list GitHub Gists" },
{ .cmd_name = "issues",
.fn = subcommand_issues,
.docstring = "Manage issues" },
{ .cmd_name = "labels",
.fn = subcommand_labels,
.docstring = "Manage issue and PR labels" },
{ .cmd_name = "milestones",
.fn = subcommand_milestones,
.docstring = "Milestone handling" },
{ .cmd_name = "pipelines",
.fn = subcommand_pipelines,
.docstring = "GitLab CI management" },
{ .cmd_name = "pulls",
.fn = subcommand_pulls,
.docstring = "Create, view and manage PRs" },
{ .cmd_name = "releases",
.fn = subcommand_releases,
.docstring = "Manage releases of repositories" },
{ .cmd_name = "repos",
.fn = subcommand_repos,
.docstring = "Remote Repository management" },
{ .cmd_name = "snippets",
.fn = subcommand_snippets,
.docstring = "Fetch and list GitLab snippets" },
{ .cmd_name = "status",
.fn = subcommand_status,
.docstring = "General user status and notifications" },
{ .cmd_name = "attachments",
.fn = subcommand_attachments,
.docstring = "Bugzilla Attachments management" },
{ .cmd_name = "api",
.fn = subcommand_api,
.docstring = "Fetch plain JSON info from an API (for debugging purposes)" },
{ .cmd_name = "version",
.fn = subcommand_version,
.docstring = "Print version" },
};
static struct subcommand *subcommands = NULL;
static size_t subcommands_size = 0;
static void
usage(void)
{
fprintf(stderr, "usage: gcli [options] subcommand\n\n");
fprintf(stderr, "OPTIONS:\n");
fprintf(stderr, " -a account Use the configured account instead of inferring it\n");
fprintf(stderr, " -r remote Infer account from the given git remote\n");
fprintf(stderr, " -t type Force the account type:\n");
fprintf(stderr, " - github (default: github.com)\n");
fprintf(stderr, " - gitlab (default: gitlab.com)\n");
fprintf(stderr, " - gitea (default: codeberg.org)\n");
fprintf(stderr, " - bugzilla (default: bugs.freebsd.org)\n");
fprintf(stderr, " -c Force colour and text formatting\n");
fprintf(stderr, " --no-spinner Disable the animated spinner\n");
fprintf(stderr, " --no-markdown Disable markdown rendering\n");
fprintf(stderr, " -q Be quiet. (Not implemented yet)\n");
fprintf(stderr, " -v Be verbose\n");
fprintf(stderr, " -V | --version Print version and exit\n\n");
fprintf(stderr, "SUBCOMMANDS:\n");
for (size_t i = 0; i < subcommands_size; ++i) {
fprintf(stderr,
" %-15.15s %s\n",
subcommands[i].cmd_name,
subcommands[i].docstring);
}
fprintf(stderr, "\n");
version();
copyright();
}
/** The CMD global gcli context */
struct gcli_ctx *g_clictx = NULL;
static void
gcli_progress_func(bool const done)
{
char spinner[] = "|/-\\";
static size_t const spinner_elems = sizeof(spinner) / sizeof(*spinner);
static int spinner_idx = 0;
static int have_checked_stderr = 0, stderr_is_tty = 1;
/* Check if stderr is a tty */
if (!have_checked_stderr) {
stderr_is_tty = isatty(STDERR_FILENO);
have_checked_stderr = 1;
}
if (!stderr_is_tty)
return;
/* Clear out the line when done */
if (done) {
fprintf(stderr, " \r");
} else {
fprintf(stderr, "Wait... %c\r", spinner[spinner_idx]);
spinner_idx = (spinner_idx + 1) % (spinner_elems - 1);
}
}
/* Abbreviated form matching:
*
* - we presort the subcommands array alphabetised
* - then we can simply match by prefix */
static int
subcommand_compare(void const *s1, void const *s2)
{
struct subcommand const *sc1 = s1;
struct subcommand const *sc2 = s2;
return strcmp(sc1->cmd_name, sc2->cmd_name);
}
static void
presort_subcommands(void)
{
qsort(subcommands, subcommands_size, sizeof(*subcommands),
subcommand_compare);
}
static bool
is_unique_match(size_t const idx, char const *const name, size_t const name_len)
{
/* Last match is always unique */
if (idx + 1 == subcommands_size)
return true;
for (size_t i = idx + 1; i < subcommands_size; ++i) {
if (strncmp(name, subcommands[i].cmd_name, name_len))
return true; /* doesn't match. meaning this one is unique. */
else
break; /* we found a duplicate prefix. */
}
fprintf(stderr, "gcli: error: %s: subcommand is ambiguous. could be one of:\n", name);
/* List until either the end or until we don't match any more prefixes */
for (size_t i = idx; i < subcommands_size; ++i) {
if (strncmp(name, subcommands[i].cmd_name, name_len))
break;
fprintf(stderr, " - %-13.13s %s\n", subcommands[i].cmd_name,
subcommands[i].docstring);
}
fprintf(stderr, "\n");
return false;
}
enum {
LOOKUP_NOSUCHCMD = 1,
LOOKUP_AMBIGUOUS,
};
static struct subcommand const *
find_subcommand(char const *const name, int *error)
{
size_t const name_len = strlen(name);
for (size_t i = 0; i < subcommands_size; ++i) {
if (strncmp(subcommands[i].cmd_name, name, name_len) == 0) {
/* At least the prefix matches. Check that it is a unique match. */
if (!is_unique_match(i, name, name_len)) {
if (error)
*error = LOOKUP_AMBIGUOUS;
return NULL;
}
return subcommands + i;
}
}
/* no match */
fprintf(stderr, "gcli: error: %s: no such subcommand\n", name);
if (error)
*error = LOOKUP_NOSUCHCMD;
return NULL;
}
static void
add_subcommand_alias(char const *alias_name, char const *alias_for)
{
char *docstring;
struct subcommand const *old_sc;
struct subcommand *new_sc;
int (*old_fn)(int, char **);
old_sc = find_subcommand(alias_for, NULL);
if (old_sc == NULL) {
fprintf(stderr, "gcli: note: this error occured while defining the alias »%s«\n",
alias_name);
exit(EXIT_FAILURE);
}
old_fn = old_sc->fn;
docstring = gcli_asprintf("Alias for %s", alias_for);
subcommands = realloc(subcommands, (subcommands_size + 1) * sizeof(*subcommands));
/* Copy in data */
new_sc = &subcommands[subcommands_size++];
new_sc->cmd_name = alias_name;
new_sc->fn = old_fn;
new_sc->docstring = docstring;
}
static void
install_aliases(void)
{
struct gcli_config_entries const *entries;
struct gcli_config_entry const *entry;
add_subcommand_alias("pr", "pulls");
/* Search for aliases in the user config file */
entries = gcli_config_get_section_entries(g_clictx, "aliases");
if (!entries)
return;
TAILQ_FOREACH(entry, entries, next)
add_subcommand_alias(entry->key, entry->value);
}
static void
setup_subcommand_table(void)
{
subcommands = calloc(ARRAY_SIZE(default_subcommands), sizeof(*subcommands));
memcpy(subcommands, default_subcommands, sizeof(default_subcommands));
subcommands_size = ARRAY_SIZE(default_subcommands);
}
int
main(int argc, char *argv[])
{
char const *errmsg;
struct subcommand const *sc;
int error_reason;
errmsg = gcli_init(&g_clictx, gcli_config_get_forge_type,
gcli_config_get_token, gcli_config_get_apibase);
if (errmsg)
errx(1, "gcli: error: %s", errmsg);
if (gcli_config_init_ctx(g_clictx) < 0)
errx(1, "gcli: error: failed to init context: %s", gcli_get_error(g_clictx));
/* Parse first arguments, must be done here because the aliases are
* read from the config file. However, the config file may be
* changed using command line options. */
if (gcli_config_parse_args(g_clictx, &argc, &argv)) {
usage();
return EXIT_FAILURE;
}
/* Initial setup */
setup_subcommand_table();
/* Install subcommand aliases into subcommand table */
install_aliases();
/* Sorts the subcommands array alphabatically */
presort_subcommands();
/* Make sure we have a subcommand */
if (argc == 0) {
fprintf(stderr, "gcli: error: missing subcommand\n");
usage();
return EXIT_FAILURE;
}
/* Search for the subcommand */
sc = find_subcommand(argv[0], &error_reason);
if (sc == NULL) {
if (error_reason == LOOKUP_AMBIGUOUS)
version();
else
usage();
return EXIT_FAILURE;
}
if(gcli_config_display_progress_spinner(g_clictx))
gcli_set_progress_func(g_clictx, gcli_progress_func);
return sc->fn(argc, argv);
}