shitbox/src/cmd/sha1sum/mod.rs

51 lines
1.5 KiB
Rust
Raw Normal View History

use super::Cmd;
use crate::{
args,
hash::{self, HashType},
};
use clap::Command;
use sha1::{Digest, Sha1};
use std::{io, process};
#[derive(Debug, Default)]
pub struct Sha1sum;
impl Cmd for Sha1sum {
fn cli(&self) -> clap::Command {
Command::new("sha1sum")
.about("compute and check SHA1 message digest")
.author("Nathan Fisher")
.version(env!("CARGO_PKG_VERSION"))
.args([args::check(), args::file()])
}
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
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::Sha1, &mut erred)?;
} else {
let hasher = Sha1::new();
let s = hash::compute_hash(f, hasher)?;
println!("{s} {f}");
}
}
if erred > 0 {
println!("sha1sum: WARNING: {erred} computed checksum did NOT match");
process::exit(1);
}
}
Ok(())
}
fn path(&self) -> Option<crate::Path> {
Some(crate::Path::UsrBin)
}
}