Skip to content

Commit

Permalink
feat(common): make dump file prefix configurable
Browse files Browse the repository at this point in the history
Closes #43
  • Loading branch information
ismaelgv committed Jan 12, 2025
1 parent c35c279 commit 487f051
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* New `--transform` option to apply text transformation to replacement in
`regex` command, including captured groups.
* Generate `man` pages files.
* New `--dump-prefix` option to configure dump file prefix.
### Changed
* **BREAKING** Move root command to `regex` subcommand.
* Update project dependencies.
Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub struct CommonArgs {
/// Force dumping operations into a file even in dry-run mode.
#[arg(long, conflicts_with = "no_dump")]
pub dump: bool,
/// Set the dump file prefix.
#[arg(long, conflicts_with = "no_dump", default_value = "rnr-")]
pub dump_prefix: String,
/// Do not dump operations into a file.
#[arg(long)]
pub no_dump: bool,
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Config {
pub backup: bool,
pub dirs: bool,
pub dump: bool,
pub dump_prefix: String,
pub run_mode: RunMode,
pub replace_mode: ReplaceMode,
pub printer: Printer,
Expand Down Expand Up @@ -157,6 +158,7 @@ fn parse_arguments() -> Result<Config> {
backup: common.backup,
dirs: path.map_or(false, |p| p.include_dirs),
dump,
dump_prefix: common.dump_prefix.clone(),
run_mode,
replace_mode,
printer,
Expand Down
9 changes: 7 additions & 2 deletions src/dumpfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ use std::fs::File;
use std::path::Path;

/// Dump operations intto file in JSON format
pub fn dump_to_file(operations: &[Operation]) -> Result<()> {
pub fn dump_to_file(prefix: String, operations: &[Operation]) -> Result<()> {
let now = chrono::Local::now();
let dump = DumpFormat {
date: now.format("%Y-%m-%d %H:%M:%S").to_string(),
operations: operations.to_vec(),
};

// Create filename with the following syntax: "rnr-<DATE>.json"
let filename = "rnr-".to_string() + &now.format("%Y-%m-%d_%H%M%S").to_string() + ".json";
let filename = format!(
"{}{}{}",
prefix,
now.format("%Y-%m-%d_%H%M%S").to_string(),
".json"
);

// Dump info to a file
let file = match File::create(&filename) {
Expand Down
34 changes: 19 additions & 15 deletions src/renamer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Renamer {

// Dump operations into a file if required
if self.config.dump {
dumpfile::dump_to_file(&operations)?;
dumpfile::dump_to_file(self.config.dump_prefix.clone(), &operations)?;
}

Ok(operations)
Expand Down Expand Up @@ -244,6 +244,21 @@ mod test {
use std::process;
use std::sync::Arc;

impl Default for Config {
fn default() -> Self {
Config {
force: true,
backup: false,
dirs: false,
dump: false,
dump_prefix: "rnr-".to_string(),
run_mode: RunMode::Simple(vec![]),
replace_mode: ReplaceMode::None,
printer: Printer::color(),
}
}
}

#[test]
fn renamer() {
let tempdir = tempfile::tempdir().expect("Error creating temp directory");
Expand Down Expand Up @@ -280,18 +295,15 @@ mod test {

// Create config
let mock_config = Arc::new(Config {
force: true,
backup: true,
dirs: false,
dump: false,
run_mode: RunMode::Simple(mock_files),
replace_mode: ReplaceMode::RegExp {
expression: Regex::new("test").unwrap(),
replacement: "passed".to_string(),
limit: 1,
transform: TextTransformation::None,
},
printer: Printer::color(),
..Config::default()
});

// Run renamer
Expand Down Expand Up @@ -339,18 +351,14 @@ mod test {
}

let mock_config = Arc::new(Config {
force: true,
backup: false,
dirs: false,
dump: false,
run_mode: RunMode::Simple(mock_files),
replace_mode: ReplaceMode::RegExp {
expression: Regex::new("a").unwrap(),
replacement: "b".to_string(),
limit: 0,
transform: TextTransformation::None,
},
printer: Printer::color(),
..Config::default()
});

let renamer = match Renamer::new(&mock_config) {
Expand Down Expand Up @@ -391,13 +399,9 @@ mod test {
}

let mock_config = Arc::new(Config {
force: true,
backup: false,
dirs: false,
dump: false,
run_mode: RunMode::Simple(mock_files),
replace_mode: ReplaceMode::ToASCII,
printer: Printer::color(),
..Config::default()
});

let renamer = match Renamer::new(&mock_config) {
Expand Down

0 comments on commit 487f051

Please sign in to comment.