aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2022-06-10 15:43:18 +0200
committerStefan Boberg <[email protected]>2022-06-10 15:43:18 +0200
commitd73d3c85a9cc47ba2fde60842af5fe26ff77ccfd (patch)
treed7f232fa5c9689b4616c6f79d79ac5c6c56f4ae8
parentcbpackage: added initial support for marshaling of attachment by local reference (diff)
downloadzen-d73d3c85a9cc47ba2fde60842af5fe26ff77ccfd.tar.xz
zen-d73d3c85a9cc47ba2fde60842af5fe26ff77ccfd.zip
extended zen print command to also handle CbPackage and CompressedBuffer format payloads
-rw-r--r--zen/cmds/print.cpp100
1 files changed, 91 insertions, 9 deletions
diff --git a/zen/cmds/print.cpp b/zen/cmds/print.cpp
index f66f433f1..e5bb2ed4f 100644
--- a/zen/cmds/print.cpp
+++ b/zen/cmds/print.cpp
@@ -5,13 +5,31 @@
#include <zencore/compactbinarypackage.h>
#include <zencore/compactbinaryvalidation.h>
#include <zencore/filesystem.h>
+#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/string.h>
+#include <zenhttp/httpshared.h>
using namespace std::literals;
namespace zen {
+static void
+PrintCbObject(CbObject Object)
+{
+ zen::StringBuilder<1024> ObjStr;
+ zen::CompactBinaryToJson(Object, ObjStr);
+ zen::ConsoleLog().info("{}", ObjStr);
+}
+
+static void
+PrintCbObject(IoBuffer Data)
+{
+ zen::CbObject Object{SharedBuffer(Data)};
+
+ PrintCbObject(Object);
+}
+
PrintCommand::PrintCommand()
{
m_Options.add_options()("h,help", "Print help");
@@ -41,18 +59,88 @@ PrintCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
if (m_Filename.empty())
throw std::runtime_error("No file specified");
- zen::FileContents Fc = zen::ReadFile(m_Filename);
+ zen::FileContents Fc;
+
+ if (m_Filename == "-")
+ {
+ Fc = zen::ReadStdIn();
+ }
+ else
+ {
+ Fc = zen::ReadFile(m_Filename);
+ }
if (Fc.ErrorCode)
{
- zen::ConsoleLog().error("Failed to open file '{}': {}", m_Filename, Fc.ErrorCode.message());
+ zen::ConsoleLog().error("Failed to read file '{}': {}", m_Filename, Fc.ErrorCode.message());
return 1;
}
IoBuffer Data = Fc.Flatten();
- if (CbValidateError Result = ValidateCompactBinary(Data, CbValidateMode::All); Result != CbValidateError::None)
+ if (CompressedBuffer Compressed{CompressedBuffer::FromCompressed(SharedBuffer(Data))})
+ {
+ zen::ConsoleLog().info("Compressed binary: size {}, raw size {}, hash: {}",
+ Compressed.GetCompressedSize(),
+ Compressed.GetRawSize(),
+ IoHash::FromBLAKE3(Compressed.GetRawHash()));
+ }
+ else if (IsPackageMessage(Data))
+ {
+ CbPackage Package = ParsePackageMessage(Data);
+
+ CbObject Object = Package.GetObject();
+ std::span<const CbAttachment> Attachments = Package.GetAttachments();
+
+ zen::ConsoleLog().info("Package - {} attachments, object hash {}", Package.GetAttachments().size(), Package.GetObjectHash());
+ zen::ConsoleLog().info("");
+
+ int AttachmentIndex = 1;
+
+ for (const CbAttachment& Attachment : Attachments)
+ {
+ std::string AttachmentSize = "n/a";
+ const char* AttachmentType = "unknown";
+
+ if (Attachment.IsCompressedBinary())
+ {
+ AttachmentType = "Compressed";
+ AttachmentSize = fmt::format("{} ({} uncompressed)", Attachment.AsCompressedBinary().GetCompressedSize(), Attachment.AsCompressedBinary().GetRawSize());
+ }
+ else if (Attachment.IsBinary())
+ {
+ AttachmentType = "Binary";
+ AttachmentSize = fmt::format("{}", Attachment.AsBinary().GetSize());
+ }
+ else if (Attachment.IsObject())
+ {
+ AttachmentType = "Object";
+ AttachmentSize = fmt::format("{}", Attachment.AsObject().GetSize());
+ }
+ else if (Attachment.IsNull())
+ {
+ AttachmentType = "null";
+ }
+
+ zen::ConsoleLog().info("Attachment #{} : {}, {}, size {}",
+ AttachmentIndex,
+ Attachment.GetHash(),
+ AttachmentType,
+ AttachmentSize);
+
+ ++AttachmentIndex;
+ }
+
+ zen::ConsoleLog().info("---8<---");
+
+ PrintCbObject(Object);
+ }
+ else if (CbValidateError Result = ValidateCompactBinary(Data, CbValidateMode::All); Result == CbValidateError::None)
+ {
+ PrintCbObject(Data);
+ }
+ else
{
zen::ConsoleLog().error("Data in file '{}' does not appear to be compact binary (validation error {:#x})",
m_Filename,
@@ -61,12 +149,6 @@ PrintCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
return 1;
}
- zen::CbObject Object{SharedBuffer(Data)};
-
- zen::StringBuilder<1024> ObjStr;
- zen::CompactBinaryToJson(Object, ObjStr);
- zen::ConsoleLog().info("{}", ObjStr);
-
return 0;
}