Skip to content

Commit 6421824

Browse files
committed
Working on accepting multiple globs on command line
1 parent ea11f75 commit 6421824

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

src/classes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct ConfigSettings {
7878
pub algorithm: HashAlgorithm,
7979
pub encoding: OutputEncoding,
8080
pub limit_num: Option<usize>,
81-
pub supplied_path: Option<String>,
81+
pub supplied_paths: Vec<String>,
8282
}
8383

8484
impl ConfigSettings {
@@ -101,12 +101,12 @@ impl ConfigSettings {
101101
algorithm,
102102
encoding,
103103
limit_num,
104-
supplied_path: None,
104+
supplied_paths: Vec::new(),
105105
}
106106
}
107107

108-
pub fn set_supplied_path(&mut self, path: Option<String>) {
109-
self.supplied_path = path;
108+
pub fn set_supplied_paths(&mut self, paths: Vec<String>) {
109+
self.supplied_paths = paths;
110110
}
111111
}
112112

src/main.rs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::io;
88
use std::io::BufRead;
99
use std::str::FromStr;
1010

11-
use anyhow::{Result, anyhow};
11+
use anyhow::{anyhow, Result};
1212

1313
//use crate::hasher::hash_file_crc32;
1414
use blake2::{Blake2b512, Blake2s256};
@@ -24,7 +24,7 @@ use classes::OutputEncoding;
2424
use hasher::{file_exists, hash_file_encoded};
2525

2626
use crate::classes::{
27-
BasicHash, ConfigSettings, DEFAULT_HASH, GIT_VERSION_SHORT, HELP, HashAlgorithm, VERSION,
27+
BasicHash, ConfigSettings, HashAlgorithm, DEFAULT_HASH, GIT_VERSION_SHORT, HELP, VERSION,
2828
};
2929

3030
mod classes;
@@ -95,7 +95,7 @@ fn worker_func() -> Result<()> {
9595

9696
/// get the required files, either using supplied path or from reading stdin
9797
fn get_required_filenames(config: &ConfigSettings) -> Result<Vec<String>> {
98-
let mut paths = if config.supplied_path.is_none() {
98+
let mut paths = if config.supplied_paths.is_empty() {
9999
// No path specified, read from stdin
100100
get_paths_from_stdin(config)?
101101
} else {
@@ -114,10 +114,10 @@ fn show_initial_info(config: &ConfigSettings) {
114114
show_help(false);
115115
eprintln!();
116116
eprintln!("Config: {config:?}");
117-
if config.supplied_path.is_none() {
117+
if config.supplied_paths.is_empty() {
118118
eprintln!("No path specified, reading from stdin");
119119
} else {
120-
eprintln!("Path: {}", config.supplied_path.as_ref().unwrap());
120+
eprintln!("Paths: {} file path(s) supplied", config.supplied_paths.len());
121121
}
122122
}
123123

@@ -165,19 +165,25 @@ fn process_command_line(mut pargs: Arguments) -> Result<ConfigSettings> {
165165
let remaining_args = args_finished(pargs)?;
166166

167167
// get the supplied path, if any. Turn the vector into a single Option<String>
168-
let supplied_path = match remaining_args.len() {
169-
0 => None, // no path, we are expecting to read from stdin
170-
1 => Some(remaining_args[0].to_string_lossy().to_string()), // one path given
171-
_ => {
172-
// more than one path given, error out
173-
return Err(anyhow!(
174-
"Only one path parameter can be given, but found {remaining_args:?}"
175-
));
176-
}
177-
};
168+
// let supplied_path = match remaining_args.len() {
169+
// 0 => None, // no path, we are expecting to read from stdin
170+
// 1 => Some(remaining_args[0].to_string_lossy().to_string()), // one path given
171+
// _ => {
172+
// // more than one path given, error out
173+
// return Err(anyhow!(
174+
// "Only one path parameter can be given, but found {remaining_args:?}"
175+
// ));
176+
// }
177+
// };
178+
179+
// get the supplied paths from remaining arguments
180+
let supplied_paths = remaining_args
181+
.into_iter()
182+
.map(|arg| arg.to_string_lossy().to_string())
183+
.collect();
178184

179-
// add the supplied path to config object
180-
config.set_supplied_path(supplied_path);
185+
// add the supplied paths to config object
186+
config.set_supplied_paths(supplied_paths);
181187

182188
Ok(config)
183189
}
@@ -207,17 +213,26 @@ fn get_paths_matching_glob(config: &ConfigSettings) -> Result<Vec<String>> {
207213
require_literal_leading_dot: false,
208214
};
209215

210-
let pattern = config
211-
.supplied_path
212-
.as_ref()
213-
.ok_or_else(|| anyhow!("Supplied path is None, but should have been Some"))?;
216+
let mut result = Vec::new();
217+
218+
for pattern in &config.supplied_paths {
219+
// Try to match the pattern as a glob
220+
let glob_matches: Vec<_> = glob::glob_with(pattern, glob_settings.clone())?
221+
.filter_map(|entry| match entry {
222+
Ok(path) if path.is_file() => Some(path.to_string_lossy().into_owned()),
223+
_ => None,
224+
})
225+
.collect();
226+
227+
// If the glob matched nothing, check if the pattern itself is a valid file
228+
if glob_matches.is_empty() && file_exists(pattern) {
229+
result.push(pattern.clone());
230+
} else {
231+
result.extend(glob_matches);
232+
}
233+
}
214234

215-
Ok(glob::glob_with(pattern, glob_settings)?
216-
.filter_map(|entry| match entry {
217-
Ok(path) if path.is_file() => Some(path.to_string_lossy().into_owned()),
218-
_ => None,
219-
})
220-
.collect())
235+
Ok(result)
221236
}
222237

223238
/// output all file hashes matching a pattern, directly to stdout. Single-threaded

0 commit comments

Comments
 (0)