mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-28 13:40:43 +08:00
This adds a rad node subcommand to the CLI. The subcommand aims to fulfill the following requirements: - Checking the node status (rad node status) - Connecting to the node's control socket (rad node connect) - Printing out our routing table (rad node routing) - Printing out our tracking policies (rad node tracking) The following are yet to be implemented: - Starting the node (rad node start) - Stopping the node (rad node stop) Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett
162 lines
4.7 KiB
Rust
162 lines
4.7 KiB
Rust
use std::ffi::OsString;
|
|
|
|
use anyhow::anyhow;
|
|
use radicle::node::{Address, Node, NodeId};
|
|
|
|
use crate::terminal as term;
|
|
use crate::terminal::args::{Args, Error, Help};
|
|
|
|
#[path = "node/control.rs"]
|
|
mod control;
|
|
#[path = "node/routing.rs"]
|
|
mod routing;
|
|
#[path = "node/tracking.rs"]
|
|
mod tracking;
|
|
|
|
pub const HELP: Help = Help {
|
|
name: "node",
|
|
description: "Control and query the Radicle Node",
|
|
version: env!("CARGO_PKG_VERSION"),
|
|
usage: r#"
|
|
Usage
|
|
|
|
rad node status
|
|
rad node (start|stop)
|
|
rad node connect <nid> <addr>
|
|
rad node routing
|
|
rad node tracking [--repos|--nodes]
|
|
|
|
Options
|
|
|
|
--help Print help
|
|
--repos Show the tracked repositories table
|
|
--nodes Show the tracked nodes table
|
|
"#,
|
|
};
|
|
|
|
pub struct Options {
|
|
op: Operation,
|
|
}
|
|
|
|
pub enum Operation {
|
|
Connect { nid: NodeId, addr: Address },
|
|
Routing,
|
|
Start,
|
|
Status,
|
|
Stop,
|
|
Tracking { mode: TrackingMode },
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub enum TrackingMode {
|
|
#[default]
|
|
Repos,
|
|
Nodes,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub enum OperationName {
|
|
Connect,
|
|
Routing,
|
|
Start,
|
|
#[default]
|
|
Status,
|
|
Stop,
|
|
Tracking,
|
|
}
|
|
|
|
impl Args for Options {
|
|
fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
|
|
use lexopt::prelude::*;
|
|
|
|
let mut parser = lexopt::Parser::from_args(args);
|
|
let mut op: Option<OperationName> = None;
|
|
let mut tracking_mode = TrackingMode::default();
|
|
let mut nid: Option<NodeId> = None;
|
|
let mut addr: Option<Address> = None;
|
|
|
|
while let Some(arg) = parser.next()? {
|
|
match arg {
|
|
Long("help") => {
|
|
return Err(Error::Help.into());
|
|
}
|
|
Value(val) if op.is_none() => match val.to_string_lossy().as_ref() {
|
|
"connect" => op = Some(OperationName::Connect),
|
|
"routing" => op = Some(OperationName::Routing),
|
|
"start" => op = Some(OperationName::Start),
|
|
"status" => op = Some(OperationName::Status),
|
|
"stop" => op = Some(OperationName::Stop),
|
|
"tracking" => op = Some(OperationName::Tracking),
|
|
|
|
unknown => anyhow::bail!("unknown operation '{}'", unknown),
|
|
},
|
|
Value(val) if matches!(op, Some(OperationName::Connect)) => {
|
|
match term::args::nid(&val) {
|
|
Ok(val) => {
|
|
nid = Some(val);
|
|
}
|
|
Err(e1) => match term::args::addr(&val) {
|
|
Ok(val) => {
|
|
addr = Some(val);
|
|
}
|
|
Err(e2) => return Err(anyhow!("'{}' or '{}'", e1, e2)),
|
|
},
|
|
}
|
|
}
|
|
Long("repos") if matches!(op, Some(OperationName::Tracking)) => {
|
|
tracking_mode = TrackingMode::Repos
|
|
}
|
|
Long("nodes") if matches!(op, Some(OperationName::Tracking)) => {
|
|
tracking_mode = TrackingMode::Nodes
|
|
}
|
|
_ => return Err(anyhow!(arg.unexpected())),
|
|
}
|
|
}
|
|
|
|
let op = match op.unwrap_or_default() {
|
|
OperationName::Connect => Operation::Connect {
|
|
nid: nid.ok_or_else(|| anyhow!("a NID must be provided"))?,
|
|
addr: addr.ok_or_else(|| anyhow!("an address must be provided"))?,
|
|
},
|
|
OperationName::Routing => Operation::Routing,
|
|
OperationName::Start => Operation::Start,
|
|
OperationName::Status => Operation::Status,
|
|
OperationName::Stop => Operation::Stop,
|
|
OperationName::Tracking => Operation::Tracking {
|
|
mode: tracking_mode,
|
|
},
|
|
};
|
|
Ok((Options { op }, vec![]))
|
|
}
|
|
}
|
|
|
|
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
|
|
let profile = ctx.profile()?;
|
|
|
|
match options.op {
|
|
Operation::Connect { nid, addr } => {
|
|
let mut node = Node::new(profile.socket());
|
|
control::connect(&mut node, nid, addr)?
|
|
}
|
|
Operation::Routing => {
|
|
let node = Node::new(profile.socket());
|
|
routing::run(&node)?;
|
|
}
|
|
Operation::Start => control::start()?,
|
|
Operation::Status => {
|
|
let node = Node::new(profile.socket());
|
|
control::status(&node);
|
|
}
|
|
Operation::Stop => {
|
|
let node = Node::new(profile.socket());
|
|
control::stop(node)?;
|
|
}
|
|
Operation::Tracking { mode } => {
|
|
let node = Node::new(profile.socket());
|
|
tracking::run(&node, mode)?
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|