diff --git a/Cargo.toml b/Cargo.toml index dda66fb..ad79bba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ anyhow = { version = "1.0.100" } clap = { version = "4.5.49", features = ["derive"] } config = { version = "0.15.16", default-features = true } dotenvy = { version = "0.15" } +dirs = {version = "6.0.0"} global_utils = { path = "./crates/global_utils" } hex = { version = "0.4.3" } itertools = { version = "0.14.0" } diff --git a/nostr_options_cli/Cargo.toml b/nostr_options_cli/Cargo.toml index 3cb588b..791f153 100644 --- a/nostr_options_cli/Cargo.toml +++ b/nostr_options_cli/Cargo.toml @@ -10,3 +10,8 @@ tokio-tungstenite = { workspace = true } futures-util = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true } +clap = { workspace = true } +dirs = { workspace = true } +tracing = { workspace = true } +thiserror = { workspace = true } + diff --git a/nostr_options_cli/src/cli_processor.rs b/nostr_options_cli/src/cli_processor.rs new file mode 100644 index 0000000..53bb4f6 --- /dev/null +++ b/nostr_options_cli/src/cli_processor.rs @@ -0,0 +1,124 @@ +use clap::{Parser, Subcommand}; +use std::path::PathBuf; +use tracing::instrument; + +use crate::utils::{default_key_path, default_relays_path, write_into_stdout}; + +#[derive(Parser)] +pub struct Cli { + #[arg(short = 'k', long)] + key_path: Option, + #[command(subcommand)] + command: Command, +} + +#[derive(Debug, Subcommand)] +enum Command { + Maker { + #[command(subcommand)] + action: MakerCommand, + }, + + Taker { + #[command(subcommand)] + action: TakerCommand, + }, +} + +#[derive(Debug, Subcommand)] +enum MakerCommand { + CreateOrder { + #[arg(short = 'm', long)] + message: String, + + #[arg(short = 'r', long)] + relays_path: Option, + }, + + GetOrderReply { + #[arg(short = 'i', long)] + id: String, + + #[arg(short = 'r', long)] + relays_path: Option, + }, +} + +#[derive(Debug, Subcommand)] +enum TakerCommand { + ListOrders { + #[arg(short = 'r', long)] + relays_path: Option, + }, + + ReplyOrder { + #[arg(short = 'i', long)] + id: String, + }, +} + +impl Cli { + #[instrument(skip(self))] + pub fn process(self) -> crate::error::Result<()> { + let msg = { + match self.command { + Command::Maker { action } => match action { + MakerCommand::CreateOrder { message, relays_path } => { + let key_path = self.key_path.unwrap_or(default_key_path()); + let relays_path = relays_path.unwrap_or(default_relays_path()); + format!( + "Maker: Create Order\n message: {}\n key_path: {}\n relays_path: {}", + message, + key_path.display(), + relays_path.display() + ) + + // TODO: + //processor.create_order(message, key_path, relays_path).await?; + } + + MakerCommand::GetOrderReply { id, relays_path } => { + let key_path = self.key_path.unwrap_or(default_key_path()); + let relays_path = relays_path.unwrap_or(default_relays_path()); + format!( + "Maker: Get Order Reply\n id: {}\n key_path: {}\n relays_path: {}", + id, + key_path.display(), + relays_path.display() + ) + + // TODO: + //processor.get_order_reply(id, key_path, relays_path).await?; + } + }, + + Command::Taker { action } => match action { + TakerCommand::ListOrders { relays_path } => { + let key_path = self.key_path.unwrap_or(default_key_path()); + let relays_path = relays_path.unwrap_or(default_relays_path()); + format!( + "Taker: List Orders\n key_path: {}\n relays_path: {}", + key_path.to_string_lossy(), + relays_path.to_string_lossy() + ) + + // let key = ... + // let relays: Vec = ... + // TODO: + //processor.list_orders(key_path, relays_path).await?; + } + + TakerCommand::ReplyOrder { id } => { + let key_path = self.key_path.unwrap_or(default_key_path()); + format!("Taker: Reply Order\n id: {}\n key_path: {}", id, key_path.display()) + + // TODO + //processor.reply_order(id, key_path).await?; + } + }, + } + }; + write_into_stdout(msg)?; + Ok(()) + } +} diff --git a/nostr_options_cli/src/error.rs b/nostr_options_cli/src/error.rs new file mode 100644 index 0000000..9188719 --- /dev/null +++ b/nostr_options_cli/src/error.rs @@ -0,0 +1,7 @@ +pub type Result = core::result::Result; + +#[derive(thiserror::Error, Debug)] +pub enum CliError { + #[error("Occcurred error with io, err: {0}")] + Io(#[from] std::io::Error), +} diff --git a/nostr_options_cli/src/lib.rs b/nostr_options_cli/src/lib.rs new file mode 100644 index 0000000..d42eeff --- /dev/null +++ b/nostr_options_cli/src/lib.rs @@ -0,0 +1,3 @@ +pub mod cli_processor; +pub mod error; +mod utils; diff --git a/nostr_options_cli/src/main.rs b/nostr_options_cli/src/main.rs index 7480d6f..478e789 100644 --- a/nostr_options_cli/src/main.rs +++ b/nostr_options_cli/src/main.rs @@ -1,9 +1,14 @@ +use clap::Parser; use global_utils::logger::init_logger; use nostr::prelude::*; +use nostr_options_cli::cli_processor::Cli; #[tokio::main] async fn main() -> Result<()> { let _logger_guard = init_logger(); + let cli = Cli::parse(); + cli.process()?; + Ok(()) } diff --git a/nostr_options_cli/src/utils.rs b/nostr_options_cli/src/utils.rs new file mode 100644 index 0000000..af75e36 --- /dev/null +++ b/nostr_options_cli/src/utils.rs @@ -0,0 +1,22 @@ +use std::{io::Write, path::PathBuf}; + +const DEFAULT_RELAYS_FILEPATH: &str = ".default_relays_path.txt"; +const DEFAULT_KEY_PATH: &str = ".default_keypair_path.txt"; + +pub fn write_into_stdout + std::fmt::Debug>(text: T) -> std::io::Result { + let mut output = text.as_ref().to_string(); + output.push('\n'); + std::io::stdout().write(output.as_bytes()) +} + +pub fn default_key_path() -> PathBuf { + dirs::home_dir() + .unwrap_or_else(|| PathBuf::from(".")) + .join(DEFAULT_KEY_PATH) +} + +pub fn default_relays_path() -> PathBuf { + dirs::home_dir() + .unwrap_or_else(|| PathBuf::from(".")) + .join(DEFAULT_RELAYS_FILEPATH) +}