Skip to content

Commit

Permalink
refactor(main): simplify suffix handling and add overwrite flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cauliyang committed Nov 7, 2023
1 parent 5ad06f6 commit e9cc401
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 70 deletions.
60 changes: 0 additions & 60 deletions README.md

This file was deleted.

11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ enum Commands {
target: Option<PathBuf>,

/// The suffix of the files to link. default is all files
#[arg(short = 's')]
suffix: Option<String>,
#[arg(short = 's', value_delimiter = ' ', num_args=1..)]
suffix: Option<Vec<String>>,

/// Overwrite existing files
#[arg(short = 'o', default_value = "false")]
overwrite: bool,
},
}

Expand Down Expand Up @@ -144,9 +148,10 @@ fn main() {
source,
target,
suffix,
overwrite,
}) => {
info!("'rsoft' {target:?} {suffix:?} ");
rsoft::rsoft(source, target.as_ref(), suffix.clone()).unwrap();
rsoft::rsoft(source, target.as_ref(), suffix.clone(), *overwrite).unwrap();
}

// If no subcommand was used, it's a normal top level command
Expand Down
26 changes: 19 additions & 7 deletions src/rsoft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,43 @@ use std::os::windows::fs as windows_fs;
pub fn rsoft<P: AsRef<Path>>(
source_directory: P,
target_directory: Option<P>,
suffix: Option<String>,
suffix: Option<Vec<String>>,
overwrite: bool,
) -> Result<()> {
let pattern = if suffix.is_some() {
format!(r".*\.{}", suffix.unwrap())
let suffix_re = suffix
.unwrap()

Check warning on line 22 in src/rsoft.rs

View workflow job for this annotation

GitHub Actions / clippy

called `unwrap` on `suffix` after checking its variant with `is_some`

warning: called `unwrap` on `suffix` after checking its variant with `is_some` --> src/rsoft.rs:21:25 | 20 | let pattern = if suffix.is_some() { | ------------------- help: try: `if let Some(..) = suffix` 21 | let suffix_re = suffix | _________________________^ 22 | | .unwrap() | |_____________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap = note: `#[warn(clippy::unnecessary_unwrap)]` on by default
.iter()
.map(|s| regex::escape(s))
.collect::<Vec<_>>()
.join("|");

format!(r".*\.({})$", suffix_re)
} else {
".*".to_string()
};

let re = Regex::new(&pattern).unwrap();

let mut current_directory = env::current_dir().unwrap();
let mut target_dir = env::current_dir().unwrap();
if target_directory.is_some() {
current_directory = target_directory.unwrap().as_ref().to_path_buf();
target_dir = target_directory.unwrap().as_ref().to_path_buf();

Check warning on line 37 in src/rsoft.rs

View workflow job for this annotation

GitHub Actions / clippy

called `unwrap` on `target_directory` after checking its variant with `is_some`

warning: called `unwrap` on `target_directory` after checking its variant with `is_some` --> src/rsoft.rs:37:22 | 36 | if target_directory.is_some() { | ----------------------------- help: try: `if let Some(..) = target_directory` 37 | target_dir = target_directory.unwrap().as_ref().to_path_buf(); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
}

for entry in WalkDir::new(source_directory)
// make target dir become absolute
target_dir = target_dir.canonicalize()?;
let source_dir = source_directory.as_ref().canonicalize()?;

for entry in WalkDir::new(source_dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().is_file() && re.is_match(&e.path().to_string_lossy()))
{
let path = entry.path();

let link_name = current_directory.join(path.file_name().unwrap());
let link_name = target_dir.join(path.file_name().unwrap());

if !link_name.exists() {
if !link_name.exists() || overwrite {
#[cfg(unix)]
{
if let Err(e) = unix_fs::symlink(path, &link_name) {
Expand Down

0 comments on commit e9cc401

Please sign in to comment.