diff options
| author | Stefan Boberg <[email protected]> | 2021-10-01 18:59:51 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-10-01 18:59:51 +0200 |
| commit | c885ef7826c7faeb60df0297e72ab101704efad5 (patch) | |
| tree | cbbbf0b2d89e1e6a325cedc2dd0a4d966075ef20 | |
| parent | cas: added some GC stubs (diff) | |
| download | zen-c885ef7826c7faeb60df0297e72ab101704efad5.tar.xz zen-c885ef7826c7faeb60df0297e72ab101704efad5.zip | |
zen: added print/printpackage subcommands to help in debugging or inspecting CbObject data generally
| -rw-r--r-- | zen/cmds/print.cpp | 107 | ||||
| -rw-r--r-- | zen/cmds/print.h | 41 | ||||
| -rw-r--r-- | zen/zen.cpp | 29 | ||||
| -rw-r--r-- | zen/zen.vcxproj | 2 | ||||
| -rw-r--r-- | zen/zen.vcxproj.filters | 2 |
5 files changed, 169 insertions, 12 deletions
diff --git a/zen/cmds/print.cpp b/zen/cmds/print.cpp new file mode 100644 index 000000000..aac6afd44 --- /dev/null +++ b/zen/cmds/print.cpp @@ -0,0 +1,107 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include "print.h" + +#include <zencore/compactbinarypackage.h> +#include <zencore/filesystem.h> +#include <zencore/logging.h> +#include <zencore/string.h> + +using namespace std::literals; + +namespace zen { + +PrintCommand::PrintCommand() +{ + m_Options.add_options()("h,help", "Print help"); + m_Options.add_option("", "s", "source", "Object payload file", cxxopts::value(m_Filename), "<file name>"); +} + +PrintCommand::~PrintCommand() = default; + +int +PrintCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) +{ + ZEN_UNUSED(GlobalOptions, argc, argv); + + m_Options.parse_positional({"source"}); + + auto result = m_Options.parse(argc, argv); + + if (result.count("help")) + { + std::cout << m_Options.help({"", "Group"}) << std::endl; + + return 0; + } + + // Validate arguments + + if (m_Filename.empty()) + throw std::runtime_error("No file specified"); + + zen::FileContents Fc = zen::ReadFile(m_Filename); + IoBuffer Data = Fc.Flatten(); + zen::CbObject Object{SharedBuffer(Data)}; + + zen::StringBuilder<1024> ObjStr; + zen::CompactBinaryToJson(Object, ObjStr); + zen::ConsoleLog().info("{}", ObjStr); + + return 0; +} + +////////////////////////////////////////////////////////////////////////// + +PrintPackageCommand::PrintPackageCommand() +{ + m_Options.add_options()("h,help", "Print help"); + m_Options.add_option("", "s", "source", "Package payload file", cxxopts::value(m_Filename), "<file name>"); +} + +PrintPackageCommand::~PrintPackageCommand() +{ +} + +int +PrintPackageCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) +{ + ZEN_UNUSED(GlobalOptions, argc, argv); + + m_Options.parse_positional({"source"}); + + auto result = m_Options.parse(argc, argv); + + if (result.count("help")) + { + std::cout << m_Options.help({"", "Group"}) << std::endl; + + return 0; + } + + // Validate arguments + + if (m_Filename.empty()) + throw std::runtime_error("No file specified"); + + zen::FileContents Fc = zen::ReadFile(m_Filename); + IoBuffer Data = Fc.Flatten(); + zen::CbPackage Package; + + bool Ok = Package.TryLoad(Data) || zen::legacy::TryLoadCbPackage(Package, Data, &UniqueBuffer::Alloc); + + if (Ok) + { + zen::StringBuilder<1024> ObjStr; + zen::CompactBinaryToJson(Package.GetObject(), ObjStr); + zen::ConsoleLog().info("{}", ObjStr); + } + else + { + zen::ConsoleLog().error("error: malformed package?"); + } + + return 0; +} + +} // namespace zen diff --git a/zen/cmds/print.h b/zen/cmds/print.h new file mode 100644 index 000000000..eed0aa14e --- /dev/null +++ b/zen/cmds/print.h @@ -0,0 +1,41 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include "../zen.h" + +namespace zen { + +/** Print Compact Binary + */ +class PrintCommand : public ZenCmdBase +{ +public: + PrintCommand(); + ~PrintCommand(); + + virtual int Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) override; + virtual cxxopts::Options* Options() override { return &m_Options; } + +private: + cxxopts::Options m_Options{"print", "Print compact binary object"}; + std::string m_Filename; +}; + +/** Print Compact Binary Package + */ +class PrintPackageCommand : public ZenCmdBase +{ +public: + PrintPackageCommand(); + ~PrintPackageCommand(); + + virtual int Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) override; + virtual cxxopts::Options* Options() override { return &m_Options; } + +private: + cxxopts::Options m_Options{"printpkg", "Print compact binary package"}; + std::string m_Filename; +}; + +} // namespace zen diff --git a/zen/zen.cpp b/zen/zen.cpp index 86c41d658..3c33ff5e0 100644 --- a/zen/zen.cpp +++ b/zen/zen.cpp @@ -9,6 +9,7 @@ #include "cmds/dedup.h" #include "cmds/deploy.h" #include "cmds/hash.h" +#include "cmds/print.h" #include "cmds/run.h" #include "cmds/status.h" #include "cmds/top.h" @@ -98,18 +99,20 @@ main(int argc, char** argv) auto _ = zen::MakeGuard([] { spdlog::shutdown(); }); - HashCommand HashCmd; - CopyCommand CopyCmd; - DedupCommand DedupCmd; - DeployCommand DeployCmd; - DropCommand DropCmd; - ChunkCommand ChunkCmd; - RunCommand RunCmd; - StatusCommand StatusCmd; - TopCommand TopCmd; - PsCommand PsCmd; - UpCommand UpCmd; - DownCommand DownCmd; + HashCommand HashCmd; + CopyCommand CopyCmd; + DedupCommand DedupCmd; + DeployCommand DeployCmd; + DropCommand DropCmd; + ChunkCommand ChunkCmd; + RunCommand RunCmd; + StatusCommand StatusCmd; + TopCommand TopCmd; + PrintCommand PrintCmd; + PrintPackageCommand PrintPkgCmd; + PsCommand PsCmd; + UpCommand UpCmd; + DownCommand DownCmd; #if ZEN_WITH_TESTS RunTestsCommand RunTestsCmd; @@ -128,6 +131,8 @@ main(int argc, char** argv) {"dedup", &DedupCmd, "Dedup files"}, {"drop", &DropCmd, "Drop cache bucket(s)"}, {"hash", &HashCmd, "Compute file hashes"}, + {"print", &PrintCmd, "Print compact binary object"}, + {"printpackage", &PrintPkgCmd, "Print compact binary package"}, {"run", &RunCmd, "Remote execution"}, {"status", &StatusCmd, "Show zen status"}, {"ps", &PsCmd, "Enumerate running zen server instances"}, diff --git a/zen/zen.vcxproj b/zen/zen.vcxproj index fb0674e87..f31c0bc17 100644 --- a/zen/zen.vcxproj +++ b/zen/zen.vcxproj @@ -99,6 +99,7 @@ <ClCompile Include="cmds\dedup.cpp" /> <ClCompile Include="cmds\deploy.cpp" /> <ClCompile Include="cmds\hash.cpp" /> + <ClCompile Include="cmds\print.cpp" /> <ClCompile Include="cmds\run.cpp" /> <ClCompile Include="cmds\scrub.cpp" /> <ClCompile Include="cmds\status.cpp" /> @@ -114,6 +115,7 @@ <ClInclude Include="cmds\dedup.h" /> <ClInclude Include="cmds\deploy.h" /> <ClInclude Include="cmds\hash.h" /> + <ClInclude Include="cmds\print.h" /> <ClInclude Include="cmds\run.h" /> <ClInclude Include="cmds\scrub.h" /> <ClInclude Include="cmds\status.h" /> diff --git a/zen/zen.vcxproj.filters b/zen/zen.vcxproj.filters index 9002f01c2..d983b413c 100644 --- a/zen/zen.vcxproj.filters +++ b/zen/zen.vcxproj.filters @@ -28,6 +28,7 @@ <ClCompile Include="cmds\up.cpp" /> <ClCompile Include="cmds\cache.cpp" /> <ClCompile Include="cmds\scrub.cpp" /> + <ClCompile Include="cmds\print.cpp" /> </ItemGroup> <ItemGroup> <ClInclude Include="chunk\chunk.h" /> @@ -57,6 +58,7 @@ <ClInclude Include="cmds\up.h" /> <ClInclude Include="cmds\cache.h" /> <ClInclude Include="cmds\scrub.h" /> + <ClInclude Include="cmds\print.h" /> </ItemGroup> <ItemGroup> <Filter Include="cmds"> |