ClickHouse
78 строк · 2.3 Кб
1#include "ICommand.h"
2#include <Interpreters/Context.h>
3#include <IO/ReadBufferFromFile.h>
4#include <IO/WriteBufferFromFile.h>
5#include <IO/copyData.h>
6#include <Common/TerminalSize.h>
7
8namespace DB
9{
10
11namespace ErrorCodes
12{
13extern const int BAD_ARGUMENTS;
14}
15
16class CommandRead final : public ICommand
17{
18public:
19CommandRead()
20{
21command_name = "read";
22command_option_description.emplace(createOptionsDescription("Allowed options", getTerminalWidth()));
23description = "Read a file from `FROM_PATH` to `TO_PATH`";
24usage = "read [OPTION]... <FROM_PATH> [<TO_PATH>]";
25command_option_description->add_options()
26("output", po::value<String>(), "file to which we are reading, defaults to `stdout`");
27}
28
29void processOptions(
30Poco::Util::LayeredConfiguration & config,
31po::variables_map & options) const override
32{
33if (options.count("output"))
34config.setString("output", options["output"].as<String>());
35}
36
37void execute(
38const std::vector<String> & command_arguments,
39std::shared_ptr<DiskSelector> & disk_selector,
40Poco::Util::LayeredConfiguration & config) override
41{
42if (command_arguments.size() != 1)
43{
44printHelpMessage();
45throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Bad Arguments");
46}
47
48String disk_name = config.getString("disk", "default");
49
50DiskPtr disk = disk_selector->get(disk_name);
51
52String relative_path = validatePathAndGetAsRelative(command_arguments[0]);
53
54String path_output = config.getString("output", "");
55
56if (!path_output.empty())
57{
58String relative_path_output = validatePathAndGetAsRelative(path_output);
59
60auto in = disk->readFile(relative_path);
61auto out = disk->writeFile(relative_path_output);
62copyData(*in, *out);
63out->finalize();
64}
65else
66{
67auto in = disk->readFile(relative_path);
68std::unique_ptr<WriteBufferFromFileBase> out = std::make_unique<WriteBufferFromFileDescriptor>(STDOUT_FILENO);
69copyData(*in, *out);
70}
71}
72};
73}
74
75std::unique_ptr <DB::ICommand> makeCommandRead()
76{
77return std::make_unique<DB::CommandRead>();
78}
79