ClickHouse/programs/disks/ICommand.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
3.4 KiB
C++
Raw Normal View History

2022-12-08 17:54:24 +00:00
#pragma once
2024-05-27 11:44:45 +00:00
#include <optional>
2023-11-30 03:09:55 +00:00
#include <Disks/DiskSelector.h>
2024-05-27 11:44:45 +00:00
#include <Disks/IDisk.h>
#include <boost/program_options.hpp>
2024-05-27 11:44:45 +00:00
#include <Parsers/IAST.h>
#include <Poco/Util/Application.h>
2024-05-27 11:44:45 +00:00
#include "Common/Exception.h"
#include <Common/Config/ConfigProcessor.h>
#include <Parsers/parseIdentifierOrStringLiteral.h>
#include <boost/program_options/positional_options.hpp>
2024-05-27 11:44:45 +00:00
#include "DisksApp.h"
#include "DisksClient.h"
#include "ICommand_fwd.h"
namespace DB
{
2024-05-27 11:44:45 +00:00
// namespace po = boost::program_options;
namespace po = boost::program_options;
2024-05-27 11:44:45 +00:00
using ProgramOptionsDescription = po::options_description;
using PositionalProgramOptionsDescription = po::positional_options_description;
using CommandLineOptions = po::variables_map;
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
class ICommand
{
public:
2024-05-27 11:44:45 +00:00
explicit ICommand() = default;
2022-06-16 17:38:33 +00:00
virtual ~ICommand() = default;
2022-06-16 17:38:33 +00:00
2024-05-27 11:44:45 +00:00
void execute(const Strings & commands, DisksClient & client);
2024-05-27 11:44:45 +00:00
virtual void executeImpl(const CommandLineOptions & options, DisksClient & client) = 0;
2022-06-21 14:40:22 +00:00
2024-05-27 11:44:45 +00:00
CommandLineOptions processCommandLineArguments(const Strings & commands);
2024-05-27 11:44:45 +00:00
void exit() { options_parsed = false; }
2022-06-21 14:40:22 +00:00
protected:
2024-05-27 11:44:45 +00:00
template <typename T>
static T getValueFromCommandLineOptions(const CommandLineOptions & options, const String & name)
{
try
{
return options[name].as<T>();
}
catch (...)
{
throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Argument {} has wrong type and can't be parsed", name);
}
}
template <typename T>
static T getValueFromCommandLineOptionsThrow(const CommandLineOptions & options, const String & name)
{
if (options.count(name))
{
return getValueFromCommandLineOptions<T>(options, name);
}
else
{
throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Mandatory argument {} is missing", name);
}
}
template <typename T>
static T getValueFromCommandLineOptionsWithDefault(const CommandLineOptions & options, const String & name, const T & default_value)
{
if (options.count(name))
{
return getValueFromCommandLineOptions<T>(options, name);
}
else
{
return default_value;
}
}
template <typename T>
static std::optional<T> getValueFromCommandLineOptionsWithOptional(const CommandLineOptions & options, const String & name)
{
if (options.count(name))
{
return std::optional{getValueFromCommandLineOptions<T>(options, name)};
}
else
{
return std::nullopt;
}
}
DiskWithPath & getDiskWithPath(DisksClient & client, const CommandLineOptions & options, const String & name);
public:
String command_name;
String description;
2024-05-27 11:44:45 +00:00
ProgramOptionsDescription options_description;
protected:
2024-05-27 11:44:45 +00:00
PositionalProgramOptionsDescription positional_options_description;
2024-05-27 11:44:45 +00:00
private:
bool options_parsed{};
};
2022-12-08 17:53:05 +00:00
DB::CommandPtr makeCommandCopy();
DB::CommandPtr makeCommandListDisks();
2024-05-27 11:44:45 +00:00
DB::CommandPtr makeCommandList();
DB::CommandPtr makeCommandChangeDirectory();
DB::CommandPtr makeCommandLink();
2022-12-08 17:53:05 +00:00
DB::CommandPtr makeCommandMove();
DB::CommandPtr makeCommandRead();
DB::CommandPtr makeCommandRemove();
DB::CommandPtr makeCommandWrite();
DB::CommandPtr makeCommandMkDir();
2024-02-14 02:31:31 +00:00
DB::CommandPtr makeCommandPackedIO();
2024-05-27 11:44:45 +00:00
DB::CommandPtr makeCommandSwitchDisk();
}