Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add `Profile::connect`
Alexis Sellier committed 3 years ago
commit ca2f766d19b54ffa76c8ad0fe97750b43642bf97
parent 854b0ba3530aa58124ad52166bd1bc29277b980e
5 files changed +42 -8
modified 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}")]
modified 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;
modified 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));
modified 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<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
        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?;
modified 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<node::Connection>,
}

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.