feat(cli): make input handling agnostic
Co-authored-by: Will Lillis <will.lillis24@gmail.com>
This commit is contained in:
parent
3456330fe9
commit
cc449ad965
4 changed files with 188 additions and 111 deletions
|
|
@ -1446,56 +1446,3 @@ const fn get_styles() -> clap::builder::Styles {
|
|||
)
|
||||
.placeholder(Style::new().fg_color(Some(Color::Ansi(AnsiColor::White))))
|
||||
}
|
||||
|
||||
fn collect_paths(paths_file: Option<&str>, paths: Option<Vec<String>>) -> Result<Vec<String>> {
|
||||
if let Some(paths_file) = paths_file {
|
||||
return Ok(fs::read_to_string(paths_file)
|
||||
.with_context(|| format!("Failed to read paths file {paths_file}"))?
|
||||
.trim()
|
||||
.lines()
|
||||
.map(String::from)
|
||||
.collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
if let Some(paths) = paths {
|
||||
let mut result = Vec::new();
|
||||
|
||||
let mut incorporate_path = |path: &str, positive| {
|
||||
if positive {
|
||||
result.push(path.to_string());
|
||||
} else if let Some(index) = result.iter().position(|p| p == path) {
|
||||
result.remove(index);
|
||||
}
|
||||
};
|
||||
|
||||
for mut path in paths {
|
||||
let mut positive = true;
|
||||
if path.starts_with('!') {
|
||||
positive = false;
|
||||
path = path.trim_start_matches('!').to_string();
|
||||
}
|
||||
|
||||
if Path::new(&path).exists() {
|
||||
incorporate_path(&path, positive);
|
||||
} else {
|
||||
let paths =
|
||||
glob(&path).with_context(|| format!("Invalid glob pattern {path:?}"))?;
|
||||
for path in paths {
|
||||
if let Some(path) = path?.to_str() {
|
||||
incorporate_path(path, positive);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if result.is_empty() {
|
||||
return Err(anyhow!(
|
||||
"No files were found at or matched by the provided pathname/glob"
|
||||
));
|
||||
}
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
Err(anyhow!("Must provide one or more paths"))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue