diff --git a/radicle-cli/src/commands/path.rs b/radicle-cli/src/commands/path.rs index 121db814..1035c3d3 100644 --- a/radicle-cli/src/commands/path.rs +++ b/radicle-cli/src/commands/path.rs @@ -50,7 +50,7 @@ impl Args for Options { pub fn run(_options: Options, _ctx: impl term::Context) -> anyhow::Result<()> { let home = profile::home()?; - println!("{}", home.display()); + println!("{}", home.path().display()); Ok(()) } diff --git a/radicle-cli/tests/commands.rs b/radicle-cli/tests/commands.rs index 4826637a..ff562771 100644 --- a/radicle-cli/tests/commands.rs +++ b/radicle-cli/tests/commands.rs @@ -1,7 +1,7 @@ use std::env; use std::path::Path; -use radicle::profile::Profile; +use radicle::profile::{Home, Profile}; use radicle::test::fixtures; mod framework; @@ -45,7 +45,7 @@ fn profile(home: &Path) -> Profile { // Set debug mode, to make test output more predictable. env::set_var("RAD_DEBUG", "1"); // Setup a new user. - Profile::init(home, "radicle".to_owned()).unwrap() + Profile::init(Home::new(home.to_path_buf()), "radicle".to_owned()).unwrap() } #[test] diff --git a/radicle-httpd/src/api/test.rs b/radicle-httpd/src/api/test.rs index d04e8911..7b4abb54 100644 --- a/radicle-httpd/src/api/test.rs +++ b/radicle-httpd/src/api/test.rs @@ -73,7 +73,7 @@ pub fn seed(dir: &Path) -> Context { .unwrap(); // eq. rad auth - let profile = radicle::Profile::init(rad_home, PASSWORD.to_owned()).unwrap(); + let profile = radicle::Profile::init(rad_home.into(), PASSWORD.to_owned()).unwrap(); // rad init rad_init::init( diff --git a/radicle-node/src/client.rs b/radicle-node/src/client.rs index 719c5421..b655c904 100644 --- a/radicle-node/src/client.rs +++ b/radicle-node/src/client.rs @@ -2,7 +2,7 @@ use std::{io, net, thread, time}; use cyphernet::{Cert, EcSign}; use netservices::resource::NetAccept; -use radicle::profile::Paths; +use radicle::profile::Home; use radicle::Storage; use reactor::poller::popol; use reactor::Reactor; @@ -67,19 +67,19 @@ impl + Clone + 'static> /// /// This function spawns threads. pub fn with( - paths: Paths, + home: Home, config: service::Config, listen: Vec, proxy: net::SocketAddr, signer: G, ) -> Result, Error> { let id = *signer.public_key(); - let node_sock = paths.socket(); - let node_dir = paths.node(); + let node_sock = home.socket(); + let node_dir = home.node(); let network = config.network; let rng = fastrand::Rng::new(); let clock = LocalTime::now(); - let storage = Storage::open(paths.storage())?; + let storage = Storage::open(home.storage())?; let address_db = node_dir.join(ADDRESS_DB_FILE); let routing_db = node_dir.join(ROUTING_DB_FILE); let tracking_db = node_dir.join(TRACKING_DB_FILE); diff --git a/radicle-node/src/main.rs b/radicle-node/src/main.rs index 56211e7b..79f07c97 100644 --- a/radicle-node/src/main.rs +++ b/radicle-node/src/main.rs @@ -8,7 +8,6 @@ use radicle::profile; use radicle_node::client::Runtime; use radicle_node::crypto::ssh::keystore::{Keystore, MemorySigner}; use radicle_node::prelude::{Address, NodeId}; -use radicle_node::profile::Paths; use radicle_node::{logger, service}; #[derive(Debug)] @@ -79,11 +78,10 @@ fn main() -> anyhow::Result<()> { let options = Options::from_env()?; let home = profile::home()?; - let paths = Paths::new(home); let passphrase = env::var(profile::env::RAD_PASSPHRASE) .context("`RAD_PASSPHRASE` is required to be set for the node to establish connections")? .into(); - let keystore = Keystore::new(&paths.keys()); + let keystore = Keystore::new(&home.keys()); let signer = MemorySigner::load(&keystore, passphrase)?; let config = service::Config { connect: options.connect.into_iter().collect(), @@ -92,7 +90,7 @@ fn main() -> anyhow::Result<()> { ..service::Config::default() }; let proxy = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 9050); - let runtime = Runtime::with(paths, config, options.listen, proxy, signer)?; + let runtime = Runtime::with(home, config, options.listen, proxy, signer)?; runtime.run()?; diff --git a/radicle-node/src/tests/e2e.rs b/radicle-node/src/tests/e2e.rs index d477b67c..04399419 100644 --- a/radicle-node/src/tests/e2e.rs +++ b/radicle-node/src/tests/e2e.rs @@ -9,7 +9,7 @@ use radicle::crypto::test::signer::MockSigner; use radicle::git::refname; use radicle::identity::Id; use radicle::node::Handle; -use radicle::profile::Paths; +use radicle::profile::Home; use radicle::storage::WriteStorage; use radicle::test::fixtures; use radicle::Storage; @@ -42,7 +42,7 @@ impl Node { .take(8) .collect::(), ); - let paths = Paths::init(home).unwrap(); + let paths = Home::init(home).unwrap(); let signer = MockSigner::default(); let listen = vec![([0, 0, 0, 0], 0).into()]; let proxy = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 9050); diff --git a/radicle/src/profile.rs b/radicle/src/profile.rs index e6a80532..4ffe8cca 100644 --- a/radicle/src/profile.rs +++ b/radicle/src/profile.rs @@ -59,24 +59,22 @@ pub enum Error { #[derive(Debug, Clone)] pub struct Profile { - pub paths: Paths, + pub home: Home, pub storage: Storage, pub keystore: Keystore, pub public_key: PublicKey, } impl Profile { - pub fn init(home: impl AsRef, passphrase: impl Into) -> Result { - let home = home.as_ref().to_path_buf(); - let paths = Paths::init(home.as_path())?; - let storage = Storage::open(paths.storage())?; - let keystore = Keystore::new(&paths.keys()); + pub fn init(home: Home, passphrase: impl Into) -> Result { + let storage = Storage::open(home.storage())?; + let keystore = Keystore::new(&home.keys()); let public_key = keystore.init("radicle", passphrase)?; transport::local::register(storage.clone()); Ok(Profile { - paths, + home, storage, keystore, public_key, @@ -85,17 +83,16 @@ impl Profile { pub fn load() -> Result { let home = self::home()?; - let paths = Paths::new(home); - let storage = Storage::open(paths.storage())?; - let keystore = Keystore::new(&paths.keys()); + let storage = Storage::open(home.storage())?; + let keystore = Keystore::new(&home.keys()); let public_key = keystore .public_key()? - .ok_or_else(|| Error::NotFound(paths.home.clone()))?; + .ok_or_else(|| Error::NotFound(home.path().to_path_buf()))?; transport::local::register(storage.clone()); Ok(Profile { - paths, + home, storage, keystore, public_key, @@ -127,31 +124,31 @@ impl Profile { /// Return the path to the keys folder. pub fn keys(&self) -> PathBuf { - self.paths.keys() + self.home.keys() } /// Get the profile home directory. pub fn home(&self) -> &Path { - self.paths.home() + self.home.path() } /// Get the path to the radicle node socket. pub fn socket(&self) -> PathBuf { - self.paths.socket() + self.home.socket() } /// Get `Paths` of profile - pub fn paths(&self) -> &Paths { - &self.paths + pub fn paths(&self) -> &Home { + &self.home } } /// Get the path to the radicle home folder. -pub fn home() -> Result { +pub fn home() -> Result { if let Some(home) = env::var_os(env::RAD_HOME) { - Ok(PathBuf::from(home)) + Ok(Home::new(PathBuf::from(home))) } else if let Some(home) = env::var_os("HOME") { - Ok(PathBuf::from(home).join(".radicle")) + Ok(Home::new(PathBuf::from(home).join(".radicle"))) } else { Err(io::Error::new( io::ErrorKind::NotFound, @@ -160,12 +157,19 @@ pub fn home() -> Result { } } +/// Radicle home. #[derive(Debug, Clone)] -pub struct Paths { - home: PathBuf, +pub struct Home { + path: PathBuf, } -impl Paths { +impl From for Home { + fn from(path: PathBuf) -> Self { + Self { path } + } +} + +impl Home { pub fn init(home: impl Into) -> Result { let paths = Self::new(home); fs::create_dir_all(paths.node()).ok(); @@ -174,23 +178,23 @@ impl Paths { } pub fn new(home: impl Into) -> Self { - Self { home: home.into() } + Self { path: home.into() } } - pub fn home(&self) -> &Path { - self.home.as_path() + pub fn path(&self) -> &Path { + self.path.as_path() } pub fn storage(&self) -> PathBuf { - self.home.join("storage") + self.path.join("storage") } pub fn keys(&self) -> PathBuf { - self.home.join("keys") + self.path.join("keys") } pub fn node(&self) -> PathBuf { - self.home.join("node") + self.path.join("node") } pub fn socket(&self) -> PathBuf { diff --git a/radicle/src/test.rs b/radicle/src/test.rs index 4741214d..47cc6762 100644 --- a/radicle/src/test.rs +++ b/radicle/src/test.rs @@ -10,7 +10,7 @@ pub mod setup { use crate::crypto::test::signer::MockSigner; use crate::prelude::*; use crate::{ - profile::Paths, + profile::Home, test::{fixtures, storage::git::Repository}, Storage, }; @@ -19,7 +19,7 @@ pub mod setup { let mut rng = fastrand::Rng::new(); let signer = MockSigner::new(&mut rng); let home = tmp.path().join("home"); - let paths = Paths::new(home.as_path()); + let paths = Home::new(home.as_path()); let storage = Storage::open(paths.storage()).unwrap(); let (id, _, _, _) = fixtures::project(tmp.path().join("copy"), &storage, &signer).unwrap(); let project = storage.repository(id).unwrap();