2023-02-05 23:50:59 -05:00
|
|
|
use crate::hash::{self, HashType};
|
2023-02-04 00:56:02 -05:00
|
|
|
use clap::Command;
|
|
|
|
use sha1::{Digest, Sha1};
|
2023-02-05 23:50:59 -05:00
|
|
|
use shitbox::{args, Cmd};
|
2023-02-04 00:56:02 -05:00
|
|
|
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()])
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
|
2023-02-04 00:56:02 -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::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(())
|
|
|
|
}
|
|
|
|
|
2023-02-05 23:50:59 -05:00
|
|
|
fn path(&self) -> Option<shitbox::Path> {
|
|
|
|
Some(shitbox::Path::UsrBin)
|
2023-02-04 00:56:02 -05:00
|
|
|
}
|
|
|
|
}
|