From d0bf2d71fe18fefe754bbd88ea963a37beb0cfe3 Mon Sep 17 00:00:00 2001 From: David Pollak Date: Wed, 28 Feb 2024 16:49:25 -0500 Subject: [PATCH] Removed the '-s' short flag and replaced it with a new 'Short' format Signed-off-by: David Pollak --- omnibor/src/bin/omnibor.rs | 40 ++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/omnibor/src/bin/omnibor.rs b/omnibor/src/bin/omnibor.rs index 714775b..6453adc 100644 --- a/omnibor/src/bin/omnibor.rs +++ b/omnibor/src/bin/omnibor.rs @@ -13,7 +13,6 @@ use omnibor::Sha256; use serde_json::json; use serde_json::Value as JsonValue; use smart_default::SmartDefault; -use tokio::time::sleep; use std::default::Default; use std::fmt::Display; use std::fmt::Formatter; @@ -30,6 +29,7 @@ use tokio::io::AsyncWrite; use tokio::io::AsyncWriteExt as _; use tokio::sync::mpsc; use tokio::sync::mpsc::Sender; +use tokio::time::sleep; use url::Url; #[tokio::main] @@ -72,7 +72,6 @@ async fn main() -> ExitCode { return_code = ExitCode::FAILURE; } - // send a message to end the printing tx.send(MsgOrEnd::End).await.unwrap(); @@ -122,17 +121,13 @@ struct IdArgs { /// Path to identify path: PathBuf, - /// Output format (can be "plain" or "json") + /// Output format (can be "plain", "short", or "json") #[arg(short = 'f', long = "format", default_value_t)] format: Format, /// Hash algorithm (can be "sha256") #[arg(short = 'H', long = "hash", default_value_t)] hash: SelectedHash, - - /// Should the messages be short (just contain the gitoid)? - #[arg(short = 's', long = "short")] - short: bool, } #[derive(Debug, Args)] @@ -143,7 +138,7 @@ struct FindArgs { /// The root path to search under path: PathBuf, - /// Output format (can be "plain" or "json") + /// Output format (can be "plain", "short", or "json") #[arg(short = 'f', long = "format", default_value_t)] format: Format, } @@ -153,6 +148,7 @@ enum Format { #[default] Plain, Json, + Short, } impl Display for Format { @@ -160,6 +156,7 @@ impl Display for Format { match self { Format::Plain => write!(f, "plain"), Format::Json => write!(f, "json"), + Format::Short => write!(f, "short"), } } } @@ -171,6 +168,7 @@ impl FromStr for Format { match s { "plain" => Ok(Format::Plain), "json" => Ok(Format::Json), + "short" => Ok(Format::Short), _ => Err(anyhow!("unknown format '{}'", s)), } } @@ -208,8 +206,8 @@ enum MsgOrEnd { } impl MsgOrEnd { - fn id(path: &Path, url: &Url, format: Format, short: bool) -> Self { - MsgOrEnd::Message(Msg::id(path, url, format, short)) + fn id(path: &Path, url: &Url, format: Format) -> Self { + MsgOrEnd::Message(Msg::id(path, url, format)) } fn error>(error: E, format: Format) -> MsgOrEnd { @@ -224,14 +222,14 @@ struct Msg { } impl Msg { - fn id(path: &Path, url: &Url, format: Format, short: bool) -> Self { + fn id(path: &Path, url: &Url, format: Format) -> Self { let status = Status::Success; let path = path.display().to_string(); let url = url.to_string(); match format { - Format::Plain if !short => Msg::plain(status, &format!("{} => {}", path, url)), - Format::Plain => Msg::plain(status, &format!("{}", url)), + Format::Plain => Msg::plain(status, &format!("{} => {}", path, url)), + Format::Short => Msg::plain(status, &format!("{}", url)), Format::Json => Msg::json(status, json!({ "path": path, "id": url })), } } @@ -241,7 +239,9 @@ impl Msg { let status = Status::Error; match format { - Format::Plain => Msg::plain(status, &format!("error: {}", error.to_string())), + Format::Plain | Format::Short => { + Msg::plain(status, &format!("error: {}", error.to_string())) + } Format::Json => Msg::json(status, json!({"error": error.to_string()})), } } @@ -311,9 +311,9 @@ async fn run_id(tx: &Sender, args: &IdArgs) -> Result<()> { let mut file = open_async_file(&args.path).await?; if file_is_dir(&file).await? { - id_directory(tx, &args.path, args.format, args.hash, args.short).await + id_directory(tx, &args.path, args.format, args.hash).await } else { - id_file(tx, &mut file, &args.path, args.format, args.hash, args.short).await + id_file(tx, &mut file, &args.path, args.format, args.hash).await } } @@ -341,7 +341,7 @@ async fn run_find(tx: &Sender, args: &FindArgs) -> Result<()> { let file_url = hash_file(SelectedHash::Sha256, &mut file, &path).await?; if url == file_url { - tx.send(MsgOrEnd::id(&path, &url, *format, false)).await?; + tx.send(MsgOrEnd::id(&path, &url, *format)).await?; return Ok(()); } } @@ -361,7 +361,6 @@ async fn id_directory( path: &Path, format: Format, hash: SelectedHash, - short: bool ) -> Result<()> { let mut entries = WalkDir::new(path); @@ -377,7 +376,7 @@ async fn id_directory( } let mut file = open_async_file(&path).await?; - id_file(tx, &mut file, &path, format, hash, short).await?; + id_file(tx, &mut file, &path, format, hash).await?; } } } @@ -392,10 +391,9 @@ async fn id_file( path: &Path, format: Format, hash: SelectedHash, - short: bool ) -> Result<()> { let url = hash_file(hash, file, &path).await?; - tx.send(MsgOrEnd::id(path, &url, format, short)).await?; + tx.send(MsgOrEnd::id(path, &url, format)).await?; Ok(()) }