Skip to content

Commit

Permalink
libsql-shell: add -cmd option
Browse files Browse the repository at this point in the history
  • Loading branch information
pjhades committed Oct 26, 2023
1 parent d08eeee commit d0de602
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions libsql-shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ struct Cli {
/// Print inputs before execution
#[arg(long, default_value = "false")]
echo: bool,
/// Run "COMMAND" before reading stdin
#[arg(long = "cmd", action = clap::ArgAction::Append)]
command: Option<Vec<String>>,
}

struct StrStatements {
Expand Down Expand Up @@ -80,6 +83,7 @@ struct Shell {
width: [usize; 5],
filename: PathBuf,

commands_before_repl: Option<Vec<String>>,
colseparator: String,
rowseparator: String,
main_prompt: String,
Expand Down Expand Up @@ -189,14 +193,34 @@ impl Shell {
width: [0; 5],
null_value: String::new(),
filename: PathBuf::from(args.db_path.unwrap_or_else(|| ":memory:".to_string())),
commands_before_repl: args.command,
colseparator: String::new(),
rowseparator: String::new(),
main_prompt: "libsql> ".to_string(),
continuation_prompt: " ...> ".to_string(),
}
}

fn parse_and_run_command(&mut self, line: &str) {
// split line on whitespace, but not inside quotes.
let mut split = vec![];
for (i, chunk) in line.split_terminator(&['\'', '"']).enumerate() {
if i % 2 != 0 {
split.push(chunk);
} else {
split.extend(chunk.split_whitespace())
}
}
self.run_command(split[0], &split[1..]);
}

fn run(mut self, rl: &mut Editor<ShellHelper, FileHistory>) -> Result<()> {
if let Some(commands) = self.commands_before_repl.take() {
for command in commands {
self.parse_and_run_command(&command);
}
}

let mut leftovers = String::new();
loop {
let prompt = if leftovers.is_empty() {
Expand All @@ -219,16 +243,7 @@ impl Shell {
writeln!(self.out, "{}", line).unwrap();
}
if line.starts_with('.') {
// split line on whitespace, but not inside quotes.
let mut split = vec![];
for (i, chunk) in line.split_terminator(&['\'', '"']).enumerate() {
if i % 2 != 0 {
split.push(chunk);
} else {
split.extend(chunk.split_whitespace())
}
}
self.run_command(split[0], &split[1..]);
self.parse_and_run_command(&line);
} else {
for str_statement in get_str_statements(line) {
let table = self.run_statement(str_statement, (), false);
Expand Down

0 comments on commit d0de602

Please sign in to comment.