nix make-content-addressable: Add --json flag

Fixes #3274.
This commit is contained in:
Eelco Dolstra 2019-12-18 17:39:02 +01:00
parent f8abbdd456
commit 4511f09b49

View file

@ -1,10 +1,12 @@
#include "command.hh" #include "command.hh"
#include "store-api.hh" #include "store-api.hh"
#include "references.hh" #include "references.hh"
#include "common-args.hh"
#include "json.hh"
using namespace nix; using namespace nix;
struct CmdMakeContentAddressable : StorePathsCommand struct CmdMakeContentAddressable : StorePathsCommand, MixJSON
{ {
CmdMakeContentAddressable() CmdMakeContentAddressable()
{ {
@ -37,6 +39,9 @@ struct CmdMakeContentAddressable : StorePathsCommand
std::map<StorePath, StorePath> remappings; std::map<StorePath, StorePath> remappings;
auto jsonRoot = json ? std::make_unique<JSONObject>(std::cout) : nullptr;
auto jsonRewrites = json ? std::make_unique<JSONObject>(jsonRoot->object("rewrites")) : nullptr;
for (auto & path : paths) { for (auto & path : paths) {
auto pathS = store->printStorePath(path); auto pathS = store->printStorePath(path);
auto oldInfo = store->queryPathInfo(path); auto oldInfo = store->queryPathInfo(path);
@ -76,6 +81,7 @@ struct CmdMakeContentAddressable : StorePathsCommand
info.narSize = sink.s->size(); info.narSize = sink.s->size();
info.ca = makeFixedOutputCA(true, info.narHash); info.ca = makeFixedOutputCA(true, info.narHash);
if (!json)
printError("rewrote '%s' to '%s'", pathS, store->printStorePath(info.path)); printError("rewrote '%s' to '%s'", pathS, store->printStorePath(info.path));
auto source = sinkToSource([&](Sink & nextSink) { auto source = sinkToSource([&](Sink & nextSink) {
@ -86,6 +92,9 @@ struct CmdMakeContentAddressable : StorePathsCommand
store->addToStore(info, *source); store->addToStore(info, *source);
if (json)
jsonRewrites->attr(store->printStorePath(path), store->printStorePath(info.path));
remappings.insert_or_assign(std::move(path), std::move(info.path)); remappings.insert_or_assign(std::move(path), std::move(info.path));
} }
} }