Add `Profile::connect`

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-09-26 14:27:56 +02:00
parent 854b0ba353
commit ca2f766d19
No known key found for this signature in database
5 changed files with 42 additions and 8 deletions

View File

@ -13,9 +13,6 @@ use crate::identity::Id;
use crate::service::FetchLookup; use crate::service::FetchLookup;
use crate::service::FetchResult; use crate::service::FetchResult;
/// Default name for control socket file.
pub const DEFAULT_SOCKET_NAME: &str = "radicle.sock";
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
#[error("failed to bind control socket listener: {0}")] #[error("failed to bind control socket listener: {0}")]

View File

@ -13,7 +13,7 @@ pub mod transport;
pub mod wire; pub mod wire;
pub use nakamoto_net::{Io, Link, LocalDuration, LocalTime}; 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 mod prelude {
pub use crate::clock::Timestamp; pub use crate::clock::Timestamp;

View File

@ -1,6 +1,7 @@
use std::thread; use std::thread;
use std::{env, net, process}; use std::{env, net, process};
use radicle_node::node;
use radicle_node::prelude::Address; use radicle_node::prelude::Address;
use radicle_node::{client, control, git, service}; use radicle_node::{client, control, git, service};
@ -64,7 +65,7 @@ fn main() -> anyhow::Result<()> {
}, },
listen: options.listen, 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 t1 = thread::spawn(move || control::listen(socket, handle));
let t2 = thread::spawn(move || client.run(config)); let t2 = thread::spawn(move || client.run(config));

View File

@ -6,6 +6,9 @@ use std::path::Path;
use crate::identity::Id; use crate::identity::Id;
/// Default name for control socket file.
pub const DEFAULT_SOCKET_NAME: &str = "radicle.sock";
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
#[error("i/o error: {0}")] #[error("i/o error: {0}")]
@ -27,11 +30,12 @@ pub trait Handle {
} }
/// Node control socket. /// Node control socket.
pub struct Socket { #[derive(Debug)]
pub struct Connection {
stream: UnixStream, stream: UnixStream,
} }
impl Socket { impl Connection {
pub fn connect<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> { pub fn connect<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
let stream = UnixStream::connect(path)?; 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> { fn fetch(&self, id: &Id) -> Result<(), Error> {
for line in self.call("fetch", id)? { for line in self.call("fetch", id)? {
let line = line?; let line = line?;

View File

@ -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::path::PathBuf;
use std::{env, io}; use std::{env, io};
use crate::crypto::{KeyPair, PublicKey, SecretKey, Signature, Signer}; use crate::crypto::{KeyPair, PublicKey, SecretKey, Signature, Signer};
use crate::keystore::UnsafeKeystore; use crate::keystore::UnsafeKeystore;
use crate::node;
use crate::storage::git::Storage; use crate::storage::git::Storage;
#[derive(Debug)] #[derive(Debug)]
@ -26,6 +40,7 @@ pub struct Profile {
pub home: PathBuf, pub home: PathBuf,
pub signer: UnsafeSigner, pub signer: UnsafeSigner,
pub storage: Storage, pub storage: Storage,
pub node: Option<node::Connection>,
} }
impl Profile { impl Profile {
@ -45,6 +60,7 @@ impl Profile {
home, home,
signer, signer,
storage, storage,
node: None,
}) })
} }
@ -58,12 +74,28 @@ impl Profile {
home, home,
signer, signer,
storage, 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 { pub fn id(&self) -> &PublicKey {
self.signer.public_key() 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. /// Get the path to the radicle home folder.