Skip to content

Commit 5433b52

Browse files
authored
Change cli to propagate error to exit code (#8856)
* feat(cli): use error exit code when command or file * fix: remove prints * style: rust fmt * refactor: use CLI specific error for display * refactor: better use statements * Revert "refactor: better use statements" This reverts commit fac8c3a. * Revert "refactor: use CLI specific error for display" This reverts commit e58d331. * refactor: wrap main_inner, use ExitCode
1 parent 08de64d commit 5433b52

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

datafusion-cli/src/command.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ impl Command {
7979
filename, e
8080
))
8181
})?;
82-
exec_from_lines(ctx, &mut BufReader::new(file), print_options).await;
82+
exec_from_lines(ctx, &mut BufReader::new(file), print_options)
83+
.await?;
8384
Ok(())
8485
} else {
8586
exec_err!("Required filename argument is missing")

datafusion-cli/src/exec.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,20 @@ pub async fn exec_from_commands(
5353
ctx: &mut SessionContext,
5454
commands: Vec<String>,
5555
print_options: &PrintOptions,
56-
) {
56+
) -> Result<()> {
5757
for sql in commands {
58-
match exec_and_print(ctx, print_options, sql).await {
59-
Ok(_) => {}
60-
Err(err) => println!("{err}"),
61-
}
58+
exec_and_print(ctx, print_options, sql).await?;
6259
}
60+
61+
Ok(())
6362
}
6463

6564
/// run and execute SQL statements and commands from a file, against a context with the given print options
6665
pub async fn exec_from_lines(
6766
ctx: &mut SessionContext,
6867
reader: &mut BufReader<File>,
6968
print_options: &PrintOptions,
70-
) {
69+
) -> Result<()> {
7170
let mut query = "".to_owned();
7271

7372
for line in reader.lines() {
@@ -97,26 +96,28 @@ pub async fn exec_from_lines(
9796
// run the left over query if the last statement doesn't contain ‘;’
9897
// ignore if it only consists of '\n'
9998
if query.contains(|c| c != '\n') {
100-
match exec_and_print(ctx, print_options, query).await {
101-
Ok(_) => {}
102-
Err(err) => println!("{err}"),
103-
}
99+
exec_and_print(ctx, print_options, query).await?;
104100
}
101+
102+
Ok(())
105103
}
106104

107105
pub async fn exec_from_files(
108106
ctx: &mut SessionContext,
109107
files: Vec<String>,
110108
print_options: &PrintOptions,
111-
) {
109+
) -> Result<()> {
112110
let files = files
113111
.into_iter()
114112
.map(|file_path| File::open(file_path).unwrap())
115113
.collect::<Vec<_>>();
114+
116115
for file in files {
117116
let mut reader = BufReader::new(file);
118-
exec_from_lines(ctx, &mut reader, print_options).await;
117+
exec_from_lines(ctx, &mut reader, print_options).await?;
119118
}
119+
120+
Ok(())
120121
}
121122

122123
/// run and execute SQL statements and commands against a context with the given print options
@@ -215,6 +216,7 @@ async fn exec_and_print(
215216
MsSQL, ClickHouse, BigQuery, Ansi."
216217
)
217218
})?;
219+
218220
let statements = DFParser::parse_sql_with_dialect(&sql, dialect.as_ref())?;
219221
for statement in statements {
220222
let mut plan = ctx.state().statement_to_plan(statement).await?;

datafusion-cli/src/main.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use std::collections::HashMap;
1919
use std::env;
2020
use std::path::Path;
21+
use std::process::ExitCode;
2122
use std::str::FromStr;
2223
use std::sync::{Arc, OnceLock};
2324

@@ -138,7 +139,18 @@ struct Args {
138139
}
139140

140141
#[tokio::main]
141-
pub async fn main() -> Result<()> {
142+
/// Calls [`main_inner`], then handles printing errors and returning the correct exit code
143+
pub async fn main() -> ExitCode {
144+
if let Err(e) = main_inner().await {
145+
println!("Error: {e}");
146+
return ExitCode::FAILURE;
147+
}
148+
149+
ExitCode::SUCCESS
150+
}
151+
152+
/// Main CLI entrypoint
153+
async fn main_inner() -> Result<()> {
142154
env_logger::init();
143155
let args = Args::parse();
144156

@@ -216,7 +228,7 @@ pub async fn main() -> Result<()> {
216228

217229
if commands.is_empty() && files.is_empty() {
218230
if !rc.is_empty() {
219-
exec::exec_from_files(&mut ctx, rc, &print_options).await
231+
exec::exec_from_files(&mut ctx, rc, &print_options).await?;
220232
}
221233
// TODO maybe we can have thiserror for cli but for now let's keep it simple
222234
return exec::exec_from_repl(&mut ctx, &mut print_options)
@@ -225,11 +237,11 @@ pub async fn main() -> Result<()> {
225237
}
226238

227239
if !files.is_empty() {
228-
exec::exec_from_files(&mut ctx, files, &print_options).await;
240+
exec::exec_from_files(&mut ctx, files, &print_options).await?;
229241
}
230242

231243
if !commands.is_empty() {
232-
exec::exec_from_commands(&mut ctx, commands, &print_options).await;
244+
exec::exec_from_commands(&mut ctx, commands, &print_options).await?;
233245
}
234246

235247
Ok(())

0 commit comments

Comments
 (0)