Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add --raw-format to info/search
Baptiste Daroussin committed 11 years ago
commit 1cd0bd1e901fc96f0f57eab387f9f45d2083796d
parent 3319f10
8 files changed +67 -6
modified docs/pkg-info.8
@@ -49,6 +49,7 @@
.Op Cm --{pkg-message,dependencies,full,comment,list-files}
.Op Cm --{quiet,prefix,raw,required-by,size}
.Cm --file Ar pkg-file
+
.Op Fl -raw-format Ar format
.Sh DESCRIPTION
.Nm
is used for displaying information for packages.
@@ -73,6 +74,9 @@ This is the default.
.It Fl R , Cm --raw
Display the full manifest (raw) for the packages matching
.Ar pkg-name .
+
.It Fl -raw-format Ar format
+
Allow to chose the output format for the raw output, format can be:
+
json, json-conpact, yaml (default).
.It Fl e , Cm --exists
If
.Ar pkg-name
modified docs/pkg-search.8
@@ -49,6 +49,7 @@
.Op Cm --{quiet,raw,size,no-repo-update}
.Op Cm --repository Ar reponame
.Op Cm --{case-sensitive,exact,glob,case-insensitive,regex}
+
.Op Fl -raw-format Ar format
.Ar pattern
.Sh DESCRIPTION
.Nm
@@ -178,7 +179,7 @@ status from
By default all repository catalogues marked
.Dq active
are searched.
-
.It Fl R , Cm --raw
+
.It Fl R , Fl -raw
Display the full manifest (raw) from the matching packages.
.It Fl S Ar search , Cm --search Ar search
Specify the field to search the repository catalogue on.
@@ -194,6 +195,9 @@ field.
See the
.Qq Sx Search and Label Options
sections for more details.
+
.It Fl -raw-format Ar format
+
Allow to chose the output format for the raw output, format can be:
+
json, json-conpact, yaml (default).
.It Fl s , Cm --size
Display the installed size of matched packages.
Equivalent to
modified libpkg/pkg.h.in
@@ -857,6 +857,7 @@ void pkg_manifest_parser_free(struct pkg_manifest_parser *p);
#define PKG_MANIFEST_EMIT_COMPACT 0x1
#define PKG_MANIFEST_EMIT_NOFILES (0x1 << 1)
#define PKG_MANIFEST_EMIT_PRETTY (0x1 << 2)
+
#define PKG_MANIFEST_EMIT_JSON (0x1 << 3)

/**
 * Emit a manifest according to the attributes of pkg.
modified libpkg/pkg_manifest.c
@@ -1127,6 +1127,8 @@ emit_manifest(struct pkg *pkg, struct sbuf **out, short flags)

	if ((flags & PKG_MANIFEST_EMIT_PRETTY) == PKG_MANIFEST_EMIT_PRETTY)
		ucl_object_emit_sbuf(top, UCL_EMIT_YAML, out);
+
	else if ((flags & PKG_MANIFEST_EMIT_JSON) == PKG_MANIFEST_EMIT_JSON)
+
		ucl_object_emit_sbuf(top, UCL_EMIT_JSON, out);
	else
		ucl_object_emit_sbuf(top, UCL_EMIT_JSON_COMPACT, out);

modified src/info.c
@@ -125,6 +125,7 @@ exec_info(int argc, char **argv)
		{ "raw",		no_argument,		NULL,	'R' },
		{ "size",		no_argument,		NULL,	's' },
		{ "regex",		no_argument,		NULL,	'x' },
+
		{ "raw-format",		required_argument,	NULL, 	1   },
		{ NULL,			0,			NULL,	0   },
	};

@@ -204,6 +205,18 @@ exec_info(int argc, char **argv)
		case 'x':
			match = MATCH_REGEX;
			break;
+
		case 1:
+
			if (strcasecmp(optarg, "json") == 0)
+
			       opt |= INFO_RAW_JSON;
+
			else if (strcasecmp(optarg, "json-compact") == 0)
+
				opt |= INFO_RAW_JSON_COMPACT;
+
			else if (strcasecmp(optarg, "yaml") == 0)
+
				opt |= INFO_RAW_YAML;
+
			else
+
				errx(EX_USAGE, "Invalid format '%s' for the "
+
				    "raw output, expecting json, json-compat "
+
				    "or yaml", optarg);
+
			break;
		default:
			usage_info();
			return(EX_USAGE);
@@ -264,6 +277,11 @@ exec_info(int argc, char **argv)
		if (opt == INFO_TAG_NAMEVER)
			opt |= INFO_FULL;
		pkg_manifest_keys_new(&keys);
+
		if (opt & INFO_RAW) {
+
			if ((opt & (INFO_RAW_JSON|INFO_RAW_JSON_COMPACT)) == 0)
+
				opt |= INFO_RAW_YAML;
+
		}
+

		if ((opt & (INFO_RAW | INFO_FILES |
				INFO_DIRS)) == 0)
			open_flags = PKG_OPEN_MANIFEST_COMPACT;
modified src/pkgcli.h
@@ -228,7 +228,10 @@ void usage_config(void);
#define INFO_TAG_NAMEVER	(1LL<<62)

/* Output YAML format */
-
#define INFO_RAW	(-1LL<<63)
+
#define INFO_RAW		(-1LL<<63)
+
#define INFO_RAW_YAML		(-1LL<<62)
+
#define INFO_RAW_JSON		(-1LL<<61)
+
#define INFO_RAW_JSON_COMPACT	(-1LL<<60)

/* Everything in the 'full' package output */
#define INFO_FULL	(INFO_NAME|INFO_VERSION|INFO_INSTALLED|INFO_ORIGIN| \
modified src/search.c
@@ -277,6 +277,7 @@ exec_search(int argc, char **argv)
		{ "size",		no_argument,		NULL,	's' },
		{ "no-repo-update",	no_argument,		NULL,	'U' },
		{ "regex",		no_argument,		NULL,	'x' },
+
		{ "raw-format",		required_argument,	NULL, 	1   },
		{ NULL,			0,			NULL,	0   },
	};

@@ -339,6 +340,18 @@ exec_search(int argc, char **argv)
		case 'x':
			match = MATCH_REGEX;
			break;
+
		case 1:
+
			if (strcasecmp(optarg, "json") == 0)
+
			       opt |= INFO_RAW_JSON;
+
			else if (strcasecmp(optarg, "json-compact") == 0)
+
				opt |= INFO_RAW_JSON_COMPACT;
+
			else if (strcasecmp(optarg, "yaml") == 0)
+
				opt |= INFO_RAW_YAML;
+
			else
+
				errx(EX_USAGE, "Invalid format '%s' for the "
+
				    "raw output, expecting json, json-compat "
+
				    "or yaml", optarg);
+
			break;
		default:
			usage_search();
			return (EX_USAGE);
@@ -420,6 +433,11 @@ exec_search(int argc, char **argv)
		return (EX_IOERR);
	}

+
	if (opt & INFO_RAW) {
+
		if ((opt & (INFO_RAW_JSON|INFO_RAW_JSON_COMPACT)) == 0)
+
			opt |= INFO_RAW_YAML;
+
	}
+

	flags = info_flags(opt, true);
	while ((ret = pkgdb_it_next(it, &pkg, flags)) == EPKG_OK) {
		print_info(pkg, opt);
modified src/utils.c
@@ -318,6 +318,7 @@ print_info(struct pkg * const pkg, uint64_t options)
	int64_t flatsize, oldflatsize, pkgsize;
	int cout = 0;		/* Number of characters output */
	int info_num;		/* Number of different data items to print */
+
	int outflags = 0;

	pkg_get(pkg,
		PKG_REPOURL,       &repourl,
@@ -326,10 +327,20 @@ print_info(struct pkg * const pkg, uint64_t options)
		PKG_PKGSIZE,       &pkgsize);

	if (options & INFO_RAW) {
-
		if (pkg_type(pkg) != PKG_REMOTE)
-
			pkg_emit_manifest_file(pkg, stdout, PKG_MANIFEST_EMIT_PRETTY, NULL);
-
		else
-
			pkg_emit_manifest_file(pkg, stdout, PKG_MANIFEST_EMIT_COMPACT|PKG_MANIFEST_EMIT_PRETTY, NULL);
+
		switch (options & (INFO_RAW_YAML|INFO_RAW_JSON|INFO_RAW_JSON_COMPACT)) {
+
		case INFO_RAW_YAML:
+
			outflags |= PKG_MANIFEST_EMIT_PRETTY;
+
			break;
+
		case INFO_RAW_JSON:
+
			outflags |= PKG_MANIFEST_EMIT_JSON;
+
		case INFO_RAW_JSON_COMPACT:
+
			break;
+
		}
+
		if (pkg_type(pkg) == PKG_REMOTE)
+
			outflags |= PKG_MANIFEST_EMIT_COMPACT;
+

+
		pkg_emit_manifest_file(pkg, stdout, outflags, NULL);
+

		return;
	}