ClickHouse/programs/disks/CommandRead.cpp

53 lines
1.7 KiB
C++
Raw Normal View History

#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"
namespace DB
{
class CommandRead final : public ICommand
{
public:
CommandRead()
{
command_name = "read";
2024-07-04 14:09:43 +00:00
description = "Read a file from `path-from` to `path-to`";
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)")(
2024-07-04 14:09:43 +00:00
"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);
}
2024-05-27 11:44:45 +00:00
void executeImpl(const CommandLineOptions & options, DisksClient & client) override
{
2024-10-15 16:24:06 +00:00
const auto & disk = client.getCurrentDiskWithPath();
2024-05-27 11:44:45 +00:00
String path_from = disk.getRelativeFromRoot(getValueFromCommandLineOptionsThrow<String>(options, "path-from"));
std::optional<String> path_to = getValueFromCommandLineOptionsWithOptional<String>(options, "path-to");
auto in = disk.getDisk()->readFile(path_from, getReadSettings());
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())
{
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);
}
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');
}
2024-05-28 13:17:49 +00:00
out->finalize();
}
};
2024-05-27 11:44:45 +00:00
CommandPtr makeCommandRead()
{
return std::make_shared<DB::CommandRead>();
}
2024-05-27 11:44:45 +00:00
}