mirror of
https://github.com/NixOS/nix
synced 2025-07-06 21:41:48 +02:00
nix ls-{store,nar}: Add --json flag
This commit is contained in:
parent
c0d93a01ee
commit
bac8055652
4 changed files with 53 additions and 40 deletions
|
@ -119,42 +119,9 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, const ref<std::str
|
|||
accessor_->addToCache(info.path, *nar);
|
||||
}
|
||||
|
||||
std::function<void(const Path &, JSONPlaceholder &)> recurse;
|
||||
|
||||
recurse = [&](const Path & path, JSONPlaceholder & res) {
|
||||
auto st = narAccessor->stat(path);
|
||||
|
||||
auto obj = res.object();
|
||||
|
||||
switch (st.type) {
|
||||
case FSAccessor::Type::tRegular:
|
||||
obj.attr("type", "regular");
|
||||
obj.attr("size", st.fileSize);
|
||||
if (st.isExecutable)
|
||||
obj.attr("executable", true);
|
||||
break;
|
||||
case FSAccessor::Type::tDirectory:
|
||||
obj.attr("type", "directory");
|
||||
{
|
||||
auto res2 = obj.object("entries");
|
||||
for (auto & name : narAccessor->readDirectory(path)) {
|
||||
auto res3 = res2.placeholder(name);
|
||||
recurse(path + "/" + name, res3);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case FSAccessor::Type::tSymlink:
|
||||
obj.attr("type", "symlink");
|
||||
obj.attr("target", narAccessor->readLink(path));
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
};
|
||||
|
||||
{
|
||||
auto res = jsonRoot.placeholder("root");
|
||||
recurse("", res);
|
||||
listNar(res, narAccessor, "");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "nar-accessor.hh"
|
||||
#include "archive.hh"
|
||||
#include "json.hh"
|
||||
|
||||
#include <map>
|
||||
#include <stack>
|
||||
|
@ -181,4 +182,36 @@ ref<FSAccessor> makeNarAccessor(ref<const std::string> nar)
|
|||
return make_ref<NarAccessor>(nar);
|
||||
}
|
||||
|
||||
void listNar(JSONPlaceholder & res, ref<FSAccessor> accessor, const Path & path)
|
||||
{
|
||||
auto st = accessor->stat(path);
|
||||
|
||||
auto obj = res.object();
|
||||
|
||||
switch (st.type) {
|
||||
case FSAccessor::Type::tRegular:
|
||||
obj.attr("type", "regular");
|
||||
obj.attr("size", st.fileSize);
|
||||
if (st.isExecutable)
|
||||
obj.attr("executable", true);
|
||||
break;
|
||||
case FSAccessor::Type::tDirectory:
|
||||
obj.attr("type", "directory");
|
||||
{
|
||||
auto res2 = obj.object("entries");
|
||||
for (auto & name : accessor->readDirectory(path)) {
|
||||
auto res3 = res2.placeholder(name);
|
||||
listNar(res3, accessor, path + "/" + name);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case FSAccessor::Type::tSymlink:
|
||||
obj.attr("type", "symlink");
|
||||
obj.attr("target", accessor->readLink(path));
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,4 +8,8 @@ namespace nix {
|
|||
file. */
|
||||
ref<FSAccessor> makeNarAccessor(ref<const std::string> nar);
|
||||
|
||||
class JSONPlaceholder;
|
||||
|
||||
void listNar(JSONPlaceholder & res, ref<FSAccessor> accessor, const Path & path);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue