Skip to content

Commit

Permalink
Add output flag to send output to file
Browse files Browse the repository at this point in the history
  • Loading branch information
w3irdrobot committed Oct 29, 2024
1 parent d78a1eb commit 7e24fc2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 42 deletions.
38 changes: 17 additions & 21 deletions crates/web5_cli/src/dids/create.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::utils::warn_if_not_root;
use clap::Subcommand;
use std::sync::Arc;
use url::Url;
use web5::dids::data_model::service::Service;
use web5::{
crypto::key_managers::in_memory_key_manager::InMemoryKeyManager,
dids::{
Expand All @@ -12,8 +14,6 @@ use web5::{
portable_did::PortableDid,
},
};
use web5::dids::data_model::service::Service;
use crate::utils::warn_if_not_root;

#[derive(Subcommand, Debug)]
pub enum Commands {
Expand All @@ -27,10 +27,7 @@ pub enum Commands {
domain: String,
#[arg(long)]
service_endpoint: Option<String>,
#[arg(
long = "service-endpoint-type",
default_value = "LinkedDomains"
)]
#[arg(long = "service-endpoint-type", default_value = "LinkedDomains")]
service_endpoint_type: String,
#[arg(long)]
no_indent: bool,
Expand All @@ -40,10 +37,7 @@ pub enum Commands {
Dht {
#[arg(long)]
service_endpoint: Option<String>,
#[arg(
long = "service-endpoint-type",
default_value = "LinkedDomains"
)]
#[arg(long = "service-endpoint-type", default_value = "LinkedDomains")]
service_endpoint_type: String,
#[arg(long)]
no_publish: bool,
Expand All @@ -54,7 +48,12 @@ pub enum Commands {
},
}

fn print_portable_did(portable_did: PortableDid, no_indent: &bool, json_escape: &bool) {
fn print_portable_did(
mut sink: impl std::io::Write,
portable_did: PortableDid,
no_indent: &bool,
json_escape: &bool,
) {
let mut output_str = match no_indent {
true => serde_json::to_string(&portable_did).unwrap(),
false => serde_json::to_string_pretty(&portable_did).unwrap(),
Expand All @@ -64,11 +63,11 @@ fn print_portable_did(portable_did: PortableDid, no_indent: &bool, json_escape:
output_str = output_str.replace('"', "\\\"");
}

println!("{}", output_str);
writeln!(sink, "{}", output_str).unwrap();
}

impl Commands {
pub async fn command(&self) {
pub async fn command(&self, sink: impl std::io::Write) {
match self {
Commands::Jwk {
no_indent,
Expand All @@ -86,7 +85,7 @@ impl Commands {

let portable_did = bearer_did.to_portable_did(key_manager).unwrap();

print_portable_did(portable_did, no_indent, json_escape);
print_portable_did(sink, portable_did, no_indent, json_escape);
}
Commands::Web {
domain,
Expand Down Expand Up @@ -129,12 +128,11 @@ impl Commands {
did_web_create_options.service = Some(vec![service]);
}

let bearer_did = DidWeb::create(domain, Some(did_web_create_options))
.unwrap();
let bearer_did = DidWeb::create(domain, Some(did_web_create_options)).unwrap();

let portable_did = bearer_did.to_portable_did(key_manager).unwrap();

print_portable_did(portable_did, no_indent, json_escape);
print_portable_did(sink, portable_did, no_indent, json_escape);
}
Commands::Dht {
service_endpoint,
Expand Down Expand Up @@ -163,13 +161,11 @@ impl Commands {
did_dht_create_options.service = Some(vec![service]);
}

let bearer_did = DidDht::create(Some(did_dht_create_options))
.await
.unwrap();
let bearer_did = DidDht::create(Some(did_dht_create_options)).await.unwrap();

let portable_did = bearer_did.to_portable_did(key_manager).unwrap();

print_portable_did(portable_did, no_indent, json_escape);
print_portable_did(sink, portable_did, no_indent, json_escape);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/web5_cli/src/dids/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ pub enum Commands {
}

impl Commands {
pub async fn command(&self) {
pub async fn command(&self, mut sink: impl std::io::Write) {
match self {
Commands::Resolve { uri } => {
let resolution_result = ResolutionResult::resolve(uri).await;
match &resolution_result.resolution_metadata.error {
Some(e) => println!("{:?} {}", e, e),
Some(e) => eprintln!("{:?} {}", e, e),
None => match &resolution_result.document {
None => println!(
None => eprintln!(
"{:?} {}",
ResolutionMetadataError::InternalError,
ResolutionMetadataError::InternalError
),
Some(document) => match serde_json::to_string_pretty(&document) {
Ok(s) => println!("{}", s),
Err(_) => println!(
Ok(s) => writeln!(sink, "{}", s).unwrap(),
Err(_) => eprintln!(
"{:?} {}",
ResolutionMetadataError::InternalError,
ResolutionMetadataError::InternalError
Expand All @@ -40,7 +40,7 @@ impl Commands {
},
}
}
Commands::Create { did_create_command } => did_create_command.command().await,
Commands::Create { did_create_command } => did_create_command.command(sink).await,
}
}
}
22 changes: 18 additions & 4 deletions crates/web5_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod test;
mod utils;
mod vcs;

use std::fs::File;

use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
Expand All @@ -14,6 +16,9 @@ use clap::{Parser, Subcommand};
struct Cli {
#[command(subcommand)]
command: Commands,
/// A file to output command output to.
#[arg(long, global = true)]
output: Option<String>,
}

#[derive(Subcommand, Debug)]
Expand All @@ -37,9 +42,18 @@ enum Commands {
async fn main() {
let cli = Cli::parse();

match cli.command {
Commands::Did { did_command } => did_command.command().await,
Commands::Vc { vc_command } => vc_command.command().await,
Commands::Pd { pd_command } => pd_command.command().await,
if let Some(path) = cli.output {
let file = File::create(path).unwrap();
command(cli.command, file).await;
} else {
command(cli.command, std::io::stdout()).await;
}
}

async fn command(command: Commands, sink: impl std::io::Write) {
match command {
Commands::Did { did_command } => did_command.command(sink).await,
Commands::Vc { vc_command } => vc_command.command(sink).await,
Commands::Pd { pd_command } => pd_command.command(sink).await,
}
}
4 changes: 2 additions & 2 deletions crates/web5_cli/src/pds/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,6 @@ fn str_to_option_string(value: &str) -> Option<String> {
(!value.is_empty()).then(|| value.to_string())
}

pub fn run_create_command(args: CreatePresentationDefinition) {
println!("{}", args.get_output());
pub fn run_create_command(args: CreatePresentationDefinition, mut sink: impl std::io::Write) {
writeln!(sink, "{}", args.get_output()).unwrap();
}
4 changes: 2 additions & 2 deletions crates/web5_cli/src/pds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub enum Commands {
}

impl Commands {
pub async fn command(self) {
pub async fn command(self, sink: impl std::io::Write) {
match self {
Commands::Create(args) => create::run_create_command(args),
Commands::Create(args) => create::run_create_command(args, sink),
};
}
}
14 changes: 7 additions & 7 deletions crates/web5_cli/src/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub enum Commands {
}

impl Commands {
pub async fn command(self) {
pub async fn command(self, mut sink: impl std::io::Write) {
match self {
Commands::Create {
credential_subject_id,
Expand Down Expand Up @@ -189,12 +189,12 @@ impl Commands {
output_str = output_str.replace('"', "\\\"");
}

println!("{}", output_str);
writeln!(sink, "{}", output_str).unwrap();

if let Some(portable_did) = portable_did {
let bearer_did = BearerDid::from_portable_did(portable_did).unwrap();
let vc_jwt = vc.sign(&bearer_did, None).unwrap();
println!("\n{}", vc_jwt);
writeln!(sink, "\n{}", vc_jwt).unwrap();
}
}
Commands::Verify {
Expand All @@ -203,11 +203,11 @@ impl Commands {
json_escape,
} => match VerifiableCredential::from_vc_jwt(&vc_jwt, true).await {
Err(e) => {
println!("\n❌ Verfication failed\n");
println!("{:?} {}", e, e);
eprintln!("\n❌ Verfication failed\n");
eprintln!("{:?} {}", e, e);
}
Ok(vc) => {
println!("\n✅ Verfied\n");
writeln!(sink, "\n✅ Verfied\n").unwrap();

let mut output_str = match no_indent {
true => serde_json::to_string(&vc).unwrap(),
Expand All @@ -218,7 +218,7 @@ impl Commands {
output_str = output_str.replace('"', "\\\"");
}

println!("{}", output_str);
writeln!(sink, "{}", output_str).unwrap();
}
},
}
Expand Down

0 comments on commit 7e24fc2

Please sign in to comment.