2022-12-08 17:20:54 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <IO/WriteBufferFromFile.h>
|
|
|
|
#include <IO/copyData.h>
|
2024-05-27 11:44:45 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2022-12-08 17:53:05 +00:00
|
|
|
#include <Common/TerminalSize.h>
|
2024-05-27 11:44:45 +00:00
|
|
|
#include "ICommand.h"
|
2022-04-08 07:52:16 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-12-08 17:20:54 +00:00
|
|
|
class CommandRead final : public ICommand
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CommandRead()
|
|
|
|
{
|
|
|
|
command_name = "read";
|
2023-08-29 12:25:04 +00:00
|
|
|
description = "Read a file from `FROM_PATH` to `TO_PATH`";
|
2024-05-28 13:17:49 +00:00
|
|
|
options_description.add_options()("path-from", po::value<String>(), "file from which we are reading (mandatory, positional)")(
|
|
|
|
"path-to", po::value<String>(), "file to which we are writing, , defaults to `stdout`");
|
2024-05-27 11:44:45 +00:00
|
|
|
positional_options_description.add("path-from", 1);
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
void executeImpl(const CommandLineOptions & options, DisksClient & client) override
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
2024-05-27 11:44:45 +00:00
|
|
|
auto disk = client.getCurrentDiskWithPath();
|
|
|
|
String path_from = disk.getRelativeFromRoot(getValueFromCommandLineOptionsThrow<String>(options, "path-from"));
|
2024-05-28 13:17:49 +00:00
|
|
|
std::cerr << path_from << std::endl;
|
2024-05-27 11:44:45 +00:00
|
|
|
std::optional<String> path_to = getValueFromCommandLineOptionsWithOptional<String>(options, "path-to");
|
2022-04-08 07:52:16 +00:00
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
auto in = disk.getDisk()->readFile(path_from);
|
2024-05-28 13:17:49 +00:00
|
|
|
std::unique_ptr<WriteBufferFromFileBase> out = {};
|
2024-05-27 11:44:45 +00:00
|
|
|
if (path_to.has_value())
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
2024-05-27 11:44:45 +00:00
|
|
|
String relative_path_to = disk.getRelativeFromRoot(path_to.value());
|
2024-05-28 13:17:49 +00:00
|
|
|
out = disk.getDisk()->writeFile(relative_path_to);
|
2022-06-02 17:58:58 +00:00
|
|
|
copyData(*in, *out);
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-28 13:17:49 +00:00
|
|
|
out = std::make_unique<WriteBufferFromFileDescriptor>(STDOUT_FILENO);
|
2022-06-02 17:58:58 +00:00
|
|
|
copyData(*in, *out);
|
2024-05-28 13:17:49 +00:00
|
|
|
out->write('\n');
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
2024-05-28 13:17:49 +00:00
|
|
|
out->finalize();
|
2022-04-08 07:52:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-27 11:44:45 +00:00
|
|
|
CommandPtr makeCommandRead()
|
2022-04-08 07:52:16 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<DB::CommandRead>();
|
|
|
|
}
|
2024-05-27 11:44:45 +00:00
|
|
|
|
|
|
|
}
|