|
1 |
| -use std::io::{stdout, Error, ErrorKind, Read, Result, Write}; |
| 1 | +use std::io::{stdin, stdout, Error, ErrorKind, Read, Write}; |
2 | 2 |
|
3 | 3 | use crate::event::{Event, Kind};
|
4 | 4 | use crate::key::Pair;
|
5 | 5 | use crate::message::MessageRequest;
|
6 | 6 | use crate::request::Request;
|
7 | 7 | use crate::Hex;
|
| 8 | +use anyhow::Result; |
| 9 | +use clap::{Parser, Subcommand}; |
| 10 | + |
| 11 | +#[derive(Parser)] |
| 12 | +#[command(author, version, about, long_about)] |
| 13 | +pub struct Args { |
| 14 | + #[command(subcommand)] |
| 15 | + command: Command, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Subcommand)] |
| 19 | +enum Command { |
| 20 | + /// Verify and generate events |
| 21 | + Event { |
| 22 | + #[command(subcommand)] |
| 23 | + subcommand: EventCommand, |
| 24 | + }, |
| 25 | + /// Generate requests |
| 26 | + Request { |
| 27 | + #[arg(short, long)] |
| 28 | + ids: Vec<Hex>, |
| 29 | + #[arg(short, long)] |
| 30 | + authors: Vec<Hex>, |
| 31 | + #[arg(short, long)] |
| 32 | + kinds: Vec<u32>, |
| 33 | + #[arg(short, long)] |
| 34 | + e: Vec<Hex>, |
| 35 | + #[arg(short, long)] |
| 36 | + p: Vec<Hex>, |
| 37 | + #[arg(short, long)] |
| 38 | + since: Option<u32>, |
| 39 | + #[arg(short, long)] |
| 40 | + until: Option<u32>, |
| 41 | + #[arg(short, long)] |
| 42 | + limit: Option<u16>, |
| 43 | + }, |
| 44 | + /// Generate message requests |
| 45 | + MessageRequest { |
| 46 | + #[command(subcommand)] |
| 47 | + subcommand: MessageRequestCommand, |
| 48 | + }, |
| 49 | + /// Print key |
| 50 | + Key, |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Subcommand)] |
| 54 | +pub enum EventCommand { |
| 55 | + /// Verifies an event on stdin |
| 56 | + Verify, |
| 57 | + /// Output a new event to stdout |
| 58 | + Generate { |
| 59 | + #[arg(short, long)] |
| 60 | + kind: Kind, |
| 61 | + content: String, |
| 62 | + }, |
| 63 | + /// Output a new set metadata event to stdout |
| 64 | + SetMetadata { |
| 65 | + name: String, |
| 66 | + about: String, |
| 67 | + picture: String, |
| 68 | + }, |
| 69 | + /// Output a new text note to stdout |
| 70 | + TextNote { content: String }, |
| 71 | + /// Output a new recommend relay to stdout |
| 72 | + RecommendRelay { relay: String }, |
| 73 | +} |
| 74 | + |
| 75 | +// #[derive(Subcommand)] |
| 76 | +// pub enum RequestCommand {} |
| 77 | + |
| 78 | +#[derive(Subcommand)] |
| 79 | +pub enum MessageRequestCommand { |
| 80 | + Event, |
| 81 | + Request { id: String }, |
| 82 | +} |
| 83 | + |
| 84 | +pub fn handle_args(args: Args, pair: &Pair) -> Result<()> { |
| 85 | + match args.command { |
| 86 | + Command::Event { subcommand } => match subcommand { |
| 87 | + EventCommand::Verify => verify_event(stdin())?, |
| 88 | + EventCommand::Generate { kind, content } => generate_event(kind, &content)?, |
| 89 | + EventCommand::SetMetadata { |
| 90 | + name, |
| 91 | + about, |
| 92 | + picture, |
| 93 | + } => set_metadata_event(&name, &about, &picture)?, |
| 94 | + EventCommand::TextNote { content } => text_note_event(&content)?, |
| 95 | + EventCommand::RecommendRelay { relay } => recommend_relay_event(&relay)?, |
| 96 | + }, |
| 97 | + Command::Request { |
| 98 | + ids, |
| 99 | + authors, |
| 100 | + kinds, |
| 101 | + e, |
| 102 | + p, |
| 103 | + since, |
| 104 | + until, |
| 105 | + limit, |
| 106 | + } => write_request(stdout(), ids, authors, kinds, e, p, since, until, limit)?, |
| 107 | + Command::MessageRequest { subcommand } => match subcommand { |
| 108 | + MessageRequestCommand::Event => event_message_request(stdin(), stdout())?, |
| 109 | + MessageRequestCommand::Request { id } => { |
| 110 | + request_message_request(stdin(), stdout(), id)? |
| 111 | + } |
| 112 | + }, |
| 113 | + Command::Key => print_key(&mut stdout(), pair)?, |
| 114 | + }; |
| 115 | + Ok(()) |
| 116 | +} |
8 | 117 |
|
9 | 118 | pub fn io_error(message: &str) -> Error {
|
10 | 119 | Error::new(ErrorKind::Other, message)
|
@@ -99,3 +208,8 @@ pub fn request_message_request<R: Read, W: Write>(reader: R, writer: W, id: Stri
|
99 | 208 | serde_json::to_writer(writer, &message)?;
|
100 | 209 | Ok(())
|
101 | 210 | }
|
| 211 | + |
| 212 | +pub fn print_key<W: Write>(writer: &mut W, pair: &Pair) -> Result<()> { |
| 213 | + writer.write_all(pair.secret_key().unwrap().display_secret_as_nsec().as_ref())?; |
| 214 | + Ok(()) |
| 215 | +} |
0 commit comments