From b4fed83b9e5a9c60d7c3f06ad8cbec8bf3cc2b8d Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 12 Aug 2023 11:44:14 +0200 Subject: Add the -n option --- src/main.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 278e530..a6c77ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ struct Flags { pub dryrun: bool, pub encode: bool, pub individual: bool, + pub nobackup: bool, pub nul: bool, pub verbose: bool, } @@ -41,6 +42,7 @@ impl Flags { Short('d') | Long("dryrun") => flags.dryrun = true, Short('e') | Long("encode") => flags.encode = true, Short('i') | Long("individual") => flags.individual = true, + Short('n') | Long("no-backup") => flags.nobackup = true, Short('v') | Long("verbose") => flags.verbose = true, Value(v) => { rest.push(v); @@ -59,7 +61,7 @@ fn usage(bad_flags: Option) -> ! { if let Some(e) = bad_flags { warn!("{e}"); } - eprintln!("Usage: {p} [-0deiv] command [argument ...]"); + eprintln!("Usage: {p} [-0deinv] command [argument ...]"); process::exit(1); } @@ -131,17 +133,19 @@ fn work() -> Result<(), io::Error> { .sorted_by_key(|s| Reverse(s.0.components().count())) .collect_vec(); - let pid = process::id().to_string(); - let cache_base = env::var("XDG_CACHE_HOME").unwrap_or_else(|_| { - err!("XDG_CACHE_HOME variable must be set"); - }); - let cache_dir = [Path::new(cache_base.as_str()), Path::new("mmv"), Path::new(pid.as_str())].iter().collect::(); - fs::create_dir_all(&cache_dir)?; + if !flags.nobackup { + let pid = process::id().to_string(); + let cache_base = env::var("XDG_CACHE_HOME").unwrap_or_else(|_| { + err!("XDG_CACHE_HOME variable must be set"); + }); + let cache_dir = [Path::new(cache_base.as_str()), Path::new("mmv"), Path::new(pid.as_str())].iter().collect::(); + fs::create_dir_all(&cache_dir)?; - let cwd = require!(env::current_dir()); - require!(env::set_current_dir(cache_dir)); - backup_srcs(ps.iter().map(|(s, _, _)| s))?; - require!(env::set_current_dir(cwd)); + let cwd = require!(env::current_dir()); + require!(env::set_current_dir(cache_dir)); + backup_srcs(ps.iter().map(|(s, _, _)| s))?; + require!(env::set_current_dir(cwd)); + } if flags.dryrun { for (s, _, d) in ps { -- cgit v1.2.3