Skip to content

Commit

Permalink
fix(cli): expose error for handle_reader (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Sep 14, 2023
1 parent d787814 commit 0bcb483
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
6 changes: 3 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub async fn main() -> Result<()> {
let mut session = session::Session::try_new(dsn, settings, is_repl).await?;

if is_repl {
session.handle().await;
session.handle_repl().await;
return Ok(());
}

Expand All @@ -309,11 +309,11 @@ pub async fn main() -> Result<()> {
if args.non_interactive {
return Err(anyhow!("no query specified"));
}
session.handle_reader(stdin().lock()).await
session.handle_reader(stdin().lock()).await?;
}
Some(query) => match args.data {
None => {
session.handle_reader(std::io::Cursor::new(query)).await;
session.handle_reader(std::io::Cursor::new(query)).await?;
}
Some(data) => {
let options = args.format.get_options(&args.format_opt);
Expand Down
21 changes: 4 additions & 17 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use std::collections::BTreeMap;
use std::io::stdin;
use std::io::BufRead;
use std::path::Path;
use std::sync::Arc;
Expand Down Expand Up @@ -86,14 +85,6 @@ impl Session {
})
}

pub async fn handle(&mut self) {
if self.is_repl {
self.handle_repl().await;
} else {
self.handle_reader(stdin().lock()).await;
}
}

async fn prompt(&self) -> String {
if !self.query.is_empty() {
"> ".to_owned()
Expand Down Expand Up @@ -173,30 +164,26 @@ impl Session {
let _ = rl.save_history(&get_history_path());
}

pub async fn handle_reader<R: BufRead>(&mut self, r: R) {
pub async fn handle_reader<R: BufRead>(&mut self, r: R) -> Result<()> {
let start = Instant::now();
let mut lines = r.lines();
while let Some(Ok(line)) = lines.next() {
let queries = self.append_query(&line);
for query in queries {
if let Err(e) = self.handle_query(false, &query).await {
eprintln!("{}", e);
return;
}
self.handle_query(false, &query).await?;
}
}

// if the last query is not finished with `;`, we need to execute it.
let query = self.query.trim().to_owned();
if !query.is_empty() {
self.query.clear();
if let Err(e) = self.handle_query(false, &query).await {
eprintln!("{}", e);
}
self.handle_query(false, &query).await?;
}
if self.settings.time {
println!("{:.3}", start.elapsed().as_secs_f64());
}
Ok(())
}

pub fn append_query(&mut self, line: &str) -> Vec<String> {
Expand Down

0 comments on commit 0bcb483

Please sign in to comment.