ClickHouse

Форк
0
/
CommandRead.cpp 
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

8
namespace DB
9
{
10

11
namespace ErrorCodes
12
{
13
    extern const int BAD_ARGUMENTS;
14
}
15

16
class CommandRead final : public ICommand
17
{
18
public:
19
    CommandRead()
20
    {
21
        command_name = "read";
22
        command_option_description.emplace(createOptionsDescription("Allowed options", getTerminalWidth()));
23
        description = "Read a file from `FROM_PATH` to `TO_PATH`";
24
        usage = "read [OPTION]... <FROM_PATH> [<TO_PATH>]";
25
        command_option_description->add_options()
26
            ("output", po::value<String>(), "file to which we are reading, defaults to `stdout`");
27
    }
28

29
    void processOptions(
30
        Poco::Util::LayeredConfiguration & config,
31
        po::variables_map & options) const override
32
    {
33
        if (options.count("output"))
34
            config.setString("output", options["output"].as<String>());
35
    }
36

37
    void execute(
38
        const std::vector<String> & command_arguments,
39
       std::shared_ptr<DiskSelector> & disk_selector,
40
        Poco::Util::LayeredConfiguration & config) override
41
    {
42
        if (command_arguments.size() != 1)
43
        {
44
            printHelpMessage();
45
            throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Bad Arguments");
46
        }
47

48
        String disk_name = config.getString("disk", "default");
49

50
        DiskPtr disk = disk_selector->get(disk_name);
51

52
        String relative_path = validatePathAndGetAsRelative(command_arguments[0]);
53

54
        String path_output = config.getString("output", "");
55

56
        if (!path_output.empty())
57
        {
58
            String relative_path_output = validatePathAndGetAsRelative(path_output);
59

60
            auto in = disk->readFile(relative_path);
61
            auto out = disk->writeFile(relative_path_output);
62
            copyData(*in, *out);
63
            out->finalize();
64
        }
65
        else
66
        {
67
            auto in = disk->readFile(relative_path);
68
            std::unique_ptr<WriteBufferFromFileBase> out = std::make_unique<WriteBufferFromFileDescriptor>(STDOUT_FILENO);
69
            copyData(*in, *out);
70
        }
71
    }
72
};
73
}
74

75
std::unique_ptr <DB::ICommand> makeCommandRead()
76
{
77
    return std::make_unique<DB::CommandRead>();
78
}
79

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.