Skip to content

Commit 62acbbe

Browse files
zahorodnyiikripaka
authored andcommitted
Introduce CLI for app
1 parent 241d69d commit 62acbbe

File tree

7 files changed

+165
-103
lines changed

7 files changed

+165
-103
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ anyhow = { version = "1.0.100" }
1818
clap = { version = "4.5.49", features = ["derive"] }
1919
config = { version = "0.15.16", default-features = true }
2020
dotenvy = { version = "0.15" }
21+
dirs = {version = "6.0.0"}
2122
global_utils = { path = "./crates/global_utils" }
2223
hex = { version = "0.4.3" }
2324
itertools = { version = "0.14.0" }

nostr_options_cli/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ tokio-tungstenite = { workspace = true }
1010
futures-util = { workspace = true }
1111
serde_json = { workspace = true }
1212
tokio = { workspace = true }
13-
clap = {version = "4.5", features = ["derive"]}
14-
dirs = "6.0"
13+
clap = { workspace = true }
14+
dirs = { workspace = true }
15+
tracing = { workspace = true }
16+
thiserror = { workspace = true }
17+
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use clap::{Parser, Subcommand};
2+
use std::path::PathBuf;
3+
use tracing::instrument;
4+
5+
use crate::utils::{default_key_path, default_relays_path, write_into_stdout};
6+
7+
#[derive(Parser)]
8+
pub struct Cli {
9+
#[arg(short = 'k', long)]
10+
key_path: Option<PathBuf>,
11+
#[command(subcommand)]
12+
command: Command,
13+
}
14+
15+
#[derive(Debug, Subcommand)]
16+
enum Command {
17+
Maker {
18+
#[command(subcommand)]
19+
action: MakerCommand,
20+
},
21+
22+
Taker {
23+
#[command(subcommand)]
24+
action: TakerCommand,
25+
},
26+
}
27+
28+
#[derive(Debug, Subcommand)]
29+
enum MakerCommand {
30+
CreateOrder {
31+
#[arg(short = 'm', long)]
32+
message: String,
33+
34+
#[arg(short = 'r', long)]
35+
relays_path: Option<PathBuf>,
36+
},
37+
38+
GetOrderReply {
39+
#[arg(short = 'i', long)]
40+
id: String,
41+
42+
#[arg(short = 'r', long)]
43+
relays_path: Option<PathBuf>,
44+
},
45+
}
46+
47+
#[derive(Debug, Subcommand)]
48+
enum TakerCommand {
49+
ListOrders {
50+
#[arg(short = 'r', long)]
51+
relays_path: Option<PathBuf>,
52+
},
53+
54+
ReplyOrder {
55+
#[arg(short = 'i', long)]
56+
id: String,
57+
},
58+
}
59+
60+
impl Cli {
61+
#[instrument(skip(self))]
62+
pub fn process(self) -> crate::error::Result<()> {
63+
let msg = {
64+
match self.command {
65+
Command::Maker { action } => match action {
66+
MakerCommand::CreateOrder { message, relays_path } => {
67+
let key_path = self.key_path.unwrap_or(default_key_path());
68+
let relays_path = relays_path.unwrap_or(default_relays_path());
69+
format!(
70+
"Maker: Create Order\n message: {}\n key_path: {}\n relays_path: {}",
71+
message,
72+
key_path.display(),
73+
relays_path.display()
74+
)
75+
76+
// TODO:
77+
//processor.create_order(message, key_path, relays_path).await?;
78+
}
79+
80+
MakerCommand::GetOrderReply { id, relays_path } => {
81+
let key_path = self.key_path.unwrap_or(default_key_path());
82+
let relays_path = relays_path.unwrap_or(default_relays_path());
83+
format!(
84+
"Maker: Get Order Reply\n id: {}\n key_path: {}\n relays_path: {}",
85+
id,
86+
key_path.display(),
87+
relays_path.display()
88+
)
89+
90+
// TODO:
91+
//processor.get_order_reply(id, key_path, relays_path).await?;
92+
}
93+
},
94+
95+
Command::Taker { action } => match action {
96+
TakerCommand::ListOrders { relays_path } => {
97+
let key_path = self.key_path.unwrap_or(default_key_path());
98+
let relays_path = relays_path.unwrap_or(default_relays_path());
99+
format!(
100+
"Taker: List Orders\n key_path: {}\n relays_path: {}",
101+
key_path.to_string_lossy(),
102+
relays_path.to_string_lossy()
103+
)
104+
105+
// let key = ...
106+
// let relays: Vec<RelayUrly> = ...
107+
// TODO:
108+
//processor.list_orders(key_path, relays_path).await?;
109+
}
110+
111+
TakerCommand::ReplyOrder { id } => {
112+
let key_path = self.key_path.unwrap_or(default_key_path());
113+
format!("Taker: Reply Order\n id: {}\n key_path: {}", id, key_path.display())
114+
115+
// TODO
116+
//processor.reply_order(id, key_path).await?;
117+
}
118+
},
119+
}
120+
};
121+
write_into_stdout(msg)?;
122+
Ok(())
123+
}
124+
}

nostr_options_cli/src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub type Result<T> = core::result::Result<T, CliError>;
2+
3+
#[derive(thiserror::Error, Debug)]
4+
pub enum CliError {
5+
#[error("Occcurred error with io, err: {0}")]
6+
Io(#[from] std::io::Error),
7+
}

nostr_options_cli/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod cli_processor;
2+
pub mod error;
3+
mod utils;

nostr_options_cli/src/main.rs

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,14 @@
1+
use clap::Parser;
12
use global_utils::logger::init_logger;
23
use nostr::prelude::*;
3-
use clap::{Parser, Subcommand};
4-
use std::path::PathBuf;
5-
6-
7-
#[derive(Parser)]
8-
struct Cli {
9-
#[command(subcommand)]
10-
command: Command
11-
}
12-
13-
#[derive(Debug, Subcommand)]
14-
enum Command {
15-
/// Maker
16-
CreateOrder {
17-
#[arg(short = 'm', long)]
18-
message: String,
19-
20-
#[arg(short = 'k', long)]
21-
key_path: Option<PathBuf>,
22-
23-
#[arg(short = 'r', long)]
24-
relays_path: Option<PathBuf>,
25-
},
26-
27-
GetOrderReply {
28-
#[arg(short = 'i', long)]
29-
id: String,
30-
31-
#[arg(short = 'k', long)]
32-
key_path: Option<PathBuf>,
33-
34-
#[arg(short = 'r', long)]
35-
relays_path: Option<PathBuf>,
36-
},
37-
38-
/// Taker
39-
ListOrders {
40-
#[arg(short = 'k', long)]
41-
key_path: Option<PathBuf>,
42-
43-
#[arg(short = 'r', long)]
44-
relays_path: Option<PathBuf>,
45-
},
46-
47-
ReplyOrder {
48-
#[arg(short = 'i', long)]
49-
id: String,
50-
51-
#[arg(short = 'k', long)]
52-
key_path: Option<PathBuf>,
53-
},
54-
}
55-
4+
use nostr_options_cli::cli_processor::Cli;
565

576
#[tokio::main]
587
async fn main() -> Result<()> {
598
let _logger_guard = init_logger();
609

6110
let cli = Cli::parse();
62-
63-
match cli.command {
64-
Command::CreateOrder { message, key_path, relays_path } => {
65-
let key_path = key_path.unwrap_or(default_key_path());
66-
let relays_path = relays_path.unwrap_or(default_relays_path());
67-
println!("🛠 Create order:");
68-
println!(" message: {}", message);
69-
println!(" key_path: {}", key_path.display());
70-
println!(" relays_path: {}", relays_path.display());
71-
}
72-
73-
Command::GetOrderReply { id, key_path, relays_path } => {
74-
let key_path = key_path.unwrap_or(default_key_path());
75-
let relays_path = relays_path.unwrap_or(default_relays_path());
76-
println!("📦 Get order reply:");
77-
println!(" id: {}", id);
78-
println!(" key_path: {}", key_path.display());
79-
println!(" relays_path: {}", relays_path.display());
80-
}
81-
82-
Command::ListOrders { key_path, relays_path } => {
83-
let key_path = key_path.unwrap_or(default_key_path());
84-
let relays_path = relays_path.unwrap_or(default_relays_path());
85-
println!("📋 List orders:");
86-
println!(" key_path: {}", key_path.display());
87-
println!(" relays_path: {}", relays_path.display());
88-
}
89-
90-
Command::ReplyOrder { id, key_path } => {
91-
let key_path = key_path.unwrap_or(default_key_path());
92-
println!("💬 Reply order:");
93-
println!(" id: {}", id);
94-
println!(" key_path: {}", key_path.display());
95-
}
96-
}
11+
cli.process()?;
9712

9813
Ok(())
9914
}
100-
101-
102-
fn default_key_path() -> PathBuf {
103-
dirs::home_dir()
104-
.unwrap_or_else(|| PathBuf::from("."))
105-
.join(".nostr/keypair.txt")
106-
}
107-
108-
fn default_relays_path() -> PathBuf {
109-
dirs::home_dir()
110-
.unwrap_or_else(|| PathBuf::from("."))
111-
.join(".nostr/relays.txt")
112-
}

nostr_options_cli/src/utils.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::{io::Write, path::PathBuf};
2+
3+
const DEFAULT_RELAYS_FILEPATH: &str = ".default_relays_path.txt";
4+
const DEFAULT_KEY_PATH: &str = ".default_keypair_path.txt";
5+
6+
pub fn write_into_stdout<T: AsRef<str> + std::fmt::Debug>(text: T) -> std::io::Result<usize> {
7+
let mut output = text.as_ref().to_string();
8+
output.push('\n');
9+
std::io::stdout().write(output.as_bytes())
10+
}
11+
12+
pub fn default_key_path() -> PathBuf {
13+
dirs::home_dir()
14+
.unwrap_or_else(|| PathBuf::from("."))
15+
.join(DEFAULT_KEY_PATH)
16+
}
17+
18+
pub fn default_relays_path() -> PathBuf {
19+
dirs::home_dir()
20+
.unwrap_or_else(|| PathBuf::from("."))
21+
.join(DEFAULT_RELAYS_FILEPATH)
22+
}

0 commit comments

Comments
 (0)