Skip to content

Commit

Permalink
Adding --dry-run and --trace options
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Jul 5, 2024
1 parent b646e21 commit 5a0e1cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
pub fn run(&self, status_writer: &mut impl Write, log_writer: &mut impl Write, options: &Options) -> ZinnResult<String> {
// 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() {
Expand All @@ -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() {
Expand All @@ -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")
Expand All @@ -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);
}
Expand Down Expand Up @@ -261,6 +274,10 @@ impl InnerJobRealization {
pub fn name(&self) -> &str {
&self.name
}

pub fn cmd(&self) -> &str {
&self.run
}
}


Expand Down
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}


Expand All @@ -85,6 +95,8 @@ impl Args {
Options {
verbose: self.verbose,
force: self.force_rebuild,
trace: self.trace,
dry_run: self.dry_run,
}
}
}
Expand Down

0 comments on commit 5a0e1cf

Please sign in to comment.