Skip to content

Commit

Permalink
Add support for dry-running single-shot & watch cmds
Browse files Browse the repository at this point in the history
Added support for dy-running the single-shot and watch subcommands to
allow for easier testing.

Signed-off-by: Jason Rogena <[email protected]>
  • Loading branch information
jasonrogena committed Jul 23, 2022
1 parent 7960421 commit a67602e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "fs-librarian"
version = "0.2.2"
authors = ["Jason Rogena <jason@rogena.me>"]
version = "0.3.0"
authors = ["Jason Rogena <null+fs-librarian@rogena.me>"]
edition = "2021"
rust-version = "1.61"
readme = "README.md"
Expand Down
29 changes: 26 additions & 3 deletions src/library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ const TEMPLATE_VAR_MIME_TYPE: &str = "mime_type";
#[derive(Debug)]
pub struct Library<'a> {
config: &'a config::Libraries,
skip_running_commands: &'a bool,
}

impl<'a> Library<'a> {
#[allow(dead_code)]
pub fn new(config: &config::Libraries) -> Library {
Library { config }
pub fn new(config: &'a config::Libraries, skip_running_commands: &'a bool) -> Library<'a> {
Library {
config,
skip_running_commands,
}
}

#[allow(dead_code)]
Expand Down Expand Up @@ -127,6 +131,25 @@ impl<'a> Library<'a> {
}

// run the command if mime_type passes
self.run_command(path, mime_type.as_str())
}

fn run_command(&self, path: &Path, mime_type: &str) -> Result<bool, error::Error> {
if *self.skip_running_commands {
match path.as_os_str().to_str() {
None => {
return Err(error::Error::new(format!(
"Could not extract string from path {:?}",
path
)));
}
Some(s) => {
println!("{}", s);
return Ok(true);
}
}
}

let path_str = match path.as_os_str().to_str() {
None => {
return Err(error::Error::new(format!(
Expand All @@ -138,7 +161,7 @@ impl<'a> Library<'a> {
};
let mut data = collections::HashMap::new();
data.insert(TEMPLATE_VAR_FILE_PATH, path_str);
data.insert(TEMPLATE_VAR_MIME_TYPE, mime_type.as_str());
data.insert(TEMPLATE_VAR_MIME_TYPE, mime_type);

let tmplt = template::Template::new(self.config.command.clone())?;
let cmd_str = tmplt.render(&data)?;
Expand Down
9 changes: 6 additions & 3 deletions src/library/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ fn test_process() {
if !test_cases.contains_key(&cur_lib_key) {
continue;
}
let lib = Library::new(&cur_lib_val);
let skip_running_commands = false;
let lib = Library::new(&cur_lib_val, &skip_running_commands);
assert_eq!(
lib.process(None).unwrap(),
test_cases.get(&cur_lib_key).unwrap().0.len() as u64
Expand Down Expand Up @@ -109,11 +110,12 @@ fn test_process_single_file() {
);
}

let skip_running_commands = false;
for (cur_lib_key, cur_lib_val) in conf.unwrap().libraries {
if !test_cases.contains_key(&cur_lib_key) {
continue;
}
let lib = Library::new(&cur_lib_val);
let lib = Library::new(&cur_lib_val, &skip_running_commands);

for cur_file in test_cases.get(&cur_lib_key).unwrap().0.iter() {
let cur_test_path = Path::new(cur_file.0.as_str());
Expand All @@ -134,7 +136,8 @@ fn test_contains_path() {
config::Config::new(&"tests/configs/good.toml".to_string()).unwrap()
};
let cur_dir = current_dir().unwrap();
let audio_lib = Library::new(&conf.libraries["audio"]);
let skip_running_commands = false;
let audio_lib = Library::new(&conf.libraries["audio"], &skip_running_commands);
assert!(!audio_lib.contains_path(Path::new("")));
if OS == "windows" {
assert!(audio_lib.contains_path(Path::new("tests\\files\\audio\\flac")));
Expand Down
34 changes: 24 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static GLOBAL_FAILED_TREADS: AtomicUsize = AtomicUsize::new(0);
/// Runs pre-configured commands against a group of files that match a set of filters
#[derive(Debug, Parser)]
#[clap(name = "fs-librarian")]
#[clap(about = "Goes through file types inside directories and does with them as you wish", long_about = None)]
#[clap(author, version, about = "Goes through file types inside directories and does with them as you wish", long_about = None)]
struct Cli {
#[clap(subcommand)]
command: Commands,
Expand All @@ -30,14 +30,22 @@ enum Commands {
#[clap(arg_required_else_help = true)]
Watch {
/// Path to the configuration file to use
#[clap(required = true)]
config_path: String,
/// Print the file Librarian got a notification for, instead of running the pre-configured command against it
#[clap(short, long)]
dry_run: bool,
},

/// Run Librarian once
#[clap(arg_required_else_help = true)]
SingleShot {
/// Path to the configuration file to use
#[clap(required = true)]
config_path: String,
/// Print the filtered files, instead of running the pre-configured commands against them
#[clap(short, long)]
dry_run: bool,
},

/// Debugging tools to help you to better work with Librarian
Expand All @@ -63,11 +71,17 @@ enum TestCommands {
fn main() {
let args = Cli::parse();
match args.command {
Commands::Watch { config_path } => {
watch(&config_path);
Commands::Watch {
config_path,
dry_run,
} => {
watch(&config_path, dry_run);
}
Commands::SingleShot { config_path } => {
single_shot(&config_path);
Commands::SingleShot {
config_path,
dry_run,
} => {
single_shot(&config_path, dry_run);
}
Commands::Test(t) => {
test(&t);
Expand All @@ -85,7 +99,7 @@ fn get_config(config_path: &String) -> config::Config {
}
}

fn watch(config_path: &String) {
fn watch(config_path: &String, dry_run: bool) {
let conf = get_config(config_path);

let mut paths: HashSet<String> = HashSet::new();
Expand All @@ -111,7 +125,7 @@ fn watch(config_path: &String) {

let conf = get_config(&config_path_clone);
for cur_lib_config in conf.libraries {
let cur_lib = library::Library::new(&cur_lib_config.1);
let cur_lib = library::Library::new(&cur_lib_config.1, &dry_run);
if !cur_lib.contains_path(Path::new(&path)) {
continue;
}
Expand All @@ -133,17 +147,17 @@ fn watch(config_path: &String) {
}
});

single_shot(config_path);
single_shot(config_path, dry_run);
notify_obj.watch();
}

fn single_shot(config_path: &String) {
fn single_shot(config_path: &String, dry_run: bool) {
let conf = get_config(config_path);

for cur_conf in conf.libraries {
GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
thread::spawn(move || {
match library::Library::new(&cur_conf.1).process(None) {
match library::Library::new(&cur_conf.1, &dry_run).process(None) {
Ok(k) => {
println!("Processed {} files in the {} library", k, cur_conf.0);
}
Expand Down

0 comments on commit a67602e

Please sign in to comment.