Skip to content

Commit

Permalink
Merge e6bbde2 into af411bb
Browse files Browse the repository at this point in the history
  • Loading branch information
Enjection authored Jan 10, 2025
2 parents af411bb + e6bbde2 commit b02f60f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
24 changes: 21 additions & 3 deletions ydb/public/lib/ydb_cli/commands/ydb_admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,31 @@
namespace NYdb {
namespace NConsoleClient {

class TCommandNode : public TClientCommandTree {
public:
TCommandNode()
: TClientCommandTree("node", {}, "Node-wide administration")
{}
};

class TCommandDatabase : public TClientCommandTree {
public:
TCommandDatabase()
: TClientCommandTree("database", {}, "Database-wide administration")
{}
};

TCommandAdmin::TCommandAdmin()
: TClientCommandTree("admin", {}, "Administrative cluster operations")
{
AddCommand(std::make_unique<NDynamicConfig::TCommandConfig>());
AddCommand(std::make_unique<NDynamicConfig::TCommandVolatileConfig>());
AddCommand(std::make_unique<NStorageConfig::TCommandStorageConfig>());
MarkDangerous();
UseOnlyExplicitProfile();
AddHiddenCommand(std::make_unique<NDynamicConfig::TCommandConfig>());
AddHiddenCommand(std::make_unique<NDynamicConfig::TCommandVolatileConfig>());
AddHiddenCommand(std::make_unique<NStorageConfig::TCommandStorageConfig>());
AddCommand(std::make_unique<NCluster::TCommandCluster>());
AddCommand(std::make_unique<TCommandNode>());
AddCommand(std::make_unique<TCommandDatabase>());
}

}
Expand Down
22 changes: 16 additions & 6 deletions ydb/public/lib/ydb_cli/commands/ydb_root_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TClientCommandRootCommon::TClientCommandRootCommon(const TString& name, const TC
, Settings(settings)
{
ValidateSettings();
AddCommand(std::make_unique<TCommandAdmin>());
AddDangerousCommand(std::make_unique<TCommandAdmin>());
AddCommand(std::make_unique<TCommandAuth>());
AddCommand(std::make_unique<TCommandDiscovery>());
AddCommand(std::make_unique<TCommandScheme>());
Expand All @@ -54,6 +54,7 @@ TClientCommandRootCommon::TClientCommandRootCommon(const TString& name, const TC
AddCommand(std::make_unique<TCommandTopic>());
AddCommand(std::make_unique<TCommandWorkload>());
AddCommand(std::make_unique<TCommandDebug>());
PropagateFlags(TCommandFlags{.Dangerous = false, .OnlyExplicitProfile = false});
}

void TClientCommandRootCommon::ValidateSettings() {
Expand Down Expand Up @@ -328,6 +329,11 @@ void TClientCommandRootCommon::Config(TConfig& config) {
}

void TClientCommandRootCommon::Parse(TConfig& config) {
TClientCommandRootBase::Parse(config);
config.VerbosityLevel = std::min(static_cast<TConfig::EVerbosityLevel>(VerbosityLevel), TConfig::EVerbosityLevel::DEBUG);
}

void TClientCommandRootCommon::PostPrepare(TConfig& config) {
if (ProfileFile.empty()) {
config.ProfileFile = TStringBuilder() << HomeDir << '/' << Settings.YdbDir << "/config/config.yaml";
} else {
Expand All @@ -339,12 +345,12 @@ void TClientCommandRootCommon::Parse(TConfig& config) {
ProfileManager = CreateProfileManager(config.ProfileFile);
ParseProfile();

TClientCommandRootBase::Parse(config);
ParseDatabase(config);
ParseCaCerts(config);
ParseIamEndpoint(config);

config.VerbosityLevel = std::min(static_cast<TConfig::EVerbosityLevel>(VerbosityLevel), TConfig::EVerbosityLevel::DEBUG);
ParseCredentials(config);
ParseAddress(config);
}

namespace {
Expand Down Expand Up @@ -435,7 +441,7 @@ void TClientCommandRootCommon::ParseAddress(TConfig& config) {
return;
}
// Priority 3. Active profile (if --profile option is not specified)
if (TryGetParamFromProfile("endpoint", ProfileManager->GetActiveProfile(), false, getAddress)) {
if (!config.OnlyExplicitProfile && TryGetParamFromProfile("endpoint", ProfileManager->GetActiveProfile(), false, getAddress)) {
return;
}
}
Expand Down Expand Up @@ -519,7 +525,7 @@ void TClientCommandRootCommon::ParseDatabase(TConfig& config) {
return;
}
// Priority 3. Active profile (if --profile option is not specified)
if (TryGetParamFromProfile("database", ProfileManager->GetActiveProfile(), false, getDatabase)) {
if (!config.OnlyExplicitProfile && TryGetParamFromProfile("database", ProfileManager->GetActiveProfile(), false, getDatabase)) {
return;
}
}
Expand Down Expand Up @@ -574,11 +580,15 @@ void TClientCommandRootCommon::Validate(TConfig& config) {
throw TMisuseException() << errors;
}
}

// TODO: Maybe NeedToConnect doesn't always mean that we don't need to check endpoint and database
// TODO: Now we supplying only one error while it is possible to return all errors at once,
// maybe even erros from nested command's validate
if (!config.NeedToConnect) {
return;
}

if (config.Address.empty()) {
if (config.Address.empty() && config.AllowEmptyAddress) {
throw TMisuseException() << "Missing required option 'endpoint'.";
}

Expand Down
1 change: 1 addition & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_root_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TClientCommandRootCommon : public TClientCommandRootBase {
public:
TClientCommandRootCommon(const TString& name, const TClientSettings& settings);
void Config(TConfig& config) override;
void PostPrepare(TConfig& config) override;
void Parse(TConfig& config) override;
void ParseAddress(TConfig& config) override;
void ParseCredentials(TConfig& config) override;
Expand Down
23 changes: 22 additions & 1 deletion ydb/public/lib/ydb_cli/common/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ void TClientCommand::CheckForExecutableOptions(TConfig& config) {

void TClientCommand::Config(TConfig& config) {
config.Opts = &Opts;
config.OnlyExplicitProfile = OnlyExplicitProfile;
TStringStream stream;
NColorizer::TColors colors = NColorizer::AutoColors(Cout);
stream << Endl << Endl
Expand Down Expand Up @@ -251,9 +252,14 @@ void TClientCommand::Prepare(TConfig& config) {
Parse(config);
}

void TClientCommand::PostPrepare(TConfig& config) {
Y_UNUSED(config);
}

int TClientCommand::ValidateAndRun(TConfig& config) {
config.Opts = &Opts;
config.ParseResult = ParseResult.get();
PostPrepare(config);
Validate(config);
return Run(config);
}
Expand Down Expand Up @@ -316,7 +322,7 @@ void TClientCommand::RenderOneCommandDescription(
prefix = "└─ ";
}
TString line = prefix + Name;
stream << prefix << colors.BoldColor() << Name << colors.OldColor();
stream << prefix << (Dangerous ? colors.Red() : "") << colors.BoldColor() << Name << colors.OldColor();
if (!Description.empty()) {
int namePartLength = GetNumberOfUTF8Chars(line);
if (namePartLength < DESCRIPTION_ALIGNMENT)
Expand All @@ -339,6 +345,15 @@ void TClientCommand::RenderOneCommandDescription(

void TClientCommand::Hide() {
Hidden = true;
Visible = false;
}

void TClientCommand::MarkDangerous() {
Dangerous = true;
}

void TClientCommand::UseOnlyExplicitProfile() {
OnlyExplicitProfile = true;
}

TClientCommandTree::TClientCommandTree(const TString& name, const std::initializer_list<TString>& aliases, const TString& description)
Expand All @@ -361,6 +376,12 @@ void TClientCommandTree::AddHiddenCommand(std::unique_ptr<TClientCommand> comman
AddCommand(std::move(command));
}

void TClientCommandTree::AddDangerousCommand(std::unique_ptr<TClientCommand> command) {
command->MarkDangerous();
command->UseOnlyExplicitProfile();
AddCommand(std::move(command));
}

void TClientCommandTree::Config(TConfig& config) {
TClientCommand::Config(config);
SetFreeArgs(config);
Expand Down
26 changes: 24 additions & 2 deletions ydb/public/lib/ydb_cli/common/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
namespace NYdb {
namespace NConsoleClient {

struct TCommandFlags {
bool Dangerous = false;
bool OnlyExplicitProfile = false;
};

class TClientCommand {
public:
static bool TIME_REQUESTS; // measure time of requests
Expand All @@ -25,6 +30,9 @@ class TClientCommand {
TVector<TString> Aliases;
TString Description;
bool Visible = true;
bool Hidden = false;
bool Dangerous = false;
bool OnlyExplicitProfile = false;
const TClientCommand* Parent;
NLastGetopt::TOpts Opts;
TString Argument;
Expand Down Expand Up @@ -137,6 +145,8 @@ class TClientCommand {
bool NeedToCheckForUpdate = true;
bool ForceVersionCheck = false;
bool AllowEmptyDatabase = false;
bool AllowEmptyAddress = false;
bool OnlyExplicitProfile = false;

TCredentialsGetter CredentialsGetter;

Expand Down Expand Up @@ -307,7 +317,12 @@ class TClientCommand {

virtual int Process(TConfig& config);
virtual void Prepare(TConfig& config);
virtual void PostPrepare(TConfig& config);
virtual int ValidateAndRun(TConfig& config);
virtual void PropagateFlags(const TCommandFlags& flags) {
Dangerous |= flags.Dangerous;
OnlyExplicitProfile |= flags.OnlyExplicitProfile;
}

enum RenderEntryType {
BEGIN,
Expand All @@ -322,6 +337,8 @@ class TClientCommand {
);

void Hide();
void MarkDangerous();
void UseOnlyExplicitProfile();

protected:
virtual void Config(TConfig& config);
Expand All @@ -342,14 +359,14 @@ class TClientCommand {
void CheckForExecutableOptions(TConfig& config);

constexpr static int DESCRIPTION_ALIGNMENT = 28;
bool Hidden = false;
};

class TClientCommandTree : public TClientCommand {
public:
TClientCommandTree(const TString& name, const std::initializer_list<TString>& aliases = std::initializer_list<TString>(), const TString& description = TString());
void AddCommand(std::unique_ptr<TClientCommand> command);
void AddHiddenCommand(std::unique_ptr<TClientCommand> command);
void AddDangerousCommand(std::unique_ptr<TClientCommand> command);
virtual void Prepare(TConfig& config) override;
void RenderCommandsDescription(
TStringStream& stream,
Expand All @@ -363,10 +380,15 @@ class TClientCommandTree : public TClientCommand {
virtual void SaveParseResult(TConfig& config) override;
virtual void Parse(TConfig& config) override;
virtual int Run(TConfig& config) override;
virtual void PropagateFlags(const TCommandFlags& flags) override {
TClientCommand::PropagateFlags(flags);
for (auto& [_, cmd] : SubCommands) {
cmd->PropagateFlags(TCommandFlags{.Dangerous = Dangerous, .OnlyExplicitProfile = OnlyExplicitProfile});
}
}

TClientCommand* SelectedCommand;

private:
bool HasOptionsToShow();

TMap<TString, std::unique_ptr<TClientCommand>> SubCommands;
Expand Down
2 changes: 0 additions & 2 deletions ydb/public/lib/ydb_cli/common/root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ void TClientCommandRootBase::SetCustomUsage(TConfig& config) {

void TClientCommandRootBase::Parse(TConfig& config) {
TClientCommandTree::Parse(config);
ParseCredentials(config);
ParseAddress(config);

TClientCommand::TIME_REQUESTS = TimeRequests;
TClientCommand::PROGRESS_REQUESTS = ProgressRequests;
Expand Down

0 comments on commit b02f60f

Please sign in to comment.