printFlakeInfo: Separate JSON output

This commit is contained in:
Eelco Dolstra 2019-05-28 14:01:57 +02:00
parent ecee759b80
commit 46294d60cd
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -94,34 +94,36 @@ static void sourceInfoToJson(const SourceInfo & sourceInfo, nlohmann::json & j)
j["path"] = sourceInfo.storePath; j["path"] = sourceInfo.storePath;
} }
static void printFlakeInfo(const Flake & flake, bool json) static void printFlakeInfo(const Flake & flake)
{ {
if (json) { std::cout << "ID: " << flake.id << "\n";
nlohmann::json j; std::cout << "Description: " << flake.description << "\n";
j["id"] = flake.id; std::cout << "Epoch: " << flake.epoch << "\n";
j["description"] = flake.description; printSourceInfo(flake.sourceInfo);
j["epoch"] = flake.epoch;
sourceInfoToJson(flake.sourceInfo, j);
std::cout << j.dump(4) << std::endl;
} else {
std::cout << "ID: " << flake.id << "\n";
std::cout << "Description: " << flake.description << "\n";
std::cout << "Epoch: " << flake.epoch << "\n";
printSourceInfo(flake.sourceInfo);
}
} }
static void printNonFlakeInfo(const NonFlake & nonFlake, bool json) static nlohmann::json flakeToJson(const Flake & flake)
{ {
if (json) { nlohmann::json j;
nlohmann::json j; j["id"] = flake.id;
j["id"] = nonFlake.alias; j["description"] = flake.description;
printSourceInfo(nonFlake.sourceInfo); j["epoch"] = flake.epoch;
std::cout << j.dump(4) << std::endl; sourceInfoToJson(flake.sourceInfo, j);
} else { return j;
std::cout << "ID: " << nonFlake.alias << "\n"; }
printSourceInfo(nonFlake.sourceInfo);
} static void printNonFlakeInfo(const NonFlake & nonFlake)
{
std::cout << "ID: " << nonFlake.alias << "\n";
printSourceInfo(nonFlake.sourceInfo);
}
static nlohmann::json nonFlakeToJson(const NonFlake & nonFlake)
{
nlohmann::json j;
j["id"] = nonFlake.alias;
sourceInfoToJson(nonFlake.sourceInfo, j);
return j;
} }
// FIXME: merge info CmdFlakeInfo? // FIXME: merge info CmdFlakeInfo?
@ -152,10 +154,10 @@ struct CmdFlakeDeps : FlakeCommand
todo.pop(); todo.pop();
for (auto & nonFlake : resFlake.nonFlakeDeps) for (auto & nonFlake : resFlake.nonFlakeDeps)
printNonFlakeInfo(nonFlake, false); printNonFlakeInfo(nonFlake);
for (auto & info : resFlake.flakeDeps) { for (auto & info : resFlake.flakeDeps) {
printFlakeInfo(info.second.flake, false); printFlakeInfo(info.second.flake);
todo.push(info.second); todo.push(info.second);
} }
} }
@ -205,7 +207,10 @@ struct CmdFlakeInfo : FlakeCommand, MixJSON
{ {
auto flake = getFlake(); auto flake = getFlake();
stopProgressBar(); stopProgressBar();
printFlakeInfo(flake, json); if (json)
std::cout << flakeToJson(flake).dump() << std::endl;
else
printFlakeInfo(flake);
} }
}; };