From 5a0e1cfb177d7633e76b3322bb9cee3d80d37ea4 Mon Sep 17 00:00:00 2001 From: jzbor Date: Fri, 5 Jul 2024 19:44:45 +0200 Subject: [PATCH] Adding --dry-run and --trace options --- src/job.rs | 23 ++++++++++++++++++++--- src/main.rs | 12 ++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/job.rs b/src/job.rs index 8fb4b44..8e6da2d 100644 --- a/src/job.rs +++ b/src/job.rs @@ -168,7 +168,15 @@ impl JobDescription { } impl InnerJobRealization { - pub fn run(&self, status_writer: &mut impl Write, log_writer: &mut impl Write, args: &Options) -> ZinnResult { + pub fn run(&self, status_writer: &mut impl Write, log_writer: &mut impl Write, options: &Options) -> ZinnResult { + // skip if dry run + if options.dry_run { + if options.trace { + let _ = writeln!(log_writer, "{}", self.cmd()); + } + return Ok(String::from("(dry run)")); + } + // check if all input files exist for file in &self.inputs { if !Path::new(file).exists() { @@ -177,7 +185,7 @@ impl InnerJobRealization { } // check if any input file is newer than any output file - if !args.force && !self.inputs.is_empty() && !self.outputs.is_empty() { + if !options.force && !self.inputs.is_empty() && !self.outputs.is_empty() { let mut dirty = false; for output in &self.outputs { if !Path::new(output).exists() { @@ -199,6 +207,11 @@ impl InnerJobRealization { } } + // print out trace + if options.trace { + let _ = writeln!(log_writer, "{}", self.cmd()); + } + let (io_reader, io_writer) = os_pipe::pipe()?; let mut process = Command::new("sh") @@ -222,7 +235,7 @@ impl InnerJobRealization { for line in BufReader::new(io_reader).lines().map_while(Result::ok) { let _ = writeln!(status_writer, "{}", line); - if args.verbose { + if options.verbose { if let Some(line) = last_line.take() { let _ = writeln!(log_writer, "{}: {}", self, line); } @@ -261,6 +274,10 @@ impl InnerJobRealization { pub fn name(&self) -> &str { &self.name } + + pub fn cmd(&self) -> &str { + &self.run + } } diff --git a/src/main.rs b/src/main.rs index 776f014..53182bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,12 +56,22 @@ struct Args { /// List all jobs with their parameters #[clap(long)] list: bool, + + /// Output commands before executing + #[clap(short, long)] + trace: bool, + + /// Don't actually execute the commands + #[clap(long)] + dry_run: bool, } #[derive(Clone, Debug, Serialize, Deserialize)] struct Options { verbose: bool, force: bool, + trace: bool, + dry_run: bool, } @@ -85,6 +95,8 @@ impl Args { Options { verbose: self.verbose, force: self.force_rebuild, + trace: self.trace, + dry_run: self.dry_run, } } }