| 1 | + | use std::{fs, path::PathBuf}; |
| 2 | + | |
1 | 3 | | use anyhow::{anyhow, Result}; |
2 | 4 | | use clap::{Parser, ValueHint}; |
3 | | - | use std::{fs, path::PathBuf}; |
| 5 | + | use globwalk::{GlobWalker, GlobWalkerBuilder}; |
4 | 6 | | |
5 | 7 | | #[derive(Parser, Debug)] |
6 | 8 | | #[command(author, version, about, long_about = None)] |
| skipped 23 lines |
30 | 32 | | } |
31 | 33 | | } |
32 | 34 | | |
33 | | - | pub fn get_directory(&self) -> Result<String> { |
34 | | - | let dir_pathbuf: PathBuf = self |
35 | | - | .dir |
36 | | - | .as_ref() |
37 | | - | .unwrap_or(&std::env::current_dir()?) |
38 | | - | .as_os_str() |
39 | | - | .into(); |
40 | | - | |
41 | | - | let dir = fs::canonicalize(dir_pathbuf)? |
42 | | - | .as_os_str() |
43 | | - | .to_str() |
44 | | - | .ok_or_else(|| anyhow!("Invalid directory"))? |
45 | | - | .to_string(); |
46 | | - | |
| 35 | + | pub fn get_directory(&self) -> Result<PathBuf> { |
| 36 | + | let current_dir = std::env::current_dir()?; |
| 37 | + | let dir_path = self.dir.as_ref().unwrap_or(¤t_dir).as_path(); |
| 38 | + | let dir = fs::canonicalize(dir_path)?; |
47 | 39 | | Ok(dir) |
48 | 40 | | } |
49 | 41 | | |
50 | | - | pub fn get_glob_patterns(&self) -> PathBuf { |
51 | | - | match self.types.as_ref() { |
52 | | - | Some(filetypes) => vec![ |
53 | | - | self.get_directory().unwrap(), |
54 | | - | String::from("**"), |
55 | | - | format!("*.{{{filetypes}}}"), |
56 | | - | ] |
57 | | - | .iter() |
58 | | - | .collect::<PathBuf>(), |
59 | | - | None => vec![self.get_directory().unwrap().as_str(), "**", "*"] |
60 | | - | .iter() |
61 | | - | .collect::<PathBuf>(), |
62 | | - | } |
| 42 | + | pub fn get_glob_walker(&self) -> Result<GlobWalker> { |
| 43 | + | let pattern: String = match self.types.as_ref() { |
| 44 | + | Some(filetypes) => format!("**/*{{{filetypes}}}"), |
| 45 | + | None => "**/*".to_string(), |
| 46 | + | }; |
| 47 | + | // TODO: add params for maximum depth and following symlinks, then pass them to this builder |
| 48 | + | GlobWalkerBuilder::from_patterns(self.get_directory()?, &[pattern]) |
| 49 | + | .build() |
| 50 | + | .map_err(|e| anyhow!(e)) |
63 | 51 | | } |
64 | 52 | | } |
65 | 53 | | |