From 72d3a1f54bb0633cae1304a9c33acd63b60c79ae Mon Sep 17 00:00:00 2001 From: fluo10 Date: Sat, 7 Jun 2025 09:28:18 +0900 Subject: [PATCH] Add PeerArg --- lazy-supplements/src/cli/mod.rs | 2 +- lazy-supplements/src/cli/node.rs | 78 +++++++++++++++++--------------- lazy-supplements/src/main.rs | 13 +++--- 3 files changed, 50 insertions(+), 43 deletions(-) diff --git a/lazy-supplements/src/cli/mod.rs b/lazy-supplements/src/cli/mod.rs index d4f0846..bdb9307 100644 --- a/lazy-supplements/src/cli/mod.rs +++ b/lazy-supplements/src/cli/mod.rs @@ -9,5 +9,5 @@ mod server; pub use config::ConfigArgs; pub use console::{ConsoleArgs, ConsoleCommands}; pub use init::InitArgs; -pub use node::{ NodeArgs, NodeCommand, JoinNodeArgs , ConsoleNodeArgs}; +pub use node::{ NodeArgs, NodeCommand, PeerArgs , ConsoleNodeArgs}; pub use server::ServerArgs; \ No newline at end of file diff --git a/lazy-supplements/src/cli/node.rs b/lazy-supplements/src/cli/node.rs index a71f175..00dcab4 100644 --- a/lazy-supplements/src/cli/node.rs +++ b/lazy-supplements/src/cli/node.rs @@ -1,9 +1,9 @@ -use std::{net::IpAddr, path::PathBuf}; +use std::{net::IpAddr, ops::Mul, path::PathBuf, str::FromStr}; use clap::{Args, Parser, Subcommand}; use futures::StreamExt; use libp2p::{ - multiaddr::Protocol, noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr + multiaddr::Protocol, noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr, PeerId }; use tracing_subscriber::EnvFilter; @@ -35,47 +35,53 @@ pub async fn parse_and_run_console_node_command(s:Vec) -> Result<(), Err } #[derive(Args, Debug)] -pub struct JoinNodeArgs { - #[arg(long)] - pub peer_ip: IpAddr, - #[arg(long)] - pub peer_port: u16, - //#[arg(long)] - //pub peer_id: String, +pub struct PeerArgs { + #[arg(value_parser = clap::value_parser!(PeerArg))] + pub peer: PeerArg, +} +#[derive(Clone, Debug)] +pub enum PeerArg { + Addr(Multiaddr), + Id(PeerId), + Number(u32), +} + +impl FromStr for PeerArg { + type Err = String; + fn from_str(s: &str) -> Result { + if let Ok(x) = s.parse::() { + Ok(Self::Addr(x)) + } else if let Ok(x) = s.parse::() { + Ok(Self::Id(x)) + } else if let Ok(x) = s.parse::() { + Ok(Self::Number(x)) + } else { + Err(format!("Invalid value: {s}").to_string()) + } + } +} + + +#[derive(Args, Debug)] +pub struct NodeJoinArgs { #[command(flatten)] - pub config: ConfigArgs, + pub peer: PeerArgs, + pub pass: Option, } #[derive(Debug, Subcommand)] pub enum NodeCommand { - Ping(JoinNodeArgs), - Join(JoinNodeArgs), + Add(PeerArgs), + Ping(PeerArgs), + Join(PeerArgs), + List, + Delete(PeerArgs), } -impl JoinNodeArgs { - pub async fn ping(self) -> Result<(), Error> { - let mut swarm = self.config.try_into_node_config().await?.try_into_swarm().await?; - - let mut remote: Multiaddr = Multiaddr::empty(); - remote.push(match self.peer_ip { - IpAddr::V4(x) => Protocol::Ip4(x), - IpAddr::V6(x) => Protocol::Ip6(x), - }); - remote.push(Protocol::Tcp(self.peer_port)); - let addr = remote.to_string(); - swarm.dial(remote)?; - println!("Dialed {addr}"); - - loop{ - match swarm.select_next_some().await { - SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"), - SwarmEvent::Behaviour(event) => { - println!("{event:?}"); - event.run().await; - }, - _ => {} - } - } +impl PeerArgs { + pub async fn run(self) -> Result<(), Error> { + println!("{self:?}"); + todo!() } } \ No newline at end of file diff --git a/lazy-supplements/src/main.rs b/lazy-supplements/src/main.rs index cdce186..2e37ec1 100644 --- a/lazy-supplements/src/main.rs +++ b/lazy-supplements/src/main.rs @@ -1,10 +1,12 @@ use clap::{Parser, Subcommand}; -use lazy_supplements::{cli::{ConsoleArgs, ConsoleCommands, InitArgs, NodeArgs, NodeCommand, ServerArgs}, *}; +use lazy_supplements::{cli::{ConfigArgs, ConsoleArgs, ConsoleCommands, InitArgs, NodeArgs, NodeCommand, ServerArgs}, global::{Global, GLOBAL}, *}; #[derive(Debug, Parser)] struct Cli { #[command(subcommand)] command: Command, + #[command(flatten)] + pub config: ConfigArgs, } #[derive(Debug, Subcommand)] @@ -18,11 +20,10 @@ enum Command { #[tokio::main] async fn main() { - match Cli::parse().command { - Command::Node(x) => match x.command { - NodeCommand::Ping(y) => y.ping().await.unwrap(), - NodeCommand::Join(y) => println!("{y:?}"), - }, + let cli = Cli::parse(); + let _ = GLOBAL.get_or_init_node_config(cli.config.try_into_node_config().await.unwrap()).await; + match cli.command { + Command::Node(x) => x.run().await.unwrap(), Command::Init(x) => x.init_config().await, Command::Server(x) => x.start_server().await.unwrap(), Command::Console(x) => x.start_console(ConsoleCommands::default()).await.unwrap(),