From 9a2f999ba78d047376f5c7937e4d10cc850707de Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Tue, 21 Nov 2023 10:39:45 +1000 Subject: [PATCH] build: add anyhow --- Cargo.lock | 7 +++++++ Cargo.toml | 2 ++ src/lib.rs | 9 +++------ src/main.rs | 30 +++++++++++++++++------------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33a23b0..9302efb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + [[package]] name = "bitflags" version = "2.4.1" @@ -191,6 +197,7 @@ checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" name = "nohuman" version = "0.1.0" dependencies = [ + "anyhow", "clap", "env_logger", "log", diff --git a/Cargo.toml b/Cargo.toml index 8ac88fe..9a9cd86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [package] name = "nohuman" +description = "Remove human reads from a sequencing run" version = "0.1.0" edition = "2021" @@ -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" diff --git a/src/lib.rs b/src/lib.rs index c076dd7..2424cf3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ -use std::process::Command; use std::io::{self, Write}; +use std::process::Command; pub struct CommandRunner { pub command: String, @@ -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); @@ -34,7 +32,6 @@ impl CommandRunner { } } - // add tests for each of the CommandRunner methods #[cfg(test)] mod tests { @@ -71,4 +68,4 @@ mod tests { let command = CommandRunner::new("not-a-real-command"); assert!(!command.is_executable()); } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 1aebd1e..06609cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)] @@ -15,7 +16,7 @@ struct Args { verbose: bool, } -fn main() { +fn main() -> Result<()> { let args = Args::parse(); // Initialize logger @@ -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(()) }