Include json output

This commit is contained in:
Michael 2019-08-12 22:09:44 +00:00
parent 8c5bbcaeeb
commit de21a2f687
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C
2 changed files with 26 additions and 2 deletions

View File

@ -1,11 +1,14 @@
#include <router_contact.hpp>
#include <util/logger.hpp>
#include <util/ostream_logger.hpp>
#include <cxxopts.hpp>
#include <string>
#include <vector>
bool dumpRc(const std::vector<std::string>& files)
bool dumpRc(const std::vector<std::string>& files, bool json)
{
nlohmann::json result;
for(const auto& file : files)
{
llarp::RouterContact rc;
@ -13,8 +16,15 @@ bool dumpRc(const std::vector<std::string>& files)
if (ret)
{
if (json)
{
result[file] = rc.ToJson();
}
else
{
std::cout << "file = " << file << "\n";
std::cout << rc << "\n\n";
}
}
else
{
@ -22,6 +32,9 @@ bool dumpRc(const std::vector<std::string>& files)
}
}
if (json)
std::cout << result << "\n";
return true;
}
@ -41,15 +54,19 @@ main(int argc, char *argv[])
options.add_options()
("v,verbose", "Verbose", cxxopts::value<bool>())
("h,help", "help", cxxopts::value<bool>())
("j,json", "output in json", cxxopts::value<bool>())
("dump", "dump rc file", cxxopts::value<std::vector<std::string>>(), "FILE");
// clang-format on
try {
const auto result = options.parse(argc, argv);
const bool json = result["json"].as<bool>();
if(result.count("verbose") > 0)
{
SetLogLevel(llarp::eLogDebug);
llarp::LogContext::Instance().logStream = std::make_unique<llarp::OStreamLogStream>(std::cerr);
llarp::LogDebug("debug logging activated");
}
@ -61,7 +78,7 @@ main(int argc, char *argv[])
if(result.count("dump") > 0)
{
if (!dumpRc(result["dump"].as<std::vector<std::string>>()))
if (!dumpRc(result["dump"].as<std::vector<std::string>>(), json))
{
return 1;
}

View File

@ -10,6 +10,7 @@
#include <util/status.hpp>
#include <functional>
#include <nlohmann/json.hpp>
#include <vector>
#define MAX_RC_SIZE (1024)
@ -107,6 +108,12 @@ namespace llarp
util::StatusObject
ExtractStatus() const;
nlohmann::json
ToJson() const
{
return ExtractStatus().get();
}
bool
BEncode(llarp_buffer_t *buf) const;