caretta-sync/lazy-supplements-desktop/src/cli/node.rs

81 lines
1.7 KiB
Rust
Raw Normal View History

2025-06-07 09:28:18 +09:00
use std::{net::IpAddr, ops::Mul, path::PathBuf, str::FromStr};
2025-05-30 09:26:47 +09:00
2025-06-06 06:50:28 +09:00
use clap::{Args, Parser, Subcommand};
2025-06-01 15:18:17 +09:00
use libp2p::{
2025-06-07 09:28:18 +09:00
multiaddr::Protocol, noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr, PeerId
2025-06-01 15:18:17 +09:00
};
use crate::{cli::ServerArgs, error::{CoreError, DesktopError, Error}};
2025-06-02 12:02:04 +09:00
use super::ConfigArgs;
2025-05-30 09:26:47 +09:00
2025-06-06 06:50:28 +09:00
#[derive(Debug, Args)]
2025-05-30 09:26:47 +09:00
pub struct NodeArgs {
#[command(subcommand)]
2025-06-01 16:29:35 +09:00
pub command: NodeCommand
2025-05-30 09:26:47 +09:00
}
2025-06-06 06:50:28 +09:00
#[derive(Debug, Parser)]
pub struct ConsoleNodeArgs {
#[command(flatten)]
pub args: NodeArgs,
}
2025-06-06 08:22:36 +09:00
impl NodeArgs {
2025-06-06 07:49:36 +09:00
pub async fn run(self) -> Result<(), Error> {
2025-06-06 06:50:28 +09:00
println!("{self:?}");
Ok(())
}
}
2025-05-30 09:26:47 +09:00
#[derive(Args, Debug)]
2025-06-07 09:28:18 +09:00
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<Self, Self::Err> {
if let Ok(x) = s.parse::<Multiaddr>() {
Ok(Self::Addr(x))
} else if let Ok(x) = s.parse::<PeerId>() {
Ok(Self::Id(x))
} else if let Ok(x) = s.parse::<u32>() {
Ok(Self::Number(x))
} else {
Err(format!("Invalid value: {s}").to_string())
}
}
}
#[derive(Args, Debug)]
pub struct NodeJoinArgs {
2025-06-02 12:02:04 +09:00
#[command(flatten)]
2025-06-07 09:28:18 +09:00
pub peer: PeerArgs,
pub pass: Option<String>,
2025-05-30 09:26:47 +09:00
}
#[derive(Debug, Subcommand)]
pub enum NodeCommand {
2025-06-07 09:28:18 +09:00
Add(PeerArgs),
Ping(PeerArgs),
Join(PeerArgs),
List,
Delete(PeerArgs),
2025-05-30 09:26:47 +09:00
}
2025-06-07 09:28:18 +09:00
impl PeerArgs {
pub async fn run(self) -> Result<(), Error> {
println!("{self:?}");
todo!()
2025-06-01 15:18:17 +09:00
}
}