2017-07-17 17:02:56 +00:00
|
|
|
#include "command.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "eval.hh"
|
|
|
|
#include "eval-inline.hh"
|
|
|
|
#include "names.hh"
|
|
|
|
#include "get-drvs.hh"
|
2017-07-18 15:30:09 +00:00
|
|
|
#include "common-args.hh"
|
|
|
|
#include "json.hh"
|
2017-07-26 15:21:46 +00:00
|
|
|
#include "json-to-value.hh"
|
2018-05-12 18:00:59 +00:00
|
|
|
#include "shared.hh"
|
2017-07-17 17:02:56 +00:00
|
|
|
|
|
|
|
#include <regex>
|
2017-07-26 15:21:46 +00:00
|
|
|
#include <fstream>
|
2017-07-17 17:02:56 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2018-05-12 18:00:59 +00:00
|
|
|
std::string wrap(std::string prefix, std::string s)
|
|
|
|
{
|
|
|
|
return prefix + s + ANSI_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string hilite(const std::string & s, const std::smatch & m, std::string postfix)
|
2017-07-17 17:02:56 +00:00
|
|
|
{
|
|
|
|
return
|
|
|
|
m.empty()
|
|
|
|
? s
|
|
|
|
: std::string(m.prefix())
|
2018-05-12 18:00:59 +00:00
|
|
|
+ ANSI_RED + std::string(m.str()) + postfix
|
2017-07-17 17:02:56 +00:00
|
|
|
+ std::string(m.suffix());
|
|
|
|
}
|
|
|
|
|
2017-07-18 15:30:09 +00:00
|
|
|
struct CmdSearch : SourceExprCommand, MixJSON
|
2017-07-17 17:02:56 +00:00
|
|
|
{
|
2018-04-18 19:08:35 +00:00
|
|
|
std::vector<std::string> res;
|
2017-07-17 17:02:56 +00:00
|
|
|
|
2017-07-26 15:21:46 +00:00
|
|
|
bool writeCache = true;
|
|
|
|
bool useCache = true;
|
|
|
|
|
2017-07-17 17:02:56 +00:00
|
|
|
CmdSearch()
|
|
|
|
{
|
2018-04-18 19:08:35 +00:00
|
|
|
expectArgs("regex", &res);
|
2017-07-26 15:21:46 +00:00
|
|
|
|
|
|
|
mkFlag()
|
|
|
|
.longName("update-cache")
|
|
|
|
.shortName('u')
|
|
|
|
.description("update the package search cache")
|
2017-10-24 10:45:11 +00:00
|
|
|
.handler([&]() { writeCache = true; useCache = false; });
|
2017-07-26 15:21:46 +00:00
|
|
|
|
|
|
|
mkFlag()
|
|
|
|
.longName("no-cache")
|
|
|
|
.description("do not use or update the package search cache")
|
2017-10-24 10:45:11 +00:00
|
|
|
.handler([&]() { writeCache = false; useCache = false; });
|
2017-07-17 17:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "query available packages";
|
|
|
|
}
|
|
|
|
|
2017-09-07 18:42:11 +00:00
|
|
|
Examples examples() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
Example{
|
|
|
|
"To show all available packages:",
|
|
|
|
"nix search"
|
|
|
|
},
|
|
|
|
Example{
|
|
|
|
"To show any packages containing 'blender' in its name or description:",
|
|
|
|
"nix search blender"
|
|
|
|
},
|
|
|
|
Example{
|
2018-04-18 19:08:35 +00:00
|
|
|
"To search for Firefox or Chromium:",
|
2017-09-07 18:42:11 +00:00
|
|
|
"nix search 'firefox|chromium'"
|
|
|
|
},
|
2018-04-18 19:08:35 +00:00
|
|
|
Example{
|
|
|
|
"To search for git and frontend or gui:",
|
|
|
|
"nix search git 'frontend|gui'"
|
|
|
|
},
|
2018-05-12 18:00:59 +00:00
|
|
|
Example{
|
|
|
|
"To display the description of the found packages:",
|
|
|
|
"nix search git --verbose"
|
|
|
|
}
|
2017-09-07 18:42:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:02:56 +00:00
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
|
|
|
settings.readOnlyMode = true;
|
|
|
|
|
2018-02-25 22:38:13 +00:00
|
|
|
// Empty search string should match all packages
|
|
|
|
// Use "^" here instead of ".*" due to differences in resulting highlighting
|
|
|
|
// (see #1893 -- libc++ claims empty search string is not in POSIX grammar)
|
2018-04-18 19:08:35 +00:00
|
|
|
if (res.empty()) {
|
|
|
|
res.push_back("^");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::regex> regexes;
|
|
|
|
regexes.reserve(res.size());
|
2018-02-25 22:38:13 +00:00
|
|
|
|
2018-04-18 19:08:35 +00:00
|
|
|
for (auto &re : res) {
|
|
|
|
regexes.push_back(std::regex(re, std::regex::extended | std::regex::icase));
|
|
|
|
}
|
2017-07-17 17:02:56 +00:00
|
|
|
|
|
|
|
auto state = getEvalState();
|
|
|
|
|
2017-11-14 13:27:01 +00:00
|
|
|
auto jsonOut = json ? std::make_unique<JSONObject>(std::cout) : nullptr;
|
2017-07-18 15:30:09 +00:00
|
|
|
|
2017-07-19 14:06:10 +00:00
|
|
|
auto sToplevel = state->symbols.create("_toplevel");
|
2017-07-26 15:21:46 +00:00
|
|
|
auto sRecurse = state->symbols.create("recurseForDerivations");
|
|
|
|
|
|
|
|
bool fromCache = false;
|
2017-07-19 14:06:10 +00:00
|
|
|
|
2017-10-31 10:57:53 +00:00
|
|
|
std::map<std::string, std::string> results;
|
|
|
|
|
2017-07-26 15:21:46 +00:00
|
|
|
std::function<void(Value *, std::string, bool, JSONObject *)> doExpr;
|
|
|
|
|
|
|
|
doExpr = [&](Value * v, std::string attrPath, bool toplevel, JSONObject * cache) {
|
2017-07-30 11:27:57 +00:00
|
|
|
debug("at attribute '%s'", attrPath);
|
2017-07-17 17:02:56 +00:00
|
|
|
|
|
|
|
try {
|
2018-04-18 19:08:35 +00:00
|
|
|
uint found = 0;
|
2017-07-17 17:02:56 +00:00
|
|
|
|
|
|
|
state->forceValue(*v);
|
|
|
|
|
|
|
|
if (v->type == tLambda && toplevel) {
|
|
|
|
Value * v2 = state->allocValue();
|
|
|
|
state->autoCallFunction(*state->allocBindings(1), *v, *v2);
|
|
|
|
v = v2;
|
|
|
|
state->forceValue(*v);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state->isDerivation(*v)) {
|
|
|
|
|
|
|
|
DrvInfo drv(*state, attrPath, v->attrs);
|
2018-04-18 19:08:35 +00:00
|
|
|
std::string description;
|
|
|
|
std::smatch attrPathMatch;
|
|
|
|
std::smatch descriptionMatch;
|
|
|
|
std::smatch nameMatch;
|
|
|
|
std::string name;
|
2017-07-17 17:02:56 +00:00
|
|
|
|
|
|
|
DrvName parsed(drv.queryName());
|
|
|
|
|
2018-04-18 19:08:35 +00:00
|
|
|
for (auto ®ex : regexes) {
|
|
|
|
std::regex_search(attrPath, attrPathMatch, regex);
|
2017-07-17 17:02:56 +00:00
|
|
|
|
2018-04-18 19:08:35 +00:00
|
|
|
name = parsed.name;
|
|
|
|
std::regex_search(name, nameMatch, regex);
|
2017-07-17 17:02:56 +00:00
|
|
|
|
2018-04-18 19:08:35 +00:00
|
|
|
description = drv.queryMetaString("description");
|
|
|
|
std::replace(description.begin(), description.end(), '\n', ' ');
|
|
|
|
std::regex_search(description, descriptionMatch, regex);
|
|
|
|
|
|
|
|
if (!attrPathMatch.empty()
|
|
|
|
|| !nameMatch.empty()
|
|
|
|
|| !descriptionMatch.empty())
|
|
|
|
{
|
|
|
|
found++;
|
|
|
|
}
|
|
|
|
}
|
2017-07-17 17:02:56 +00:00
|
|
|
|
2018-04-18 19:08:35 +00:00
|
|
|
if (found == res.size()) {
|
2017-07-18 15:30:09 +00:00
|
|
|
if (json) {
|
|
|
|
|
|
|
|
auto jsonElem = jsonOut->object(attrPath);
|
|
|
|
|
|
|
|
jsonElem.attr("pkgName", parsed.name);
|
|
|
|
jsonElem.attr("version", parsed.version);
|
|
|
|
jsonElem.attr("description", description);
|
|
|
|
|
|
|
|
} else {
|
2018-07-27 14:52:54 +00:00
|
|
|
auto name = hilite(parsed.name, nameMatch, "\e[0;2m")
|
|
|
|
+ std::string(parsed.fullName, parsed.name.length());
|
2017-10-31 10:57:53 +00:00
|
|
|
results[attrPath] = fmt(
|
2018-05-12 18:00:59 +00:00
|
|
|
"* %s (%s)\n %s\n",
|
|
|
|
wrap("\e[0;1m", hilite(attrPath, attrPathMatch, "\e[0;1m")),
|
2018-07-27 14:52:54 +00:00
|
|
|
wrap("\e[0;2m", hilite(name, nameMatch, "\e[0;2m")),
|
2018-05-12 18:00:59 +00:00
|
|
|
hilite(description, descriptionMatch, ANSI_NORMAL));
|
2017-07-18 15:30:09 +00:00
|
|
|
}
|
2017-07-17 17:02:56 +00:00
|
|
|
}
|
2017-07-26 15:21:46 +00:00
|
|
|
|
|
|
|
if (cache) {
|
|
|
|
cache->attr("type", "derivation");
|
|
|
|
cache->attr("name", drv.queryName());
|
|
|
|
cache->attr("system", drv.querySystem());
|
|
|
|
if (description != "") {
|
|
|
|
auto meta(cache->object("meta"));
|
|
|
|
meta.attr("description", description);
|
|
|
|
}
|
|
|
|
}
|
2017-07-17 17:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (v->type == tAttrs) {
|
|
|
|
|
|
|
|
if (!toplevel) {
|
|
|
|
auto attrs = v->attrs;
|
2017-07-26 15:21:46 +00:00
|
|
|
Bindings::iterator j = attrs->find(sRecurse);
|
|
|
|
if (j == attrs->end() || !state->forceBool(*j->value, *j->pos)) {
|
2017-07-30 11:27:57 +00:00
|
|
|
debug("skip attribute '%s'", attrPath);
|
2017-07-26 15:21:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-17 17:02:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 15:21:46 +00:00
|
|
|
bool toplevel2 = false;
|
|
|
|
if (!fromCache) {
|
|
|
|
Bindings::iterator j = v->attrs->find(sToplevel);
|
|
|
|
toplevel2 = j != v->attrs->end() && state->forceBool(*j->value, *j->pos);
|
|
|
|
}
|
2017-07-17 17:02:56 +00:00
|
|
|
|
|
|
|
for (auto & i : *v->attrs) {
|
2017-07-26 15:21:46 +00:00
|
|
|
auto cache2 =
|
|
|
|
cache ? std::make_unique<JSONObject>(cache->object(i.name)) : nullptr;
|
2017-07-17 17:02:56 +00:00
|
|
|
doExpr(i.value,
|
|
|
|
attrPath == "" ? (std::string) i.name : attrPath + "." + (std::string) i.name,
|
2017-07-26 15:21:46 +00:00
|
|
|
toplevel2 || fromCache, cache2 ? cache2.get() : nullptr);
|
2017-07-17 17:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (AssertionError & e) {
|
2017-07-19 14:06:10 +00:00
|
|
|
} catch (Error & e) {
|
|
|
|
if (!toplevel) {
|
2017-07-30 11:27:57 +00:00
|
|
|
e.addPrefix(fmt("While evaluating the attribute '%s':\n", attrPath));
|
2017-07-19 14:06:10 +00:00
|
|
|
throw;
|
|
|
|
}
|
2017-07-17 17:02:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-26 15:21:46 +00:00
|
|
|
Path jsonCacheFileName = getCacheDir() + "/nix/package-search.json";
|
|
|
|
|
|
|
|
if (useCache && pathExists(jsonCacheFileName)) {
|
|
|
|
|
2017-08-29 13:22:05 +00:00
|
|
|
warn("using cached results; pass '-u' to update the cache");
|
|
|
|
|
2017-07-26 15:21:46 +00:00
|
|
|
Value vRoot;
|
|
|
|
parseJSON(*state, readFile(jsonCacheFileName), vRoot);
|
|
|
|
|
|
|
|
fromCache = true;
|
|
|
|
|
|
|
|
doExpr(&vRoot, "", true, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2017-12-04 12:18:25 +00:00
|
|
|
createDirs(dirOf(jsonCacheFileName));
|
|
|
|
|
2017-07-26 15:21:46 +00:00
|
|
|
Path tmpFile = fmt("%s.tmp.%d", jsonCacheFileName, getpid());
|
|
|
|
|
2017-12-04 12:18:25 +00:00
|
|
|
std::ofstream jsonCacheFile;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// iostream considered harmful
|
|
|
|
jsonCacheFile.exceptions(std::ofstream::failbit);
|
|
|
|
jsonCacheFile.open(tmpFile);
|
2017-07-26 15:21:46 +00:00
|
|
|
|
2017-12-04 12:18:25 +00:00
|
|
|
auto cache = writeCache ? std::make_unique<JSONObject>(jsonCacheFile, false) : nullptr;
|
2017-07-26 15:21:46 +00:00
|
|
|
|
2019-04-08 14:11:17 +00:00
|
|
|
// FIXME
|
|
|
|
throw Error("NOT IMPLEMENTED");
|
|
|
|
//doExpr(getSourceExpr(*state), "", true, cache.get());
|
2017-12-04 12:18:25 +00:00
|
|
|
|
|
|
|
} catch (std::exception &) {
|
|
|
|
/* Fun fact: catching std::ios::failure does not work
|
|
|
|
due to C++11 ABI shenanigans.
|
|
|
|
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66145 */
|
|
|
|
if (!jsonCacheFile)
|
|
|
|
throw Error("error writing to %s", tmpFile);
|
|
|
|
}
|
2017-07-26 15:21:46 +00:00
|
|
|
|
2018-02-25 22:32:36 +00:00
|
|
|
if (writeCache && rename(tmpFile.c_str(), jsonCacheFileName.c_str()) == -1)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw SysError("cannot rename '%s' to '%s'", tmpFile, jsonCacheFileName);
|
2017-07-26 15:21:46 +00:00
|
|
|
}
|
2017-10-31 10:57:53 +00:00
|
|
|
|
2018-05-12 18:00:59 +00:00
|
|
|
if (results.size() == 0)
|
|
|
|
throw Error("no results for the given search term(s)!");
|
|
|
|
|
|
|
|
RunPager pager;
|
2017-10-31 10:57:53 +00:00
|
|
|
for (auto el : results) std::cout << el.second << "\n";
|
|
|
|
|
2017-07-17 17:02:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 14:01:35 +00:00
|
|
|
static auto r1 = registerCommand<CmdSearch>("search");
|