From ca2f766d19b54ffa76c8ad0fe97750b43642bf97 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 26 Sep 2022 14:27:56 +0200 Subject: [PATCH] Add `Profile::connect` Signed-off-by: Alexis Sellier --- radicle-node/src/control.rs | 3 --- radicle-node/src/lib.rs | 2 +- radicle-node/src/main.rs | 3 ++- radicle/src/node.rs | 10 +++++++--- radicle/src/profile.rs | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/radicle-node/src/control.rs b/radicle-node/src/control.rs index eb58d395..f687de52 100644 --- a/radicle-node/src/control.rs +++ b/radicle-node/src/control.rs @@ -13,9 +13,6 @@ use crate::identity::Id; use crate::service::FetchLookup; use crate::service::FetchResult; -/// Default name for control socket file. -pub const DEFAULT_SOCKET_NAME: &str = "radicle.sock"; - #[derive(thiserror::Error, Debug)] pub enum Error { #[error("failed to bind control socket listener: {0}")] diff --git a/radicle-node/src/lib.rs b/radicle-node/src/lib.rs index 21045cca..bfbb9298 100644 --- a/radicle-node/src/lib.rs +++ b/radicle-node/src/lib.rs @@ -13,7 +13,7 @@ pub mod transport; pub mod wire; pub use nakamoto_net::{Io, Link, LocalDuration, LocalTime}; -pub use radicle::{collections, crypto, git, hash, identity, profile, rad, storage}; +pub use radicle::{collections, crypto, git, hash, identity, node, profile, rad, storage}; pub mod prelude { pub use crate::clock::Timestamp; diff --git a/radicle-node/src/main.rs b/radicle-node/src/main.rs index 9e182f1e..e1bac11d 100644 --- a/radicle-node/src/main.rs +++ b/radicle-node/src/main.rs @@ -1,6 +1,7 @@ use std::thread; use std::{env, net, process}; +use radicle_node::node; use radicle_node::prelude::Address; use radicle_node::{client, control, git, service}; @@ -64,7 +65,7 @@ fn main() -> anyhow::Result<()> { }, listen: options.listen, }; - let socket = env::var("RAD_SOCKET").unwrap_or_else(|_| control::DEFAULT_SOCKET_NAME.to_owned()); + let socket = env::var("RAD_SOCKET").unwrap_or_else(|_| node::DEFAULT_SOCKET_NAME.to_owned()); let t1 = thread::spawn(move || control::listen(socket, handle)); let t2 = thread::spawn(move || client.run(config)); diff --git a/radicle/src/node.rs b/radicle/src/node.rs index 3f7736f5..7bdfc47e 100644 --- a/radicle/src/node.rs +++ b/radicle/src/node.rs @@ -6,6 +6,9 @@ use std::path::Path; use crate::identity::Id; +/// Default name for control socket file. +pub const DEFAULT_SOCKET_NAME: &str = "radicle.sock"; + #[derive(thiserror::Error, Debug)] pub enum Error { #[error("i/o error: {0}")] @@ -27,11 +30,12 @@ pub trait Handle { } /// Node control socket. -pub struct Socket { +#[derive(Debug)] +pub struct Connection { stream: UnixStream, } -impl Socket { +impl Connection { pub fn connect>(path: P) -> Result { let stream = UnixStream::connect(path)?; @@ -49,7 +53,7 @@ impl Socket { } } -impl Handle for Socket { +impl Handle for Connection { fn fetch(&self, id: &Id) -> Result<(), Error> { for line in self.call("fetch", id)? { let line = line?; diff --git a/radicle/src/profile.rs b/radicle/src/profile.rs index 94fa30d7..0abc60e9 100644 --- a/radicle/src/profile.rs +++ b/radicle/src/profile.rs @@ -1,8 +1,22 @@ +//! Radicle node profile. +//! +//! $RAD_HOME/ # Radicle home +//! storage/ # Storage root +//! zEQNunJUqkNahQ8VvQYuWZZV7EJB/ # Project root +//! git/ # Project Git repository +//! ... # More projects... +//! keys/ +//! radicle # Secret key (PKCS 8) +//! radicle.pub # Public key (PKCS 8) +//! node/ +//! radicle.sock # Node control socket +//! use std::path::PathBuf; use std::{env, io}; use crate::crypto::{KeyPair, PublicKey, SecretKey, Signature, Signer}; use crate::keystore::UnsafeKeystore; +use crate::node; use crate::storage::git::Storage; #[derive(Debug)] @@ -26,6 +40,7 @@ pub struct Profile { pub home: PathBuf, pub signer: UnsafeSigner, pub storage: Storage, + pub node: Option, } impl Profile { @@ -45,6 +60,7 @@ impl Profile { home, signer, storage, + node: None, }) } @@ -58,12 +74,28 @@ impl Profile { home, signer, storage, + node: None, }) } + /// Connect to the local radicle node. + pub fn connect(&mut self) -> Result<(), io::Error> { + let conn = node::Connection::connect(self.socket())?; + self.node = Some(conn); + + Ok(()) + } + pub fn id(&self) -> &PublicKey { self.signer.public_key() } + + /// Get the path to the radicle node socket. + fn socket(&self) -> PathBuf { + env::var_os("RAD_SOCKET") + .map(PathBuf::from) + .unwrap_or_else(|| self.home.join("node").join(node::DEFAULT_SOCKET_NAME)) + } } /// Get the path to the radicle home folder.