Skip to content

Commit 411c272

Browse files
committed
feat: report error when loading directory fails
1 parent dfe376e commit 411c272

File tree

1 file changed

+53
-47
lines changed

1 file changed

+53
-47
lines changed

datafusion-postgres-cli/src/main.rs

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,61 +47,67 @@ fn parse_table_def(table_def: &str) -> (&str, &str) {
4747
}
4848

4949
impl Opt {
50-
fn include_directory_files(&mut self) {
50+
fn include_directory_files(&mut self) -> Result<(), Box<dyn std::error::Error>> {
5151
if let Some(directory) = &self.directory {
52-
if let Ok(entries) = fs::read_dir(directory) {
53-
for entry in entries.flatten() {
54-
let path = entry.path();
55-
if !path.is_file() {
56-
continue;
57-
}
52+
match fs::read_dir(directory) {
53+
Ok(entries) => {
54+
for entry in entries.flatten() {
55+
let path = entry.path();
56+
if !path.is_file() {
57+
continue;
58+
}
5859

59-
if let Some(ext) = path.extension().and_then(OsStr::to_str) {
60-
let ext_lower = ext.to_lowercase();
61-
if let Some(base_name) = path.file_stem().and_then(|s| s.to_str()) {
62-
match ext_lower.as_ref() {
63-
"json" => {
64-
self.json_tables.push(format!(
65-
"{}:{}",
66-
base_name,
67-
path.to_string_lossy()
68-
));
69-
}
70-
"avro" => {
71-
self.avro_tables.push(format!(
72-
"{}:{}",
73-
base_name,
74-
path.to_string_lossy()
75-
));
76-
}
77-
"parquet" => {
78-
self.parquet_tables.push(format!(
79-
"{}:{}",
80-
base_name,
81-
path.to_string_lossy()
82-
));
60+
if let Some(ext) = path.extension().and_then(OsStr::to_str) {
61+
let ext_lower = ext.to_lowercase();
62+
if let Some(base_name) = path.file_stem().and_then(|s| s.to_str()) {
63+
match ext_lower.as_ref() {
64+
"json" => {
65+
self.json_tables.push(format!(
66+
"{}:{}",
67+
base_name,
68+
path.to_string_lossy()
69+
));
70+
}
71+
"avro" => {
72+
self.avro_tables.push(format!(
73+
"{}:{}",
74+
base_name,
75+
path.to_string_lossy()
76+
));
77+
}
78+
"parquet" => {
79+
self.parquet_tables.push(format!(
80+
"{}:{}",
81+
base_name,
82+
path.to_string_lossy()
83+
));
84+
}
85+
"csv" => {
86+
self.csv_tables.push(format!(
87+
"{}:{}",
88+
base_name,
89+
path.to_string_lossy()
90+
));
91+
}
92+
"arrow" => {
93+
self.arrow_tables.push(format!(
94+
"{}:{}",
95+
base_name,
96+
path.to_string_lossy()
97+
));
98+
}
99+
_ => {}
83100
}
84-
"csv" => {
85-
self.csv_tables.push(format!(
86-
"{}:{}",
87-
base_name,
88-
path.to_string_lossy()
89-
));
90-
}
91-
"arrow" => {
92-
self.arrow_tables.push(format!(
93-
"{}:{}",
94-
base_name,
95-
path.to_string_lossy()
96-
));
97-
}
98-
_ => {}
99101
}
100102
}
101103
}
102104
}
105+
Err(e) => {
106+
return Err(format!("Failed to load directory {}: {}", directory, e).into());
107+
}
103108
}
104109
}
110+
Ok(())
105111
}
106112
}
107113

@@ -168,7 +174,7 @@ async fn setup_session_context(
168174
#[tokio::main]
169175
async fn main() -> Result<(), Box<dyn std::error::Error>> {
170176
let mut opts = Opt::from_args();
171-
opts.include_directory_files();
177+
opts.include_directory_files()?;
172178

173179
let session_context = SessionContext::new();
174180

0 commit comments

Comments
 (0)