Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 5 additions & 0 deletions nostr_options_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

124 changes: 124 additions & 0 deletions nostr_options_cli/src/cli_processor.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>,
#[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<PathBuf>,
},

GetOrderReply {
#[arg(short = 'i', long)]
id: String,

#[arg(short = 'r', long)]
relays_path: Option<PathBuf>,
},
}

#[derive(Debug, Subcommand)]
enum TakerCommand {
ListOrders {
#[arg(short = 'r', long)]
relays_path: Option<PathBuf>,
},

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<RelayUrly> = ...
// 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(())
}
}
7 changes: 7 additions & 0 deletions nostr_options_cli/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub type Result<T> = core::result::Result<T, CliError>;

#[derive(thiserror::Error, Debug)]
pub enum CliError {
#[error("Occcurred error with io, err: {0}")]
Io(#[from] std::io::Error),
}
3 changes: 3 additions & 0 deletions nostr_options_cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod cli_processor;
pub mod error;
mod utils;
5 changes: 5 additions & 0 deletions nostr_options_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
22 changes: 22 additions & 0 deletions nostr_options_cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<T: AsRef<str> + std::fmt::Debug>(text: T) -> std::io::Result<usize> {
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)
}