cli: Add `node config` command

Shows the current (loaded) node configuration.
This commit is contained in:
Alexis Sellier 2023-09-16 21:20:05 +02:00 committed by cloudhead
parent 6bb0dd1a28
commit af724eeb0d
No known key found for this signature in database
7 changed files with 50 additions and 2 deletions

View File

@ -34,6 +34,7 @@ Usage
rad node routing [--rid <rid>] [--nid <nid>] [--json] [<option>...]
rad node tracking [--repos | --nodes] [<option>...]
rad node events [--timeout <secs>] [-n <count>] [<option>...]
rad node config
For `<node-option>` see `radicle-node --help`.
@ -73,6 +74,7 @@ pub enum Operation {
addr: PeerAddr<NodeId, Address>,
timeout: time::Duration,
},
Config,
Events {
timeout: time::Duration,
count: usize,
@ -108,6 +110,7 @@ pub enum TrackingMode {
#[derive(Default, PartialEq, Eq)]
pub enum OperationName {
Connect,
Config,
Events,
Routing,
Logs,
@ -146,6 +149,7 @@ impl Args for Options {
"connect" => op = Some(OperationName::Connect),
"events" => op = Some(OperationName::Events),
"logs" => op = Some(OperationName::Logs),
"config" => op = Some(OperationName::Config),
"routing" => op = Some(OperationName::Routing),
"start" => op = Some(OperationName::Start),
"status" => op = Some(OperationName::Status),
@ -206,6 +210,7 @@ impl Args for Options {
})?,
timeout,
},
OperationName::Config => Operation::Config,
OperationName::Events => Operation::Events { timeout, count },
OperationName::Routing => Operation::Routing { rid, nid, json },
OperationName::Logs => Operation::Logs { lines },
@ -233,6 +238,7 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
Operation::Connect { addr, timeout } => {
control::connect(&mut node, addr.id, addr.addr, timeout)?
}
Operation::Config => control::config(&node)?,
Operation::Sessions => {
let sessions = control::sessions(&node)?;
if let Some(table) = sessions {

View File

@ -250,3 +250,12 @@ pub fn sessions(node: &Node) -> Result<Option<term::Table<4, term::Label>>, node
}
Ok(Some(table))
}
pub fn config(node: &Node) -> anyhow::Result<()> {
let cfg = node.config()?;
let cfg = serde_json::to_string_pretty(&cfg)?;
println!("{cfg}");
Ok(())
}

View File

@ -105,6 +105,11 @@ where
Command::Fetch { rid, nid, timeout } => {
fetch(rid, nid, timeout, writer, &mut handle)?;
}
Command::Config => {
let config = handle.config()?;
json::to_writer(writer, &config)?;
}
Command::Seeds { rid } => {
let seeds = handle.seeds(rid)?;

View File

@ -15,7 +15,7 @@ use crate::runtime::Emitter;
use crate::service;
use crate::service::tracking;
use crate::service::NodeId;
use crate::service::{CommandError, QueryState};
use crate::service::{CommandError, Config, QueryState};
use crate::service::{Event, Events};
use crate::wire;
use crate::wire::StreamId;
@ -183,6 +183,12 @@ impl radicle::node::Handle for Handle {
receiver.recv().map_err(Error::from)
}
fn config(&self) -> Result<Config, Self::Error> {
let (sender, receiver) = chan::bounded(1);
self.command(service::Command::Config(sender))?;
receiver.recv().map_err(Error::from)
}
fn fetch(
&mut self,
id: Id,

View File

@ -138,6 +138,8 @@ pub enum Command {
Connect(NodeId, Address, ConnectOptions),
/// Disconnect from node.
Disconnect(NodeId),
/// Get the node configuration.
Config(chan::Sender<Config>),
/// Lookup seeds for the given repository in the routing table.
Seeds(Id, chan::Sender<Seeds>),
/// Fetch the given repository from the network.
@ -162,6 +164,7 @@ impl fmt::Debug for Command {
Self::SyncInventory(_) => write!(f, "SyncInventory(..)"),
Self::Connect(id, addr, opts) => write!(f, "Connect({id}, {addr}, {opts:?})"),
Self::Disconnect(id) => write!(f, "Disconnect({id})"),
Self::Config(_) => write!(f, "Config"),
Self::Seeds(id, _) => write!(f, "Seeds({id})"),
Self::Fetch(id, node, _, _) => write!(f, "Fetch({id}, {node})"),
Self::TrackRepo(id, scope, _) => write!(f, "TrackRepo({id}, {scope})"),
@ -518,6 +521,9 @@ where
Command::Disconnect(nid) => {
self.outbox.disconnect(nid, DisconnectReason::Command);
}
Command::Config(resp) => {
resp.send(self.config.clone()).ok();
}
Command::Seeds(rid, resp) => match self.seeds(&rid) {
Ok(seeds) => {
let (connected, disconnected) = seeds.partition();

View File

@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};
use std::{io, time};
use crate::identity::Id;
use crate::node::{Alias, ConnectOptions, ConnectResult, Event, FetchResult, Seeds};
use crate::node::{Alias, Config, ConnectOptions, ConnectResult, Event, FetchResult, Seeds};
use crate::runtime::HandleError;
use crate::service::tracking;
use crate::service::NodeId;
@ -28,6 +28,10 @@ impl radicle::node::Handle for Handle {
true
}
fn config(&self) -> Result<Config, Self::Error> {
Ok(Config::new(Alias::new("acme")))
}
fn connect(
&mut self,
_node: NodeId,

View File

@ -339,6 +339,9 @@ pub enum Command {
/// Sync local inventory with node.
SyncInventory,
/// Get the current node condiguration.
Config,
/// Connect to node with the given address.
#[serde(rename_all = "camelCase")]
Connect {
@ -653,6 +656,8 @@ pub trait Handle: Clone + Sync + Send {
fn nid(&self) -> Result<NodeId, Self::Error>;
/// Check if the node is running. to a peer.
fn is_running(&self) -> bool;
/// Get the current node configuration.
fn config(&self) -> Result<config::Config, Self::Error>;
/// Connect to a peer.
fn connect(
&mut self,
@ -810,6 +815,13 @@ impl Handle for Node {
matches!(result, CommandResult::Okay { .. })
}
fn config(&self) -> Result<config::Config, Error> {
self.call::<config::Config>(Command::Config, DEFAULT_TIMEOUT)?
.next()
.ok_or(Error::EmptyResponse)?
.map_err(Error::from)
}
fn connect(
&mut self,
nid: NodeId,