Skip to content

#9056 Format module commands/interactive #15212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions ydb/public/lib/ydb_cli/commands/interactive/interactive_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#include <ydb/public/lib/ydb_cli/commands/ydb_service_table.h>
#include <ydb/public/lib/ydb_cli/commands/ydb_sql.h>

namespace NYdb {
namespace NConsoleClient {

namespace NYdb::NConsoleClient {

namespace {

Expand Down Expand Up @@ -43,13 +41,14 @@ class Lexer {

private:
std::string_view Input;
const char * Position = nullptr;
const char* Position = nullptr;
};

Lexer::Lexer(std::string_view input)
: Input(input)
, Position(Input.data())
{}
{
}

std::optional<Token> Lexer::GetNextToken() {
while (Position < Input.end() && std::isspace(*Position)) {
Expand All @@ -60,7 +59,7 @@ std::optional<Token> Lexer::GetNextToken() {
return {};
}

const char * tokenStart = Position;
const char* tokenStart = Position;
if (IsSeparatedTokenSymbol(*Position)) {
++Position;
} else {
Expand Down Expand Up @@ -92,7 +91,7 @@ struct InteractiveCLIState {
NTable::ECollectQueryStatsMode CollectStatsMode = NTable::ECollectQueryStatsMode::None;
};

std::optional<NTable::ECollectQueryStatsMode> TryParseCollectStatsMode(const std::vector<Token> & tokens) {
std::optional<NTable::ECollectQueryStatsMode> TryParseCollectStatsMode(const std::vector<Token>& tokens) {
size_t tokensSize = tokens.size();

if (tokensSize > 4) {
Expand All @@ -107,7 +106,7 @@ std::optional<NTable::ECollectQueryStatsMode> TryParseCollectStatsMode(const std
return statsMode;
}

void ParseSetCommand(const std::vector<Token> & tokens, InteractiveCLIState & interactiveCLIState) {
void ParseSetCommand(const std::vector<Token>& tokens, InteractiveCLIState& interactiveCLIState) {
if (tokens.size() == 1) {
Cerr << "Missing variable name for \"SET\" special command." << Endl;
} else if (tokens.size() == 2 || tokens[2].data != "=") {
Expand All @@ -123,12 +122,13 @@ void ParseSetCommand(const std::vector<Token> & tokens, InteractiveCLIState & in
}
}

}
} // namespace

TInteractiveCLI::TInteractiveCLI(TClientCommand::TConfig & config, std::string prompt)
TInteractiveCLI::TInteractiveCLI(TClientCommand::TConfig& config, std::string prompt)
: Config(config)
, Prompt(std::move(prompt))
{}
{
}

void TInteractiveCLI::Run() {
TFsPath homeDirPath(HomeDir);
Expand All @@ -139,7 +139,7 @@ void TInteractiveCLI::Run() {

while (auto lineOptional = lineReader->ReadLine())
{
auto & line = *lineOptional;
auto& line = *lineOptional;
if (line.empty()) {
continue;
}
Expand Down Expand Up @@ -184,17 +184,16 @@ void TInteractiveCLI::Run() {
sqlCommand.SetCollectStatsMode(std::move(queryStatsMode));
sqlCommand.SetSyntax("yql");
sqlCommand.Run(Config);
} catch (NStatusHelpers::TYdbErrorException &error) {
} catch (NStatusHelpers::TYdbErrorException& error) {
Cerr << error;
} catch (yexception & error) {
} catch (yexception& error) {
Cerr << error;
} catch (std::exception & error) {
} catch (std::exception& error) {
Cerr << error.what();
}
}

std::cout << std::endl << "Bye" << std::endl;
}

}
}
} // namespace NYdb::NConsoleClient
13 changes: 5 additions & 8 deletions ydb/public/lib/ydb_cli/commands/interactive/interactive_cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

#include <ydb/public/lib/ydb_cli/common/command.h>

namespace NYdb {
namespace NConsoleClient {
namespace NYdb::NConsoleClient {

class TInteractiveCLI
{
class TInteractiveCLI {
public:
TInteractiveCLI(TClientCommand::TConfig & config, std::string prompt);
TInteractiveCLI(TClientCommand::TConfig& config, std::string prompt);

void Run();

private:
TClientCommand::TConfig & Config;
TClientCommand::TConfig& Config;
std::string Prompt;
};

}
}
} // namespace NYdb::NConsoleClient
41 changes: 20 additions & 21 deletions ydb/public/lib/ydb_cli/commands/interactive/line_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,26 @@

#include <contrib/restricted/patched/replxx/include/replxx.hxx>

namespace NYdb {
namespace NConsoleClient {
namespace NYdb::NConsoleClient {

namespace
{
namespace {

class FileHandlerLockGuard
{
class FileHandlerLockGuard {
public:
FileHandlerLockGuard(TFileHandle * handle)
FileHandlerLockGuard(TFileHandle* handle)
: Handle(handle)
{}
{
}

~FileHandlerLockGuard() {
Handle->Flock(LOCK_UN);
}

private:
TFileHandle * Handle = nullptr;
TFileHandle* Handle = nullptr;
};

std::optional<FileHandlerLockGuard> LockFile(TFileHandle & fileHandle) {
std::optional<FileHandlerLockGuard> LockFile(TFileHandle& fileHandle) {
if (fileHandle.Flock(LOCK_EX) != 0) {
return {};
}
Expand All @@ -42,15 +41,14 @@ replxx::Replxx::Color ReplxxColorOf(NSQLComplete::ECandidateKind /* kind */) {
return replxx::Replxx::Color::DEFAULT;
}

class TLineReader : public ILineReader
{
class TLineReader: public ILineReader {
public:
TLineReader(std::string prompt, std::string historyFilePath);

std::optional<std::string> ReadLine() override;

private:
void AddToHistory(const std::string & line);
void AddToHistory(const std::string& line);

std::string Prompt;
std::string HistoryFilePath;
Expand All @@ -67,7 +65,7 @@ TLineReader::TLineReader(std::string prompt, std::string historyFilePath)
{
Rx.install_window_change_handler();

auto completion_callback = [this](const std::string & prefix, size_t contextLen) {
auto completion_callback = [this](const std::string& prefix, size_t contextLen) {
auto completion = CompletionEngine->Complete({
.Text = prefix,
.CursorPosition = prefix.length(),
Expand Down Expand Up @@ -114,26 +112,28 @@ TLineReader::TLineReader(std::string prompt, std::string historyFilePath)

std::optional<std::string> TLineReader::ReadLine() {
while (true) {
const auto * status = Rx.input(Prompt.c_str());
const auto* status = Rx.input(Prompt.c_str());

if (status == nullptr) {
if (errno == EAGAIN)
if (errno == EAGAIN) {
continue;
}

return {};
}

std::string line = status;
while (!line.empty() && std::isspace(line.back()))
while (!line.empty() && std::isspace(line.back())) {
line.pop_back();
}

AddToHistory(line);

return line;
}
}

void TLineReader::AddToHistory(const std::string & line) {
void TLineReader::AddToHistory(const std::string& line) {
Rx.history_add(line);

auto fileLockGuard = LockFile(HistoryFileHandle);
Expand All @@ -147,11 +147,10 @@ void TLineReader::AddToHistory(const std::string & line) {
}
}

}
} // namespace

std::unique_ptr<ILineReader> CreateLineReader(std::string prompt, std::string historyFilePath) {
return std::make_unique<TLineReader>(std::move(prompt), std::move(historyFilePath));
}

}
}
} // namespace NYdb::NConsoleClient
7 changes: 2 additions & 5 deletions ydb/public/lib/ydb_cli/commands/interactive/line_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
#include <optional>
#include <string>

namespace NYdb {
namespace NConsoleClient {
namespace NYdb::NConsoleClient {

class ILineReader {
public:
virtual std::optional<std::string> ReadLine() = 0;

virtual ~ILineReader() = default;

};

std::unique_ptr<ILineReader> CreateLineReader(std::string prompt, std::string historyFilePath);

}
}
} // namespace NYdb::NConsoleClient
4 changes: 2 additions & 2 deletions ydb/public/lib/ydb_cli/commands/interactive/yql_highlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ namespace NYdb {
TParsedTokenList Tokens;
};

}
}
} // namespace NConsoleClient
} // namespace NYdb
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,4 @@ Y_UNIT_TEST_SUITE(YqlHighlightTests) {
" "
"kkkkkk no ovvvo ss kk kkkkko"));
}
}
} // Y_UNIT_TEST_SUITE(YqlHighlightTests)
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ namespace NYdb {
{
}

}
}
} // namespace NConsoleClient
} // namespace NYdb
4 changes: 2 additions & 2 deletions ydb/public/lib/ydb_cli/commands/interactive/yql_position.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ namespace NYdb {
TVector<ui32> SymbolsCountBeforeLine;
};

}
}
} // namespace NConsoleClient
} // namespace NYdb