-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsnippets.c
More file actions
291 lines (248 loc) · 7.79 KB
/
snippets.c
File metadata and controls
291 lines (248 loc) · 7.79 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
/*
* 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>
#include <gcli/cmd/cmd.h>
#include <gcli/cmd/table.h>
#include <gcli/gitlab/snippets.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
static void
usage(void)
{
fprintf(stderr, "usage: gcli snippets [-n number] [-sl]\n");
fprintf(stderr, " gcli snippets delete id\n");
fprintf(stderr, " gcli snippets get id\n");
fprintf(stderr, "OPTIONS:\n");
fprintf(stderr, " -l Print a long list instead of a short table\n");
fprintf(stderr, " -n number Number of snippets to fetch\n");
fprintf(stderr, " -s Sort the output in reverse order\n");
fprintf(stderr, " -u user User for whom to list gists\n");
fprintf(stderr, "\n");
version();
copyright();
}
static void
gcli_print_snippet(enum gcli_output_flags const flags,
struct gcli_gitlab_snippet const *const it)
{
gcli_dict dict;
(void) flags;
dict = gcli_dict_begin();
gcli_dict_add(dict, "ID", 0, 0, "%"PRIid, it->id);
gcli_dict_add_string(dict, "TITLE", 0, 0, it->title);
gcli_dict_add_string(dict, "AUTHOR", 0, 0, it->author);
gcli_dict_add_string(dict, "FILE", 0, 0, it->filename);
gcli_dict_add_string(dict, "DATE", 0, 0, it->date);
gcli_dict_add_string(dict, "VSBLTY", 0, 0, it->visibility);
gcli_dict_add_string(dict, "URL", 0, 0, it->raw_url);
gcli_dict_end(dict);
}
static void
gcli_print_snippets_long(enum gcli_output_flags const flags,
struct gcli_gitlab_snippet_list const *const list, int const max)
{
int n;
/* Determine number of items to print */
if (max < 0 || (size_t)(max) > list->snippets_size)
n = list->snippets_size;
else
n = max;
if (flags & OUTPUT_SORTED) {
for (int i = 0; i < n; ++i)
gcli_print_snippet(flags, &list->snippets[n-i-1]);
} else {
for (int i = 0; i < n; ++i)
gcli_print_snippet(flags, &list->snippets[i]);
}
}
static void
gcli_print_snippets_short(enum gcli_output_flags const flags,
struct gcli_gitlab_snippet_list const *const list,
int const max)
{
int n;
gcli_tbl table;
struct gcli_tblcoldef cols[] = {
{ .name = "ID", .type = GCLI_TBLCOLTYPE_ID, .flags = GCLI_TBLCOL_JUSTIFYR },
{ .name = "DATE", .type = GCLI_TBLCOLTYPE_STRING, .flags = 0 },
{ .name = "VISIBILITY", .type = GCLI_TBLCOLTYPE_STRING, .flags = 0 },
{ .name = "AUTHOR", .type = GCLI_TBLCOLTYPE_STRING, .flags = 0 },
{ .name = "TITLE", .type = GCLI_TBLCOLTYPE_STRING, .flags = 0 },
};
/* Determine number of items to print */
if (max < 0 || (size_t)(max) > list->snippets_size)
n = list->snippets_size;
else
n = max;
/* Fill table */
table = gcli_tbl_begin(cols, ARRAY_SIZE(cols));
if (!table)
errx(1, "gcli: error: could not init table");
if (flags & OUTPUT_SORTED) {
for (int i = 0; i < n; ++i)
gcli_tbl_add_row(table,
list->snippets[n-i-1].id,
list->snippets[n-i-1].date,
list->snippets[n-i-1].visibility,
list->snippets[n-i-1].author,
list->snippets[n-i-1].title);
} else {
for (int i = 0; i < n; ++i)
gcli_tbl_add_row(table,
list->snippets[i].id,
list->snippets[i].date,
list->snippets[i].visibility,
list->snippets[i].author,
list->snippets[i].title);
}
gcli_tbl_end(table);
}
void
gcli_snippets_print(enum gcli_output_flags const flags,
struct gcli_gitlab_snippet_list const *const list, int const max)
{
if (list->snippets_size == 0) {
puts("No Snippets");
return;
}
if (flags & OUTPUT_LONG)
gcli_print_snippets_long(flags, list, max);
else
gcli_print_snippets_short(flags, list, max);
}
static int
subcommand_snippet_get(int argc, char *argv[])
{
argc -= 1;
argv += 1;
if (!argc) {
fprintf(stderr, "gcli: error: expected ID of snippet to fetch\n");
usage();
return EXIT_FAILURE;
}
char *snippet_id = shift(&argc, &argv);
if (argc) {
fprintf(stderr, "gcli: error: stray arguments\n");
usage();
return EXIT_FAILURE;
}
if (gcli_snippet_get(g_clictx, snippet_id, stdout) < 0)
errx(1, "gcli: error: failed to fetch snippet contents: %s",
gcli_get_error(g_clictx));
return EXIT_SUCCESS;
}
static int
subcommand_snippet_delete(int argc, char *argv[])
{
argc -= 1;
argv += 1;
if (!argc) {
fprintf(stderr, "gcli: error: expected ID of snippet to delete\n");
usage();
return EXIT_FAILURE;
}
char *snippet_id = shift(&argc, &argv);
if (argc) {
fprintf(stderr, "gcli: error: trailing options\n");
usage();
return EXIT_FAILURE;
}
if (gcli_snippet_delete(g_clictx, snippet_id) < 0)
errx(1, "gcli: error: failed to delete snippet: %s",
gcli_get_error(g_clictx));
return EXIT_SUCCESS;
}
static struct snippet_subcommand {
const char *name;
int (*fn)(int argc, char *argv[]);
} snippet_subcommands[] = {
{ .name = "get", .fn = subcommand_snippet_get },
{ .name = "delete", .fn = subcommand_snippet_delete },
};
int
subcommand_snippets(int argc, char *argv[])
{
int ch;
struct gcli_gitlab_snippet_list list = {0};
int count = 30;
enum gcli_output_flags flags = 0;
for (size_t i = 0; i < ARRAY_SIZE(snippet_subcommands); ++i) {
if (argc > 1 && strcmp(argv[1], snippet_subcommands[i].name) == 0) {
argc -= 1;
argv += 1;
return snippet_subcommands[i].fn(argc, argv);
}
}
const struct option options[] = {
{ .name = "count",
.has_arg = required_argument,
.flag = NULL,
.val = 'n' },
{ .name = "sorted",
.has_arg = no_argument,
.flag = NULL,
.val = 's' },
{ .name = "long",
.has_arg = no_argument,
.flag = NULL,
.val = 'l' },
{0},
};
while ((ch = getopt_long(argc, argv, "sn:l", options, NULL)) != -1) {
switch (ch) {
case 'n': {
char *endptr = NULL;
count = strtol(optarg, &endptr, 10);
if (endptr != (optarg + strlen(optarg)))
err(1, "gcli: error: cannot parse snippets count");
if (count == 0)
errx(1, "gcli: error: snippets count must not be zero");
} break;
case 's':
flags |= OUTPUT_SORTED;
break;
case 'l':
flags |= OUTPUT_LONG;
break;
case '?':
default:
usage();
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (gcli_snippets_get(g_clictx, count, &list) < 0)
errx(1, "gcli: error: failed to fetch snippets: %s",
gcli_get_error(g_clictx));
gcli_snippets_print(flags, &list, count);
gcli_snippets_free(&list);
return EXIT_SUCCESS;
}