Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new flag for printing the executed commands #1068

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Features

- Added flag `--print-exec` for printing commands before they are ran.


## Bugfixes

Expand Down
10 changes: 10 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,16 @@ pub fn build_app() -> Command<'static> {
"
),
)
.arg(
Arg::new("print-exec")
.long("print-exec")
.help("Print each command ran with -x or -X.")
.requires("exec-batch")
.requires("exec")
.long_help(
"Print each command before it is executed. Must be ran with -x or -X."
),
)
.arg(
Arg::new("batch-size")
.long("batch-size")
Expand Down
8 changes: 8 additions & 0 deletions src/exec/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ pub fn execute_commands<I: Iterator<Item = io::Result<Command>>>(
cmds: I,
out_perm: &Mutex<()>,
enable_output_buffering: bool,
cmd_display_result: Option<String>,
) -> ExitCode {
let mut output_buffer = OutputBuffer::new(out_perm);

if let Some(ref d) = cmd_display_result {
// Print command.
let cmd_bytes: Vec<u8> = d.clone().as_bytes().to_vec();
output_buffer.push(cmd_bytes, vec![]);
}

for result in cmds {
let mut cmd = match result {
Ok(cmd) => cmd,
Expand Down
32 changes: 27 additions & 5 deletions src/exec/job.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex};

use crate::config::Config;
use crate::dir_entry::DirEntry;
use crate::error::print_error;
use crate::exit_codes::{merge_exitcodes, ExitCode};
use crate::walk::WorkerResult;

use super::CommandSet;
use super::{CommandSet, CommandSetDisplay};

/// An event loop that listens for inputs from the `rx` receiver. Each received input will
/// generate a command with the supplied command template. The generated command will then
Expand All @@ -17,6 +18,7 @@ pub fn job(
out_perm: Arc<Mutex<()>>,
show_filesystem_errors: bool,
buffer_output: bool,
config: &Config,
) -> ExitCode {
let mut results: Vec<ExitCode> = Vec::new();
loop {
Expand All @@ -38,9 +40,20 @@ pub fn job(

// Drop the lock so that other threads can read from the receiver.
drop(lock);

// Generate a command, execute it and store its exit code.
results.push(cmd.execute(dir_entry.path(), Arc::clone(&out_perm), buffer_output))
results.push(cmd.execute(
dir_entry.path(),
Arc::clone(&out_perm),
buffer_output,
if cmd.should_print() {
Some(CommandSetDisplay::new(&cmd, &dir_entry, config))
} else {
None
},
))
}

// Returns error in case of any error.
merge_exitcodes(results)
}
Expand All @@ -50,11 +63,12 @@ pub fn batch(
cmd: &CommandSet,
show_filesystem_errors: bool,
limit: usize,
config: &Config,
) -> ExitCode {
let paths = rx
let entries = rx
.into_iter()
.filter_map(|worker_result| match worker_result {
WorkerResult::Entry(dir_entry) => Some(dir_entry.into_path()),
WorkerResult::Entry(dir_entry) => Some(dir_entry),
WorkerResult::Error(err) => {
if show_filesystem_errors {
print_error(err.to_string());
Expand All @@ -63,5 +77,13 @@ pub fn batch(
}
});

cmd.execute_batch(paths, limit)
cmd.execute_batch(
entries,
limit,
if cmd.should_print() {
Some(config)
} else {
None
},
)
}
Loading