Skip to content

Commit

Permalink
feat: Removed the '-s' short flag and replaced it with a new 'Short' …
Browse files Browse the repository at this point in the history
…format

Signed-off-by: David Pollak <[email protected]>
  • Loading branch information
dpp committed Feb 28, 2024
1 parent 22af5dc commit 761124f
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions omnibor/src/bin/omnibor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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)]
Expand All @@ -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,
}
Expand All @@ -153,13 +148,15 @@ enum Format {
#[default]
Plain,
Json,
Short,
}

impl Display for Format {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Format::Plain => write!(f, "plain"),
Format::Json => write!(f, "json"),
Format::Short => write!(f, "short"),
}
}
}
Expand All @@ -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)),
}
}
Expand Down Expand Up @@ -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<E: Into<Error>>(error: E, format: Format) -> MsgOrEnd {
Expand All @@ -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 })),
}
}
Expand All @@ -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()})),
}
}
Expand Down Expand Up @@ -311,9 +311,9 @@ async fn run_id(tx: &Sender<MsgOrEnd>, 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
}
}

Expand Down Expand Up @@ -341,7 +341,7 @@ async fn run_find(tx: &Sender<MsgOrEnd>, 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(());
}
}
Expand All @@ -361,7 +361,6 @@ async fn id_directory(
path: &Path,
format: Format,
hash: SelectedHash,
short: bool
) -> Result<()> {
let mut entries = WalkDir::new(path);

Expand All @@ -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?;
}
}
}
Expand All @@ -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(())
}

Expand Down

0 comments on commit 761124f

Please sign in to comment.