Skip to content

Commit

Permalink
build: add anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhall88 committed Nov 21, 2023
1 parent eec31e5 commit 9a2f999
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "nohuman"
description = "Remove human reads from a sequencing run"
version = "0.1.0"
edition = "2021"

Expand All @@ -9,4 +10,5 @@ edition = "2021"
clap = { version = "4.4.7", features = ["derive"] }
log = "0.4.20"
env_logger = "0.10.0"
anyhow = "1.0.75"

9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::process::Command;
use std::io::{self, Write};
use std::process::Command;

pub struct CommandRunner {
pub command: String,
Expand All @@ -13,9 +13,7 @@ impl CommandRunner {
}

pub fn run(&self, args: &[&str]) -> io::Result<()> {
let output = Command::new(&self.command)
.args(args)
.output()?;
let output = Command::new(&self.command).args(args).output()?;

if !output.status.success() {
let error_message = String::from_utf8_lossy(&output.stderr);
Expand All @@ -34,7 +32,6 @@ impl CommandRunner {
}
}


// add tests for each of the CommandRunner methods
#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -71,4 +68,4 @@ mod tests {
let command = CommandRunner::new("not-a-real-command");
assert!(!command.is_executable());
}
}
}
30 changes: 17 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use clap::{Parser};
use log::{info, warn, LevelFilter, error, debug};
use nohuman::CommandRunner;
use anyhow::{anyhow, bail, Context, Result};
use clap::Parser;
use env_logger::Builder;
use log::{debug, error, info, warn, LevelFilter};
use nohuman::CommandRunner;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand All @@ -15,7 +16,7 @@ struct Args {
verbose: bool,
}

fn main() {
fn main() -> Result<()> {
let args = Args::parse();

// Initialize logger
Expand Down Expand Up @@ -46,15 +47,18 @@ fn main() {
}
}

if args.check {
if !missing_commands.is_empty() {
error!("The following dependencies are missing:");
for cmd in missing_commands {
error!("{}", cmd);
}
std::process::exit(1);
if !missing_commands.is_empty() {
error!("The following dependencies are missing:");
for cmd in missing_commands {
error!("{}", cmd);
}
info!("Dependencies are available");
std::process::exit(0);
bail!("Missing dependencies");
}

if args.check {
info!("All dependencies are available");
return Ok(());
}

Ok(())
}

0 comments on commit 9a2f999

Please sign in to comment.