nix-env -q: Add a --json flag

This commit is contained in:
Eelco Dolstra 2013-11-19 00:41:45 +01:00
parent 5fea98111b
commit 245e26408f

View file

@ -11,6 +11,7 @@
#include "store-api.hh" #include "store-api.hh"
#include "user-env.hh" #include "user-env.hh"
#include "util.hh" #include "util.hh"
#include "value-to-json.hh"
#include <cerrno> #include <cerrno>
#include <ctime> #include <ctime>
@ -874,6 +875,35 @@ static string colorString(const string & s)
} }
static void queryJSON(Globals & globals, vector<DrvInfo> & elems)
{
JSONObject topObj(cout);
foreach (vector<DrvInfo>::iterator, i, elems) {
topObj.attr(i->attrPath);
JSONObject pkgObj(cout);
pkgObj.attr("name", i->name);
pkgObj.attr("system", i->system);
pkgObj.attr("meta");
JSONObject metaObj(cout);
MetaInfo meta = i->queryMetaInfo(globals.state);
foreach (MetaInfo::iterator, j, meta) {
metaObj.attr(j->first);
if (j->second.type == MetaValue::tpString) {
escapeJSON(cout, j->second.stringValue);
} else if (j->second.type == MetaValue::tpInt) {
cout << j->second.intValue;
} else if (j->second.type == MetaValue::tpStrings) {
JSONList l(cout);
foreach (Strings::iterator, k, j->second.stringValues)
l.elem(*k);
}
}
}
}
static void opQuery(Globals & globals, static void opQuery(Globals & globals,
Strings args, Strings opFlags, Strings opArgs) Strings args, Strings opFlags, Strings opArgs)
{ {
@ -891,6 +921,7 @@ static void opQuery(Globals & globals,
bool printMeta = false; bool printMeta = false;
bool compareVersions = false; bool compareVersions = false;
bool xmlOutput = false; bool xmlOutput = false;
bool jsonOutput = false;
enum { sInstalled, sAvailable } source = sInstalled; enum { sInstalled, sAvailable } source = sInstalled;
@ -909,6 +940,7 @@ static void opQuery(Globals & globals,
else if (arg == "--installed") source = sInstalled; else if (arg == "--installed") source = sInstalled;
else if (arg == "--available" || arg == "-a") source = sAvailable; else if (arg == "--available" || arg == "-a") source = sAvailable;
else if (arg == "--xml") xmlOutput = true; else if (arg == "--xml") xmlOutput = true;
else if (arg == "--json") jsonOutput = true;
else if (arg == "--attr-path" || arg == "-P") printAttrPath = true; else if (arg == "--attr-path" || arg == "-P") printAttrPath = true;
else if (arg == "--attr" || arg == "-A") else if (arg == "--attr" || arg == "-A")
attrPath = needArg(i, args, arg); attrPath = needArg(i, args, arg);
@ -929,7 +961,7 @@ static void opQuery(Globals & globals,
globals.instSource.systemFilter, globals.instSource.autoArgs, globals.instSource.systemFilter, globals.instSource.autoArgs,
attrPath, availElems); attrPath, availElems);
DrvInfos elems = filterBySelector(globals.state, DrvInfos elems_ = filterBySelector(globals.state,
source == sInstalled ? installedElems : availElems, source == sInstalled ? installedElems : availElems,
remaining, false); remaining, false);
@ -938,10 +970,10 @@ static void opQuery(Globals & globals,
/* Sort them by name. */ /* Sort them by name. */
/* !!! */ /* !!! */
vector<DrvInfo> elems2; vector<DrvInfo> elems;
for (DrvInfos::iterator i = elems.begin(); i != elems.end(); ++i) for (DrvInfos::iterator i = elems_.begin(); i != elems_.end(); ++i)
elems2.push_back(*i); elems.push_back(*i);
sort(elems2.begin(), elems2.end(), cmpElemByName); sort(elems.begin(), elems.end(), cmpElemByName);
/* We only need to know the installed paths when we are querying /* We only need to know the installed paths when we are querying
@ -959,7 +991,7 @@ static void opQuery(Globals & globals,
PathSet validPaths, substitutablePaths; PathSet validPaths, substitutablePaths;
if (printStatus || globals.prebuiltOnly) { if (printStatus || globals.prebuiltOnly) {
PathSet paths; PathSet paths;
foreach (vector<DrvInfo>::iterator, i, elems2) foreach (vector<DrvInfo>::iterator, i, elems)
try { try {
paths.insert(i->queryOutPath(globals.state)); paths.insert(i->queryOutPath(globals.state));
} catch (AssertionError & e) { } catch (AssertionError & e) {
@ -972,12 +1004,17 @@ static void opQuery(Globals & globals,
/* Print the desired columns, or XML output. */ /* Print the desired columns, or XML output. */
if (jsonOutput) {
queryJSON(globals, elems);
return;
}
Table table; Table table;
std::ostringstream dummy; std::ostringstream dummy;
XMLWriter xml(true, *(xmlOutput ? &cout : &dummy)); XMLWriter xml(true, *(xmlOutput ? &cout : &dummy));
XMLOpenElement xmlRoot(xml, "items"); XMLOpenElement xmlRoot(xml, "items");
foreach (vector<DrvInfo>::iterator, i, elems2) { foreach (vector<DrvInfo>::iterator, i, elems) {
try { try {
if (i->hasFailed()) continue; if (i->hasFailed()) continue;
@ -1098,7 +1135,7 @@ static void opQuery(Globals & globals,
} }
if (printMeta) { if (printMeta) {
MetaInfo meta = i->queryMetaInfo(globals.state); MetaInfo meta = i->queryMetaInfo(globals.state);
for (MetaInfo::iterator j = meta.begin(); j != meta.end(); ++j) { foreach (MetaInfo::iterator, j, meta) {
XMLAttrs attrs2; XMLAttrs attrs2;
attrs2["name"] = j->first; attrs2["name"] = j->first;
if (j->second.type == MetaValue::tpString) { if (j->second.type == MetaValue::tpString) {