/
flatpak-builtins-remote-list.c
271 lines (232 loc) · 11 KB
/
flatpak-builtins-remote-list.c
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
/* vi:set et sw=2 sts=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e-s:
* Copyright © 2014 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http:https://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <[email protected]>
*/
#include "config.h"
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glib/gi18n.h>
#include "libglnx.h"
#include "flatpak-builtins.h"
#include "flatpak-builtins-utils.h"
#include "flatpak-utils-private.h"
#include "flatpak-table-printer.h"
static gboolean opt_show_details;
static gboolean opt_show_disabled;
static const char **opt_cols;
static GOptionEntry options[] = {
{ "show-details", 'd', 0, G_OPTION_ARG_NONE, &opt_show_details, N_("Show remote details"), NULL },
{ "show-disabled", 0, 0, G_OPTION_ARG_NONE, &opt_show_disabled, N_("Show disabled remotes"), NULL },
{ "columns", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_cols, N_("What information to show"), N_("FIELD,…") },
{ NULL }
};
static Column all_columns[] = {
{ "name", N_("Name"), N_("Show the name"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 1 },
{ "title", N_("Title"), N_("Show the title"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
{ "url", N_("URL"), N_("Show the URL"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
{ "collection", N_("Collection ID"), N_("Show the collection ID"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
{ "subset", N_("Subset"), N_("Show the subset"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
{ "filter", N_("Filter"), N_("Show filter file"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
{ "priority", N_("Priority"), N_("Show the priority"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
{ "options", N_("Options"), N_("Show options"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 1 },
{ "comment", N_("Comment"), N_("Show comment"), 0, FLATPAK_ELLIPSIZE_MODE_END, 1, 0 },
{ "description", N_("Description"), N_("Show description"), 0, FLATPAK_ELLIPSIZE_MODE_END, 1, 0 },
{ "homepage", N_("Homepage"), N_("Show homepage"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
{ "icon", N_("Icon"), N_("Show icon"), 0, FLATPAK_ELLIPSIZE_MODE_NONE, 1, 0 },
{ NULL }
};
static gboolean
list_remotes (GPtrArray *dirs, Column *columns, GCancellable *cancellable, GError **error)
{
g_autoptr(FlatpakTablePrinter) printer = NULL;
int i, j, k;
if (columns[0].name == NULL)
return TRUE;
printer = flatpak_table_printer_new ();
flatpak_table_printer_set_columns (printer, columns,
opt_cols == NULL && !opt_show_details);
for (j = 0; j < dirs->len; j++)
{
FlatpakDir *dir = g_ptr_array_index (dirs, j);
g_auto(GStrv) remotes = NULL;
remotes = flatpak_dir_list_remotes (dir, cancellable, error);
if (remotes == NULL)
return FALSE;
for (i = 0; remotes[i] != NULL; i++)
{
char *remote_name = remotes[i];
gboolean disabled;
disabled = flatpak_dir_get_remote_disabled (dir, remote_name);
if (disabled && !opt_show_disabled)
continue;
for (k = 0; columns[k].name; k++)
{
if (strcmp (columns[k].name, "name") == 0)
flatpak_table_printer_add_column (printer, remote_name);
else if (strcmp (columns[k].name, "title") == 0)
{
g_autofree char *title = flatpak_dir_get_remote_title (dir, remote_name);
if (title)
flatpak_table_printer_add_column (printer, title);
else
flatpak_table_printer_add_column (printer, "-");
}
else if (strcmp (columns[k].name, "subset") == 0)
{
g_autofree char *subset = flatpak_dir_get_remote_subset (dir, remote_name);
if (subset)
flatpak_table_printer_add_column (printer, subset);
else
flatpak_table_printer_add_column (printer, "-");
}
else if (strcmp (columns[k].name, "comment") == 0)
{
g_autofree char *comment = flatpak_dir_get_remote_comment (dir, remote_name);
if (comment)
flatpak_table_printer_add_column (printer, comment);
else
flatpak_table_printer_add_column (printer, "-");
}
else if (strcmp (columns[k].name, "description") == 0)
{
g_autofree char *description = flatpak_dir_get_remote_description (dir, remote_name);
if (description)
flatpak_table_printer_add_column (printer, description);
else
flatpak_table_printer_add_column (printer, "-");
}
else if (strcmp (columns[k].name, "filter") == 0)
{
g_autofree char *filter = flatpak_dir_get_remote_filter (dir, remote_name);
if (filter)
flatpak_table_printer_add_column (printer, filter);
else
flatpak_table_printer_add_column (printer, "-");
}
else if (strcmp (columns[k].name, "homepage") == 0)
{
g_autofree char *homepage = flatpak_dir_get_remote_homepage (dir, remote_name);
if (homepage)
flatpak_table_printer_add_column (printer, homepage);
else
flatpak_table_printer_add_column (printer, "-");
}
else if (strcmp (columns[k].name, "icon") == 0)
{
g_autofree char *icon = flatpak_dir_get_remote_icon (dir, remote_name);
if (icon)
flatpak_table_printer_add_column (printer, icon);
else
flatpak_table_printer_add_column (printer, "-");
}
else if (strcmp (columns[k].name, "url") == 0)
{
g_autofree char *remote_url = NULL;
if (ostree_repo_remote_get_url (flatpak_dir_get_repo (dir), remote_name, &remote_url, NULL))
flatpak_table_printer_add_column (printer, remote_url);
else
flatpak_table_printer_add_column (printer, "-");
}
else if (strcmp (columns[k].name, "collection") == 0)
{
g_autofree char *id = flatpak_dir_get_remote_collection_id (dir, remote_name);
if (id != NULL)
flatpak_table_printer_add_column (printer, id);
else
flatpak_table_printer_add_column (printer, "-");
}
else if (strcmp (columns[k].name, "priority") == 0)
{
int prio = flatpak_dir_get_remote_prio (dir, remote_name);
g_autofree char *prio_as_string = g_strdup_printf ("%d", prio);
flatpak_table_printer_add_column (printer, prio_as_string);
}
else if (strcmp (columns[k].name, "options") == 0)
{
gboolean gpg_verify = TRUE;
g_autofree char *filter = flatpak_dir_get_remote_filter (dir, remote_name);
flatpak_table_printer_add_column (printer, ""); /* Options */
if (dirs->len > 1)
{
g_autofree char *dir_id = flatpak_dir_get_name (dir);
flatpak_table_printer_append_with_comma (printer, dir_id);
}
if (disabled)
flatpak_table_printer_append_with_comma (printer, "disabled");
if (flatpak_dir_get_remote_oci (dir, remote_name))
flatpak_table_printer_append_with_comma (printer, "oci");
if (flatpak_dir_get_remote_noenumerate (dir, remote_name))
flatpak_table_printer_append_with_comma (printer, "no-enumerate");
if (!ostree_repo_remote_get_gpg_verify (flatpak_dir_get_repo (dir), remote_name,
&gpg_verify, error))
return FALSE; /* shouldn't happen unless repo config is modified out-of-band */
if (!gpg_verify)
flatpak_table_printer_append_with_comma (printer, "no-gpg-verify");
if (filter != NULL && *filter != 0)
flatpak_table_printer_append_with_comma (printer, "filtered");
}
}
flatpak_table_printer_finish_row (printer);
}
}
flatpak_table_printer_print (printer);
return TRUE;
}
gboolean
flatpak_builtin_remote_list (int argc, char **argv, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autofree char *col_help = NULL;
g_autofree Column *columns = NULL;
g_autoptr(GPtrArray) dirs = NULL;
context = g_option_context_new (_(" - List remote repositories"));
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
col_help = column_help (all_columns);
g_option_context_set_description (context, col_help);
if (!flatpak_option_context_parse (context, options, &argc, &argv,
FLATPAK_BUILTIN_FLAG_STANDARD_DIRS | FLATPAK_BUILTIN_FLAG_OPTIONAL_REPO, &dirs, cancellable, error))
return FALSE;
if (argc > 1)
return usage_error (context, _("Too many arguments"), error);
columns = handle_column_args (all_columns, opt_show_details, opt_cols, error);
if (columns == NULL)
return FALSE;
return list_remotes (dirs, columns, cancellable, error);
}
gboolean
flatpak_complete_remote_list (FlatpakCompletion *completion)
{
g_autoptr(GOptionContext) context = NULL;
context = g_option_context_new ("");
if (!flatpak_option_context_parse (context, options, &completion->argc, &completion->argv,
FLATPAK_BUILTIN_FLAG_STANDARD_DIRS, NULL, NULL, NULL))
return FALSE;
switch (completion->argc)
{
case 0:
case 1:
flatpak_complete_options (completion, global_entries);
flatpak_complete_options (completion, options);
flatpak_complete_options (completion, user_entries);
flatpak_complete_columns (completion, all_columns);
break;
}
return TRUE;
}