diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2017-07-18T15·30+0200 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2017-07-20T11·33+0200 |
commit | b144c4d61738b9d9e3d741e77066a2be115f0ab5 (patch) | |
tree | 77ac8605c1e5b96a3ec8eb939fae1701163a504f /src/nix | |
parent | 90825dea518ea078f0783a72cc471a5b3716d198 (diff) |
nix search: Add --json flag
Diffstat (limited to 'src/nix')
-rw-r--r-- | src/nix/search.cc | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/src/nix/search.cc b/src/nix/search.cc index 813f6d0a6226..8aac06ad2cd3 100644 --- a/src/nix/search.cc +++ b/src/nix/search.cc @@ -4,6 +4,8 @@ #include "eval-inline.hh" #include "names.hh" #include "get-drvs.hh" +#include "common-args.hh" +#include "json.hh" #include <regex> @@ -19,7 +21,7 @@ std::string hilite(const std::string & s, const std::smatch & m) + std::string(m.suffix()); } -struct CmdSearch : SourceExprCommand +struct CmdSearch : SourceExprCommand, MixJSON { std::string re; @@ -50,6 +52,8 @@ struct CmdSearch : SourceExprCommand bool first = true; + auto jsonOut = json ? std::make_unique<JSONObject>(std::cout, true) : nullptr; + doExpr = [&](Value * v, std::string attrPath, bool toplevel) { debug("at attribute ‘%s’", attrPath); @@ -86,18 +90,28 @@ struct CmdSearch : SourceExprCommand || !nameMatch.empty() || !descriptionMatch.empty()) { - if (!first) std::cout << "\n"; - first = false; - - std::cout << fmt( - "Attribute name: %s\n" - "Package name: %s\n" - "Version: %s\n" - "Description: %s\n", - hilite(attrPath, attrPathMatch), - hilite(name, nameMatch), - parsed.version, - hilite(description, descriptionMatch)); + if (json) { + + auto jsonElem = jsonOut->object(attrPath); + + jsonElem.attr("pkgName", parsed.name); + jsonElem.attr("version", parsed.version); + jsonElem.attr("description", description); + + } else { + if (!first) std::cout << "\n"; + first = false; + + std::cout << fmt( + "Attribute name: %s\n" + "Package name: %s\n" + "Version: %s\n" + "Description: %s\n", + hilite(attrPath, attrPathMatch), + hilite(name, nameMatch), + parsed.version, + hilite(description, descriptionMatch)); + } } } |