Skip to content
Snippets Groups Projects
Commit 7fbb0d01 authored by Вартан Бабаян's avatar Вартан Бабаян :dart:
Browse files

fix: add EOF when process input from files

parent 0bf5381c
No related branches found
No related tags found
1 merge request!1390add EOF as second delimiter along with ;
use nix::unistd::isatty;
use std::collections::VecDeque;
use std::env;
use std::fs::read_to_string;
......@@ -69,6 +70,7 @@ pub struct Console<H: Helper> {
// Queue of separated by delimiter statements
separated_statements: VecDeque<String>,
uncompleted_statement: String,
eof_received: bool,
}
impl<T: Helper> Console<T> {
......@@ -79,14 +81,14 @@ impl<T: Helper> Console<T> {
fn handle_special_command(&mut self, command: &str) -> Result<ControlFlow<Command>> {
match command {
"\\e" => self.open_external_editor(),
"\\help" | "\\h" => Ok(ControlFlow::Break(Command::Control(
"\\e" | "\\e;" => self.open_external_editor(),
"\\help" | "\\h" | "\\help;" | "\\h;" => Ok(ControlFlow::Break(Command::Control(
SpecialCommand::PrintHelp,
))),
"\\lua" => Ok(ControlFlow::Break(Command::Control(
"\\lua" | "\\lua;" => Ok(ControlFlow::Break(Command::Control(
SpecialCommand::SwitchLanguageToLua,
))),
"\\sql" => Ok(ControlFlow::Break(Command::Control(
"\\sql" | "\\sql;" => Ok(ControlFlow::Break(Command::Control(
SpecialCommand::SwitchLanguageToSql,
))),
_ => self.handle_parsed_command(command),
......@@ -190,8 +192,27 @@ impl<T: Helper> Console<T> {
Ok(Some(command))
}
fn process_command(&mut self) {
if let Some(ref delimiter) = self.delimiter {
while let Some((separated_part, tail)) =
self.uncompleted_statement.split_once(delimiter)
{
self.separated_statements.push_back(separated_part.into());
self.uncompleted_statement = tail.into();
}
} else {
self.separated_statements
.push_back(std::mem::take(&mut self.uncompleted_statement));
}
}
pub fn read(&mut self) -> Result<Option<Command>> {
loop {
if self.eof_received {
self.write("Bye");
return Ok(None);
}
while let Some(separated_input) = self.separated_statements.pop_front() {
let processed = {
if separated_input.starts_with(Self::SPECIAL_COMMAND_PREFIX) {
......@@ -217,10 +238,7 @@ impl<T: Helper> Console<T> {
match readline {
Ok(line) => {
if line.is_empty() {
return Ok(Some(Command::Expression(line)));
}
// process special command with no need for delimiter
if line.starts_with(Self::SPECIAL_COMMAND_PREFIX) {
let processed = self.handle_special_command(&line)?;
......@@ -230,24 +248,22 @@ impl<T: Helper> Console<T> {
}
} else {
self.uncompleted_statement += &line;
if let Some(ref delimiter) = self.delimiter {
while let Some((separated_part, tail)) =
self.uncompleted_statement.split_once(delimiter)
{
self.separated_statements.push_back(separated_part.into());
self.uncompleted_statement = tail.into();
}
} else {
self.separated_statements
.push_back(std::mem::take(&mut self.uncompleted_statement));
}
self.process_command();
}
}
Err(ReadlineError::Interrupted) => {
self.write("CTRL+C");
}
Err(ReadlineError::Eof) => {
let is_terminal = isatty(0).unwrap_or(false);
if !is_terminal && !self.uncompleted_statement.is_empty() {
self.eof_received = true;
return Ok(Some(Command::Expression(std::mem::take(
&mut self.uncompleted_statement,
))));
}
self.write("Bye");
return Ok(None);
}
......@@ -304,6 +320,7 @@ impl Console<LuaHelper> {
delimiter: Some(DELIMITER.to_string()),
separated_statements: VecDeque::new(),
uncompleted_statement: String::new(),
eof_received: false,
})
}
}
......@@ -318,6 +335,7 @@ impl Console<()> {
delimiter: Some(DELIMITER.to_string()),
separated_statements: VecDeque::new(),
uncompleted_statement: String::new(),
eof_received: false,
})
}
}
......@@ -34,14 +34,14 @@ def test_connect_ux(cluster: Cluster):
cli.expect_exact("picodata> ")
# sql console doesn't know about language switching
cli.sendline("\\lua")
cli.sendline("\\lua;")
cli.expect_exact("Unknown special sequence")
cli.sendline("\\sql")
cli.expect_exact("Unknown special sequence")
# for not registried command nothing can happen
cli.sendline("\\lya")
cli.sendline("\\lya;")
cli.expect_exact("Unknown special sequence")
cli.sendline("\\scl")
cli.expect_exact("Unknown special sequence")
......@@ -114,7 +114,7 @@ def test_admin_ux(cluster: Cluster):
cli.sendline("\\lua")
cli.expect_exact("Language switched to Lua")
cli.sendline("\\sql")
cli.sendline("\\sql;")
cli.expect_exact("Language switched to SQL")
# for not registried command nothing happend
......@@ -133,7 +133,7 @@ def test_admin_ux(cluster: Cluster):
cli.expect_exact("Language switched to SQL")
# nothing happens on completion in SQL mode
cli.sendline("\\sql")
cli.sendline("\\sql;")
cli.expect_exact("Language switched to SQL")
cli.sendline("\t\t")
cli.expect_exact("picodata> ")
......@@ -431,9 +431,11 @@ def test_cat_file_to_picodata_admin_stdin(cluster: Cluster):
CREATE TABLE ids (id INTEGER NOT NULL, PRIMARY KEY(id))
USING MEMTX
DISTRIBUTED BY (id);
INSERT INTO ids
VALUES(1);
SELECT * FROM ids;
SELECT * FROM ids
""",
)
......@@ -462,7 +464,7 @@ def test_cat_file_to_picodata_connect_stdin(cluster: Cluster):
[cluster.binary_path, "admin", f"{i1.data_dir}/admin.sock"],
input=b"""\
CREATE USER "alice" WITH PASSWORD 'T0psecret';
GRANT CREATE TABLE TO "alice";
GRANT CREATE TABLE TO "alice"
""",
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment