Skip to content

Commit

Permalink
feat: Add quiet mode option to suppress progress output during file t…
Browse files Browse the repository at this point in the history
…ransfers
  • Loading branch information
ajclark committed Nov 9, 2024
1 parent 4f4d400 commit 529c56d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ fn main() {
.takes_value(true)
.required(false)
.default_value("22"))
.arg(Arg::new("quiet")
.short('q')
.long("quiet")
.help("Suppress progress output")
.takes_value(false))
.after_help(
"EXAMPLES:\n\
\tPull a file from remote to local:\n\
Expand Down Expand Up @@ -172,11 +177,13 @@ fn main() {

let ssh_key_path = matches.value_of("ssh_key_path");
let max_threads = num_streams;
let quiet_mode = matches.is_present("quiet");

match (source_remote, dest_remote) {
(Some((remote_user, remote_host)), None) => {
// Pull transfer
if let Err(e) = split_and_copy_from_remote(
quiet_mode,
&source_path,
num_streams,
&remote_user,
Expand All @@ -194,6 +201,7 @@ fn main() {
(None, Some((remote_user, remote_host))) => {
// Push transfer
split_and_copy_binary_file(
quiet_mode,
&source_path,
num_streams,
&remote_user,
Expand Down
64 changes: 40 additions & 24 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn get_remote_file_size(
}

pub fn split_and_copy_binary_file(
quiet_mode: bool,
input_file: &str,
num_streams: usize,
remote_user: &str,
Expand All @@ -104,7 +105,9 @@ pub fn split_and_copy_binary_file(
retries: u32,
ssh_port: usize,
) {
println!("Preparing to transfer {}...", input_file);
if !quiet_mode {
println!("Preparing to transfer {}...", input_file);
}

let file_size = match fs::metadata(input_file) {
Ok(metadata) => metadata.len() as usize,
Expand All @@ -120,24 +123,29 @@ pub fn split_and_copy_binary_file(
streams_completed: 0,
}));

println!("Local file size: {} ({})", format_size(file_size), file_size);
if !quiet_mode {
println!("Local file size: {} ({})", format_size(file_size), file_size);

let stream_size = file_size / num_streams;
println!("Using {} streams of approximately {} each",
num_streams,
format_size(stream_size));
let stream_size = file_size / num_streams;
println!("Using {} streams of approximately {} each",
num_streams,
format_size(stream_size));

let extra_bytes = file_size % num_streams;
if extra_bytes > 0 {
println!("Last stream will have an additional {} bytes", extra_bytes);
}
let extra_bytes = file_size % num_streams;
if extra_bytes > 0 {
println!("Last stream will have an additional {} bytes", extra_bytes);
}

println!("Initializing transfer...");
println!("Initializing transfer...");
}

let retry_flag = Arc::new(Mutex::new(vec![false; num_streams]));
let mut handles = Vec::with_capacity(max_threads);

let m = MultiProgress::new();
let stream_size = file_size / num_streams;
let extra_bytes = file_size % num_streams;

let m = if !quiet_mode { MultiProgress::new() } else { MultiProgress::with_draw_target(indicatif::ProgressDrawTarget::hidden()) };
let style = ProgressStyle::with_template(
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
)
Expand Down Expand Up @@ -219,6 +227,7 @@ pub fn split_and_copy_binary_file(
}

pub fn split_and_copy_from_remote(
quiet_mode: bool,
remote_file: &str,
num_streams: usize,
remote_user: &str,
Expand All @@ -229,7 +238,9 @@ pub fn split_and_copy_from_remote(
retries: u32,
ssh_port: usize,
) -> io::Result<()> {
println!("Preparing to transfer {}...", remote_file);
if !quiet_mode {
println!("Preparing to transfer {}...", remote_file);
}

let file_size = get_remote_file_size(
remote_file,
Expand All @@ -245,24 +256,29 @@ pub fn split_and_copy_from_remote(
streams_completed: 0,
}));

println!("Remote file size: {} ({})", format_size(file_size), file_size);
if !quiet_mode {
println!("Remote file size: {} ({})", format_size(file_size), file_size);

let stream_size = file_size / num_streams;
println!("Using {} streams of approximately {} each",
num_streams,
format_size(stream_size));
let stream_size = file_size / num_streams;
println!("Using {} streams of approximately {} each",
num_streams,
format_size(stream_size));

let extra_bytes = file_size % num_streams;
if extra_bytes > 0 {
println!("Last stream will have an additional {} bytes", extra_bytes);
}
let extra_bytes = file_size % num_streams;
if extra_bytes > 0 {
println!("Last stream will have an additional {} bytes", extra_bytes);
}

println!("Initializing transfer...");
println!("Initializing transfer...");
}

let retry_flag = Arc::new(Mutex::new(vec![false; num_streams]));
let mut handles = Vec::with_capacity(max_threads);

let m = MultiProgress::new();
let stream_size = file_size / num_streams;
let extra_bytes = file_size % num_streams;

let m = if !quiet_mode { MultiProgress::new() } else { MultiProgress::with_draw_target(indicatif::ProgressDrawTarget::hidden()) };
let style = ProgressStyle::with_template(
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}",
)
Expand Down

0 comments on commit 529c56d

Please sign in to comment.