Skip to content

Commit e53c837

Browse files
committed
bin/xbps-query: add --json flag as alternative to --format
1 parent c139c5f commit e53c837

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

bin/xbps-query/defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int ownedby(struct xbps_handle *, const char *, bool, bool);
5555
int list_manual_pkgs(struct xbps_handle *, xbps_object_t, const char *, void *, bool *);
5656
int list_orphans(struct xbps_handle *, const char *);
5757
int list_pkgs_pkgdb(struct xbps_handle *);
58-
int list_pkgdb(struct xbps_handle *, int (*filter)(xbps_object_t), const char *format);
58+
int list_pkgdb(struct xbps_handle *, int (*filter)(xbps_object_t), const char *format, int json);
5959

6060
int repo_list(struct xbps_handle *);
6161

bin/xbps-query/list.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "defs.h"
3838
#include "xbps.h"
39+
#include "xbps/json.h"
3940

4041
struct length_max_cb {
4142
const char *key;
@@ -135,6 +136,7 @@ list_pkgs_pkgdb(struct xbps_handle *xhp)
135136

136137
struct list_pkgdb_cb {
137138
struct xbps_fmt *fmt;
139+
struct xbps_json_printer *json;
138140
int (*filter)(xbps_object_t obj);
139141
};
140142

@@ -153,23 +155,34 @@ list_pkgdb_cb(struct xbps_handle *xhp UNUSED, xbps_object_t obj,
153155
return 0;
154156
}
155157

156-
r = xbps_fmt_dictionary(ctx->fmt, obj, stdout);
157-
if (r < 0)
158-
return r;
159-
return 0;
158+
if (ctx->fmt) {
159+
r = xbps_fmt_dictionary(ctx->fmt, obj, stdout);
160+
} else if (ctx->json) {
161+
r = xbps_json_print_xbps_object(ctx->json, obj);
162+
fprintf(ctx->json->file, "\n");
163+
} else {
164+
r = -ENOTSUP;
165+
}
166+
return r;
160167
}
161168

162169
int
163-
list_pkgdb(struct xbps_handle *xhp, int (*filter)(xbps_object_t), const char *format)
170+
list_pkgdb(struct xbps_handle *xhp, int (*filter)(xbps_object_t), const char *format, int json)
164171
{
165172
struct list_pkgdb_cb ctx = {.filter = filter};
173+
struct xbps_json_printer pr = {0};
166174
int r;
167-
168-
ctx.fmt = xbps_fmt_parse(format);
169-
if (!ctx.fmt) {
170-
r = -errno;
171-
xbps_error_printf("failed to parse format: %s\n", strerror(-r));
172-
return r;
175+
if (json > 0) {
176+
pr.indent = (json-1) * 2;
177+
pr.file = stdout;
178+
ctx.json = &pr;
179+
} else if (format) {
180+
ctx.fmt = xbps_fmt_parse(format);
181+
if (!ctx.fmt) {
182+
r = -errno;
183+
xbps_error_printf("failed to parse format: %s\n", strerror(-r));
184+
return r;
185+
}
173186
}
174187
r = xbps_pkgdb_foreach_cb(xhp, list_pkgdb_cb, &ctx);
175188
xbps_fmt_free(ctx.fmt);

bin/xbps-query/main.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include <xbps.h>
3434

3535
#include "defs.h"
36+
#include "xbps.h"
37+
#include "xbps/json.h"
3638

3739
static void __attribute__((noreturn))
3840
usage(bool fail)
@@ -46,6 +48,7 @@ usage(bool fail)
4648
" -F, --format <format> Format for list output\n"
4749
" -h, --help Show usage\n"
4850
" -i, --ignore-conf-repos Ignore repositories defined in xbps.d\n"
51+
" -J, --json Print output as json\n"
4952
" -M, --memory-sync Remote repository data is fetched and stored\n"
5053
" in memory, ignoring on-disk repodata archives\n"
5154
" -p, --property PROP[,...] Show properties for PKGNAME\n"
@@ -100,13 +103,14 @@ filter_repolock(xbps_object_t obj)
100103
int
101104
main(int argc, char **argv)
102105
{
103-
const char *shortopts = "C:c:dF:f:hHiLlMmOo:p:Rr:s:S:VvX:x:";
106+
const char *shortopts = "C:c:dF:f:hHiJLlMmOo:p:Rr:s:S:VvX:x:";
104107
const struct option longopts[] = {
105108
{ "config", required_argument, NULL, 'C' },
106109
{ "cachedir", required_argument, NULL, 'c' },
107110
{ "debug", no_argument, NULL, 'd' },
108111
{ "help", no_argument, NULL, 'h' },
109112
{ "ignore-conf-repos", no_argument, NULL, 'i' },
113+
{ "json", no_argument, NULL, 'J' },
110114
{ "list-repos", no_argument, NULL, 'L' },
111115
{ "list-pkgs", no_argument, NULL, 'l' },
112116
{ "list-hold-pkgs", no_argument, NULL, 'H' },
@@ -137,6 +141,7 @@ main(int argc, char **argv)
137141
bool list_pkgs, list_repos, orphans, own, list_repolock;
138142
bool list_manual, list_hold, show_prop, show_files, show_deps, show_rdeps;
139143
bool show, pkg_search, regex, repo_mode, opmode, fulldeptree;
144+
int json = 0;
140145

141146
rootdir = cachedir = confdir = props = pkg = catfile = format = NULL;
142147
flags = rv = c = 0;
@@ -165,6 +170,9 @@ main(int argc, char **argv)
165170
case 'F':
166171
format = optarg;
167172
break;
173+
case 'J':
174+
json++;
175+
break;
168176
case 'H':
169177
list_hold = opmode = true;
170178
break;
@@ -286,20 +294,21 @@ main(int argc, char **argv)
286294
rv = repo_list(&xh);
287295

288296
} else if (list_hold) {
289-
rv = list_pkgdb(&xh, filter_hold, format ? format : "{pkgver}\n") < 0;
297+
rv = list_pkgdb(&xh, filter_hold, format ? format : "{pkgver}\n", json) < 0;
290298

291299
} else if (list_repolock) {
292-
rv = list_pkgdb(&xh, filter_repolock, format ? format : "{pkgver}\n") < 0;
300+
rv = list_pkgdb(&xh, filter_repolock, format ? format : "{pkgver}\n", json) < 0;
293301

294302
} else if (list_manual) {
295-
rv = list_pkgdb(&xh, filter_manual, format ? format : "{pkgver}\n") < 0;
303+
rv = list_pkgdb(&xh, filter_manual, format ? format : "{pkgver}\n", json) < 0;
296304

297305
} else if (list_pkgs) {
298306
/* list available pkgs */
299-
if (format)
300-
rv = list_pkgdb(&xh, NULL, format);
301-
else
307+
if (format || json > 0) {
308+
rv = list_pkgdb(&xh, NULL, format, json);
309+
} else {
302310
rv = list_pkgs_pkgdb(&xh);
311+
}
303312

304313
} else if (orphans) {
305314
/* list pkg orphans */

bin/xbps-query/xbps-query.1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Ignore repositories defined in configuration files.
100100
Only repositories specified in the command line via
101101
.Ar --repository
102102
will be used.
103+
.It Fl J, Fl -json
104+
Print output as json.
103105
.It Fl M, Fl -memory-sync
104106
For remote repositories, the data is fetched and stored in memory for the current
105107
operation.

0 commit comments

Comments
 (0)