Skip to content

Commit 8049a17

Browse files
committed
bin/xbps-query: add format string support to --list-repos
1 parent 03e1b5d commit 8049a17

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

bin/xbps-query/main.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ usage(bool fail)
8080
exit(fail ? EXIT_FAILURE : EXIT_SUCCESS);
8181
}
8282

83+
static int
84+
repo_fmt(FILE *fp, const struct xbps_fmt *fmt, void *data)
85+
{
86+
struct xbps_repo *repo = data;
87+
if (strcmp(fmt->var, "url") == 0) {
88+
return xbps_fmt_print_string(fmt, repo->uri, 0, fp);
89+
} else if (strcmp(fmt->var, "signed") == 0) {
90+
return xbps_fmt_print_string(fmt, repo->is_signed ? "true" : "false", 0, fp);
91+
} else if (strcmp(fmt->var, "packages") == 0) {
92+
return xbps_fmt_print_number(fmt, xbps_dictionary_count(repo->idx), fp);
93+
}
94+
return 0;
95+
}
96+
8397
static void
8498
show_repo(struct xbps_repo *repo)
8599
{
@@ -108,8 +122,16 @@ show_repo(struct xbps_repo *repo)
108122
}
109123

110124
static int
111-
show_repos(struct xbps_handle *xhp)
125+
show_repos(struct xbps_handle *xhp, const char *format)
112126
{
127+
struct xbps_fmt *fmt = NULL;
128+
if (format) {
129+
fmt = xbps_fmt_parse(format);
130+
if (!fmt) {
131+
xbps_error_printf("failed to parse format: %s\n", strerror(errno));
132+
return 1;
133+
}
134+
}
113135
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
114136
const char *repouri = NULL;
115137
struct xbps_repo *repo;
@@ -119,9 +141,18 @@ show_repos(struct xbps_handle *xhp)
119141
printf("%5zd %s (RSA maybe-signed)\n", (ssize_t)-1, repouri);
120142
continue;
121143
}
122-
show_repo(repo);
144+
if (fmt) {
145+
int r = xbps_fmt(fmt, repo_fmt, repo, stdout);
146+
if (r < 0) {
147+
xbps_error_printf("failed to format repo: %s\n", strerror(-r));
148+
return 1;
149+
}
150+
} else {
151+
show_repo(repo);
152+
}
123153
xbps_repo_release(repo);
124154
}
155+
xbps_fmt_free(fmt);
125156
return 0;
126157
}
127158

@@ -354,7 +385,7 @@ main(int argc, char **argv)
354385
rv = list_pkgdb(&xh, filter_repolock, format ? format : "{pkgver}\n", json) < 0;
355386
break;
356387
case SHOW_REPOS:
357-
rv = show_repos(&xh);
388+
rv = show_repos(&xh, format);
358389
break;
359390
case SEARCH_FILE:
360391
rv = ownedby(&xh, pkg, repo_mode, regex);

bin/xbps-query/xbps-query.1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@ If a repository is not available the number of packages will be
181181
The
182182
.Fl v
183183
option can be used to show more detailed information of remote repositories.
184+
.Pp
185+
The following
186+
.Fl F , Fl -format
187+
variables can be used:
188+
.Bl -tag -compact -width Ic
189+
.It Ic url
190+
repository url.
191+
.It Ic signed
192+
.Sq true
193+
if the repository is signed or
194+
.Sq false
195+
otherwise.
196+
.It Ic packages
197+
The number of packages in the repository.
198+
.El
184199
.It Fl H, Fl -list-hold-pkgs
185200
List registered packages in the package database (pkgdb) that are on
186201
.Sy hold .

0 commit comments

Comments
 (0)