shitbox/hashbox/commands/md5sum/mod.rs

49 lines
1.5 KiB
Rust
Raw Permalink Normal View History

use crate::hash::{self, HashType};
use clap::Command;
use md5::{Digest, Md5};
use shitbox::args;
use shitbox::Cmd;
use std::{io, process};
2023-02-03 19:12:40 -05:00
#[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()])
2023-02-03 19:12:40 -05:00
}
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
2023-02-03 19:12:40 -05:00
if let Some(files) = matches.get_many::<String>("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)?;
2023-02-03 19:12:40 -05:00
} else {
let hasher = Md5::new();
let s = hash::compute_hash(f, hasher)?;
println!("{s} {f}");
2023-02-03 19:12:40 -05:00
}
}
if erred > 0 {
println!("md5sum: WARNING: {erred} computed checksum did NOT match");
process::exit(1);
}
}
Ok(())
}
fn path(&self) -> Option<shitbox::Path> {
Some(shitbox::Path::UsrBin)
2023-02-03 19:12:40 -05:00
}
}