Skip to content

Commit

Permalink
refactor(main): refactor rsoft function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cauliyang committed Nov 7, 2023
1 parent 8aff28d commit 534d4cc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ assert_cmd = "2.0"
assert_fs = "1.0"
pretty_assertions = "1"
sha256 = "1.4"
tempfile = "3.8"
17 changes: 13 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ enum Commands {
/// Create softlinks to files with same suffix in one directory recursively
Rsoft {
/// The directory to search
target: PathBuf,
/// The suffix of the files to link default is all files
source: PathBuf,

/// The directory to create the softlinks. default is current directory
#[arg(short = 't')]
target: Option<PathBuf>,

/// The suffix of the files to link. default is all files
#[arg(short = 's')]
suffix: Option<String>,
},
Expand Down Expand Up @@ -110,9 +115,13 @@ fn main() {
fq2fa::fq2fa(input).unwrap();
}

Some(Commands::Rsoft { target, suffix }) => {
Some(Commands::Rsoft {
source,
target,
suffix,
}) => {
info!("'rsoft' {target:?} {suffix:?} ");
rsoft::rsoft(target, suffix.clone()).unwrap();
rsoft::rsoft(source, target.as_ref(), suffix.clone()).unwrap();
}

// If no subcommand was used, it's a normal top level command
Expand Down
14 changes: 11 additions & 3 deletions src/rsoft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ use std::os::unix::fs as unix_fs;
#[cfg(windows)]
use std::os::windows::fs as windows_fs;

pub fn rsoft<P: AsRef<Path>>(target_directory: P, suffix: Option<String>) -> Result<()> {
pub fn rsoft<P: AsRef<Path>>(
source_directory: P,
target_directory: Option<P>,
suffix: Option<String>,
) -> Result<()> {
let pattern = if suffix.is_some() {
format!(r".*\.{}", suffix.unwrap())
} else {
".*".to_string()
};

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

for entry in WalkDir::new(target_directory)
let mut current_directory = env::current_dir().unwrap();
if target_directory.is_some() {
current_directory = target_directory.unwrap().as_ref().to_path_buf();

Check warning on line 29 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:29:29 | 28 | if target_directory.is_some() { | ----------------------------- help: try: `if let Some(..) = target_directory` 29 | current_directory = 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 = note: `#[warn(clippy::unnecessary_unwrap)]` on by default
}

for entry in WalkDir::new(source_directory)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().is_file() && re.is_match(&e.path().to_string_lossy()))
Expand Down
42 changes: 42 additions & 0 deletions tests/test_rsoft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use assert_cmd::cmd::Command;
use tempfile::tempdir;

#[test]
fn test_rsoft() {
let mut cmd = Command::cargo_bin("rboss").unwrap();

let temp_dir = tempdir().unwrap();

cmd.args([
"rsoft",
"tests",
"-t",
temp_dir.path().to_str().unwrap(),
"-s",
"bam",
]);

cmd.assert().success();

assert!(temp_dir.path().join("extract_1.bam").is_symlink());
assert!(temp_dir.path().join("reads.bam").is_symlink());
}

#[test]
fn test_rsoft_all() {
let mut cmd = Command::cargo_bin("rboss").unwrap();

let temp_dir = tempdir().unwrap();

cmd.args([
"rsoft",
"tests/data",
"-t",
temp_dir.path().to_str().unwrap(),
]);

cmd.assert().success();

assert!(temp_dir.path().join("extract_1.bam").is_symlink());
assert!(temp_dir.path().join("reads.bam").is_symlink());
}

0 comments on commit 534d4cc

Please sign in to comment.