use super::Cmd; use crate::{ args, hash::{self, HashType}, }; use clap::Command; use md5::{Digest, Md5}; use std::{io, process}; #[derive(Debug, Default)] pub struct Md5sum; impl Cmd for Md5sum { fn cli(&self) -> clap::Command { Command::new("md5sum") .about("compute and check MD5 message digest") .author("Nathan Fisher") .version(env!("CARGO_PKG_VERSION")) .args([args::check(), args::file()]) } fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box> { let Some(matches) = matches else { return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); }; if let Some(files) = matches.get_many::("file") { let mut erred = 0; for f in files { if matches.get_flag("check") { if f == "-" { return Err( io::Error::new(io::ErrorKind::Other, "no file specified").into() ); } hash::check_sums(f, HashType::Md5, &mut erred)?; } else { let hasher = Md5::new(); let s = hash::compute_hash(f, hasher)?; println!("{s} {f}"); } } if erred > 0 { println!("md5sum: WARNING: {erred} computed checksum did NOT match"); process::exit(1); } } Ok(()) } fn path(&self) -> Option { Some(crate::Path::UsrBin) } }