Skip to content

Commit

Permalink
feat: allow to read from stdin for verify cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
blelump committed Nov 29, 2024
1 parent ba18723 commit 910d4f0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/subcommands/data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Subcommand;
use std::io::{self, IsTerminal, Read};

use crate::{
expand,
Expand Down Expand Up @@ -26,7 +27,7 @@ pub enum DataCommand {
#[arg(short, long)]
oobi: Vec<String>,
#[arg(short, long)]
message: String,
message: Option<String>,
},
/// Presents CESR data in a human-readable format
Expand {
Expand Down Expand Up @@ -56,18 +57,52 @@ pub async fn process_data_command(command: DataCommand) -> Result<(), CliError>
oobi,
message,
} => {
let status = match handle_verify(
&alias,
&oobi.iter().map(|e| e.as_str()).collect::<Vec<_>>(),
message,
)
.await
{
Ok(result) => VerificationStatus::from(result),
Err(VerifyHandleError::NoWatchersConfigured(id)) => {
return Err(CliError::NoWatchers(id))
let status = match message {
Some(message) => {
println!("{}", message);
match handle_verify(
&alias,
&oobi.iter().map(|e| e.as_str()).collect::<Vec<_>>(),
message,
)
.await
{
Ok(result) => VerificationStatus::from(result),
Err(VerifyHandleError::NoWatchersConfigured(id)) => {
return Err(CliError::NoWatchers(id))
}
Err(e) => VerificationStatus::from(e),
}
}
None => {
if io::stdin().is_terminal() {
eprintln!(
"Error: No input provided. Provide an argument or pipe data via stdin."
);
std::process::exit(1);
}

let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer).map_err(|e| {
CliError::Verification(VerificationStatus::Error {
description: format!("Failed to read from stdin: {}", e),
})
})?;

match handle_verify(
&alias,
&oobi.iter().map(|e| e.as_str()).collect::<Vec<_>>(),
buffer,
)
.await
{
Ok(result) => VerificationStatus::from(result),
Err(VerifyHandleError::NoWatchersConfigured(id)) => {
return Err(CliError::NoWatchers(id))
}
Err(e) => VerificationStatus::from(e),
}
}
Err(e) => VerificationStatus::from(e),
};
match &status {
VerificationStatus::Ok { description: _ } => println!("{}", &status),
Expand Down
1 change: 1 addition & 0 deletions src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub async fn handle_verify(
},
};
}
let message = message.trim();

// Parse cesr stream of message
let (rest, cesr) = cesrox::parse(message.as_bytes()).unwrap();
Expand Down

0 comments on commit 910d4f0

Please sign in to comment.