Skip to content

Commit

Permalink
Added dry-run and removed glob
Browse files Browse the repository at this point in the history
Glob wasn't needed (at least on the Mac), so it's removed.
Added dry-run as a safety measure.
  • Loading branch information
evensolberg committed Dec 16, 2021
1 parent 4c5a9c8 commit e399341
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 96 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gather"
version = "0.1.0"
version = "0.2.0"
description = "Gathers files from directories and subdirectories into a target directory."
license = "Apache-2.0"
authors = ["evensolberg <[email protected]>"]
Expand All @@ -11,7 +11,6 @@ include = ["src/**/*", "README.md"]
[dependencies]
clap = "2.34.0"
env_logger = "0.9.0"
glob = "0.3.0"
log = "0.4.14"

[features]
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,33 @@ Gathers files from directories and subdirectories into a target directory.

`gather [FLAGS] <FILE(S)>... [--] <TARGET>`

Example:

`gather -m -u **/*.png **/*.jpg -- ../images/`

### Flags

|Short Form|Long Form|Description|
|:----|:---|:----------|
`-d`|`--debug`|Output debug information as we go. Supply it twice for trace-level logs.
`-o`|`--detail-off`|Don't print detailed information about each file processed.
`-h`|`--help`|Prints help information.
`-m`|`--move`|Move files instead of copying them.
`-o`|`--detail-off`|Don't print detailed information about each file processed.
`-p`|`--print-summary`|Print summary information about the number of files gathered.
`-q`|`--quiet`|Don't produce any output except errors while working.
`-r`|`--dry-run`|Iterate through the files and produce output without actually processing anything.
`-s`|`--stop-on-error`|Stop on error. If this flag isn't set, the application will attempt to continue in case of error.
`-u`|`--print-summary`|Print summary information about the number of files gathered.
`-V`|`--version`|Prints version information

### Arguments

|Argument|Description|
|:-------|:----------|
`<FILE(S)>...`|One or more file(s) to process. Wildcards and multiple files (e.g. `2019*.pdf 2020*.pdf`) are supported. Use `**` glob to recurse (i.e. `**/*.pdf`).
`<FILE(S)>...`|One or more file(s) to process. Wildcards and multiple files (e.g. `2019*.pdf 2020*.pdf`) are supported. Use `**` glob to recurse (i.e. `**/*.pdf`).<br>**Note: Case sensitive.**
`<TARGET>`|The target directory into which files are to be gathered.

## Notes

Currently, using `zsh` on the Mac, the program exits with an error if one of the `<FILE>` arguments isn't found (ie. `*.jpg *.jpeg *.png` - `*.jpeg` not found). This is due to how this is handled in the shell.

You can work around this by using the following command: `setopt NO_MATCH`
You can work around this by using the following command: `setopt +o NO_MATCH`
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ alias fmt := format
# Copy this settings files to the templates directory
@just:
cp {{invocation_directory()}}/justfile ~/CloudStation/Source/_Templates/justfile.template
-sd {{application}} gather ~/CloudStation/Source/_Templates/justfile.template
-sd {{application}} myapplication ~/CloudStation/Source/_Templates/justfile.template
cp {{invocation_directory()}}/deny.toml ~/CloudStation/Source/_Templates/deny.toml

# Check, but verbose
Expand Down
161 changes: 80 additions & 81 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use clap::{App, Arg}; // Command line
use std::error::Error;
use std::path::Path;

use glob::glob;

// Logging
use env_logger::{Builder, Target};
use log::LevelFilter;
Expand Down Expand Up @@ -52,6 +50,14 @@ fn run() -> Result<(), Box<dyn Error>> {
.takes_value(false)
.hidden(false),
)
.arg( // Dry-run
Arg::with_name("dry-run")
.short("r")
.long("dry-run")
.multiple(false)
.help("Iterate through the files and produce output without actually processing anything.")
.takes_value(false)
)
.arg( // Hidden debug parameter
Arg::with_name("debug")
.short("d")
Expand All @@ -71,7 +77,7 @@ fn run() -> Result<(), Box<dyn Error>> {
)
.arg( // Print summary information
Arg::with_name("summary")
.short("u")
.short("p")
.long("print-summary")
.multiple(false)
.help("Print summary information about the number of files gathered.")
Expand Down Expand Up @@ -141,99 +147,92 @@ fn run() -> Result<(), Box<dyn Error>> {
}

let show_detail_info = !cli_args.is_present("detail-off");
let dry_run = cli_args.is_present("dry-run");
if dry_run {
log::info!("Dry-run starting.");
}

let mut total_file_count: usize = 0;
let mut processed_file_count: usize = 0;
let mut skipped_file_count: usize = 0;

// Gather files
for filename in files_to_gather {
for entry in glob(filename).unwrap() {
if let Ok(path) = entry {
let new_filename = Path::new(target_dir).join(Path::new(path.file_name().unwrap()));
let targetfile = new_filename.as_path();
let new_filename =
Path::new(target_dir).join(Path::new(&filename).file_name().unwrap_or_default());
let targetfile = new_filename.as_path();

total_file_count += 1;
total_file_count += 1;

if move_files {
log::debug!(
"Moving file {} to {}",
&path.to_str().unwrap(),
&targetfile.display()
);
match std::fs::rename(&path, targetfile) {
Ok(_) => {
if show_detail_info {
log::info!(
" {} ==> {}",
path.to_str().unwrap(),
targetfile.to_str().unwrap()
);
}
processed_file_count += 1;
if dry_run {
if move_files {
log::info!(" {} ==> {}", filename, targetfile.to_str().unwrap());
processed_file_count += 1;
} else {
log::info!(" {} --> {}", filename, targetfile.to_str().unwrap());
processed_file_count += 1;
}
} else {
if move_files {
log::debug!("Moving file {} to {}", filename, targetfile.display());
match std::fs::rename(&filename, targetfile) {
Ok(_) => {
if show_detail_info {
log::info!(" {} ==> {}", filename, targetfile.to_str().unwrap());
}
Err(err) => {
if stop_on_error {
return Err(format!(
"Error: {}. Unable to move file {} to {}. Halting.",
err,
path.to_str().unwrap(),
targetfile.to_str().unwrap()
)
.into());
} else {
log::warn!(
"Unable to move file {} to {}. Continuing.",
path.to_str().unwrap(),
targetfile.to_str().unwrap()
);
skipped_file_count += 1;
}
processed_file_count += 1;
}
Err(err) => {
if stop_on_error {
return Err(format!(
"Error: {}. Unable to move file {} to {}. Halting.",
err,
filename,
targetfile.to_str().unwrap()
)
.into());
} else {
log::warn!(
"Unable to move file {} to {}. Continuing.",
filename,
targetfile.to_str().unwrap()
);
skipped_file_count += 1;
}
}
} else {
// Copy files
log::debug!(
"Copying file {} to {}",
&path.to_str().unwrap(),
&targetfile.display()
);
match std::fs::copy(&path.to_str().unwrap(), targetfile) {
Ok(_) => {
if show_detail_info {
log::info!(
" {} --> {}",
path.to_str().unwrap(),
targetfile.to_str().unwrap()
);
}
processed_file_count += 1;
}
} else {
// Copy files
log::debug!("Copying file {} to {}", &filename, &targetfile.display());
match std::fs::copy(&filename, targetfile) {
Ok(_) => {
if show_detail_info {
log::info!(" {} --> {}", filename, targetfile.to_str().unwrap());
}
Err(err) => {
if stop_on_error {
return Err(format!(
"Error: {}. Unable to copy file {} to {}. Halting.",
err,
path.to_str().unwrap(),
targetfile.to_str().unwrap()
)
.into());
} else {
log::warn!(
"Unable to copy file {} to {}. Continuing.",
path.to_str().unwrap(),
targetfile.to_str().unwrap()
);
skipped_file_count += 1;
}
processed_file_count += 1;
}
Err(err) => {
if stop_on_error {
return Err(format!(
"Error: {}. Unable to copy file {} to {}. Halting.",
err,
filename,
targetfile.to_str().unwrap()
)
.into());
} else {
log::warn!(
"Unable to copy file {} to {}. Continuing.",
filename,
targetfile.to_str().unwrap()
);
skipped_file_count += 1;
}
}
} // if move_files
} else {
log::error!("Unable to process {}", &entry?.to_str().unwrap());
}
}
}
}
} // if move_files
} // if dry_run
} // for filename

// Print summary information
if cli_args.is_present("summary") {
Expand Down

0 comments on commit e399341

Please sign in to comment.