Allow working directory to be nonexistent if search path is given

Fixes #1072
This commit is contained in:
Sijmen 2022-07-31 23:13:08 +02:00 committed by David Peter
parent ac934bd703
commit fdcbb2f008

View file

@ -70,10 +70,7 @@ fn run() -> Result<ExitCode> {
let matches = app::build_app().get_matches_from(env::args_os()); let matches = app::build_app().get_matches_from(env::args_os());
set_working_dir(&matches)?; set_working_dir(&matches)?;
let current_directory = Path::new("."); let search_paths = extract_search_paths(&matches)?;
ensure_current_directory_exists(current_directory)?;
let search_paths = extract_search_paths(&matches, current_directory)?;
let pattern = extract_search_pattern(&matches)?; let pattern = extract_search_pattern(&matches)?;
ensure_search_pattern_is_not_a_path(&matches, pattern)?; ensure_search_pattern_is_not_a_path(&matches, pattern)?;
let pattern_regex = build_pattern_regex(&matches, pattern)?; let pattern_regex = build_pattern_regex(&matches, pattern)?;
@ -125,17 +122,15 @@ fn extract_search_pattern(matches: &clap::ArgMatches) -> Result<&'_ str> {
Ok(pattern) Ok(pattern)
} }
fn extract_search_paths( fn extract_search_paths(matches: &clap::ArgMatches) -> Result<Vec<PathBuf>> {
matches: &clap::ArgMatches, let current_directory = Path::new(".");
current_directory: &Path,
) -> Result<Vec<PathBuf>> { let parameter_paths = matches
let mut search_paths = matches
.values_of_os("path") .values_of_os("path")
.or_else(|| matches.values_of_os("search-path")) .or_else(|| matches.values_of_os("search-path"));
.map_or_else(
|| vec![current_directory.to_path_buf()], let mut search_paths = match parameter_paths {
|paths| { Some(paths) => paths
paths
.filter_map(|path| { .filter_map(|path| {
let path_buffer = PathBuf::from(path); let path_buffer = PathBuf::from(path);
if filesystem::is_existing_directory(&path_buffer) { if filesystem::is_existing_directory(&path_buffer) {
@ -143,14 +138,18 @@ fn extract_search_paths(
} else { } else {
print_error(format!( print_error(format!(
"Search path '{}' is not a directory.", "Search path '{}' is not a directory.",
path_buffer.to_string_lossy() path_buffer.to_string_lossy(),
)); ));
None None
} }
}) })
.collect() .collect(),
}, None => {
); ensure_current_directory_exists(current_directory)?;
vec![current_directory.to_path_buf()]
}
};
if search_paths.is_empty() { if search_paths.is_empty() {
return Err(anyhow!("No valid search paths given.")); return Err(anyhow!("No valid search paths given."));
} }