Rename `profile::Paths` to `profile::Home`

This makes the intent clearer.

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier 2023-01-16 18:06:14 +01:00
parent d31968509d
commit be9def8b11
No known key found for this signature in database
8 changed files with 48 additions and 46 deletions

View File

@ -50,7 +50,7 @@ impl Args for Options {
pub fn run(_options: Options, _ctx: impl term::Context) -> anyhow::Result<()> { pub fn run(_options: Options, _ctx: impl term::Context) -> anyhow::Result<()> {
let home = profile::home()?; let home = profile::home()?;
println!("{}", home.display()); println!("{}", home.path().display());
Ok(()) Ok(())
} }

View File

@ -1,7 +1,7 @@
use std::env; use std::env;
use std::path::Path; use std::path::Path;
use radicle::profile::Profile; use radicle::profile::{Home, Profile};
use radicle::test::fixtures; use radicle::test::fixtures;
mod framework; mod framework;
@ -45,7 +45,7 @@ fn profile(home: &Path) -> Profile {
// Set debug mode, to make test output more predictable. // Set debug mode, to make test output more predictable.
env::set_var("RAD_DEBUG", "1"); env::set_var("RAD_DEBUG", "1");
// Setup a new user. // Setup a new user.
Profile::init(home, "radicle".to_owned()).unwrap() Profile::init(Home::new(home.to_path_buf()), "radicle".to_owned()).unwrap()
} }
#[test] #[test]

View File

@ -73,7 +73,7 @@ pub fn seed(dir: &Path) -> Context {
.unwrap(); .unwrap();
// eq. rad auth // 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
rad_init::init( rad_init::init(

View File

@ -2,7 +2,7 @@ use std::{io, net, thread, time};
use cyphernet::{Cert, EcSign}; use cyphernet::{Cert, EcSign};
use netservices::resource::NetAccept; use netservices::resource::NetAccept;
use radicle::profile::Paths; use radicle::profile::Home;
use radicle::Storage; use radicle::Storage;
use reactor::poller::popol; use reactor::poller::popol;
use reactor::Reactor; use reactor::Reactor;
@ -67,19 +67,19 @@ impl<G: crypto::Signer + EcSign<Pk = NodeId, Sig = Signature> + Clone + 'static>
/// ///
/// This function spawns threads. /// This function spawns threads.
pub fn with( pub fn with(
paths: Paths, home: Home,
config: service::Config, config: service::Config,
listen: Vec<net::SocketAddr>, listen: Vec<net::SocketAddr>,
proxy: net::SocketAddr, proxy: net::SocketAddr,
signer: G, signer: G,
) -> Result<Runtime<G>, Error> { ) -> Result<Runtime<G>, Error> {
let id = *signer.public_key(); let id = *signer.public_key();
let node_sock = paths.socket(); let node_sock = home.socket();
let node_dir = paths.node(); let node_dir = home.node();
let network = config.network; let network = config.network;
let rng = fastrand::Rng::new(); let rng = fastrand::Rng::new();
let clock = LocalTime::now(); 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 address_db = node_dir.join(ADDRESS_DB_FILE);
let routing_db = node_dir.join(ROUTING_DB_FILE); let routing_db = node_dir.join(ROUTING_DB_FILE);
let tracking_db = node_dir.join(TRACKING_DB_FILE); let tracking_db = node_dir.join(TRACKING_DB_FILE);

View File

@ -8,7 +8,6 @@ use radicle::profile;
use radicle_node::client::Runtime; use radicle_node::client::Runtime;
use radicle_node::crypto::ssh::keystore::{Keystore, MemorySigner}; use radicle_node::crypto::ssh::keystore::{Keystore, MemorySigner};
use radicle_node::prelude::{Address, NodeId}; use radicle_node::prelude::{Address, NodeId};
use radicle_node::profile::Paths;
use radicle_node::{logger, service}; use radicle_node::{logger, service};
#[derive(Debug)] #[derive(Debug)]
@ -79,11 +78,10 @@ fn main() -> anyhow::Result<()> {
let options = Options::from_env()?; let options = Options::from_env()?;
let home = profile::home()?; let home = profile::home()?;
let paths = Paths::new(home);
let passphrase = env::var(profile::env::RAD_PASSPHRASE) let passphrase = env::var(profile::env::RAD_PASSPHRASE)
.context("`RAD_PASSPHRASE` is required to be set for the node to establish connections")? .context("`RAD_PASSPHRASE` is required to be set for the node to establish connections")?
.into(); .into();
let keystore = Keystore::new(&paths.keys()); let keystore = Keystore::new(&home.keys());
let signer = MemorySigner::load(&keystore, passphrase)?; let signer = MemorySigner::load(&keystore, passphrase)?;
let config = service::Config { let config = service::Config {
connect: options.connect.into_iter().collect(), connect: options.connect.into_iter().collect(),
@ -92,7 +90,7 @@ fn main() -> anyhow::Result<()> {
..service::Config::default() ..service::Config::default()
}; };
let proxy = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 9050); 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()?; runtime.run()?;

View File

@ -9,7 +9,7 @@ use radicle::crypto::test::signer::MockSigner;
use radicle::git::refname; use radicle::git::refname;
use radicle::identity::Id; use radicle::identity::Id;
use radicle::node::Handle; use radicle::node::Handle;
use radicle::profile::Paths; use radicle::profile::Home;
use radicle::storage::WriteStorage; use radicle::storage::WriteStorage;
use radicle::test::fixtures; use radicle::test::fixtures;
use radicle::Storage; use radicle::Storage;
@ -42,7 +42,7 @@ impl Node {
.take(8) .take(8)
.collect::<String>(), .collect::<String>(),
); );
let paths = Paths::init(home).unwrap(); let paths = Home::init(home).unwrap();
let signer = MockSigner::default(); let signer = MockSigner::default();
let listen = vec![([0, 0, 0, 0], 0).into()]; let listen = vec![([0, 0, 0, 0], 0).into()];
let proxy = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 9050); let proxy = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 9050);

View File

@ -59,24 +59,22 @@ pub enum Error {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Profile { pub struct Profile {
pub paths: Paths, pub home: Home,
pub storage: Storage, pub storage: Storage,
pub keystore: Keystore, pub keystore: Keystore,
pub public_key: PublicKey, pub public_key: PublicKey,
} }
impl Profile { impl Profile {
pub fn init(home: impl AsRef<Path>, passphrase: impl Into<Passphrase>) -> Result<Self, Error> { pub fn init(home: Home, passphrase: impl Into<Passphrase>) -> Result<Self, Error> {
let home = home.as_ref().to_path_buf(); let storage = Storage::open(home.storage())?;
let paths = Paths::init(home.as_path())?; let keystore = Keystore::new(&home.keys());
let storage = Storage::open(paths.storage())?;
let keystore = Keystore::new(&paths.keys());
let public_key = keystore.init("radicle", passphrase)?; let public_key = keystore.init("radicle", passphrase)?;
transport::local::register(storage.clone()); transport::local::register(storage.clone());
Ok(Profile { Ok(Profile {
paths, home,
storage, storage,
keystore, keystore,
public_key, public_key,
@ -85,17 +83,16 @@ impl Profile {
pub fn load() -> Result<Self, Error> { pub fn load() -> Result<Self, Error> {
let home = self::home()?; let home = self::home()?;
let paths = Paths::new(home); let storage = Storage::open(home.storage())?;
let storage = Storage::open(paths.storage())?; let keystore = Keystore::new(&home.keys());
let keystore = Keystore::new(&paths.keys());
let public_key = keystore let public_key = keystore
.public_key()? .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()); transport::local::register(storage.clone());
Ok(Profile { Ok(Profile {
paths, home,
storage, storage,
keystore, keystore,
public_key, public_key,
@ -127,31 +124,31 @@ impl Profile {
/// Return the path to the keys folder. /// Return the path to the keys folder.
pub fn keys(&self) -> PathBuf { pub fn keys(&self) -> PathBuf {
self.paths.keys() self.home.keys()
} }
/// Get the profile home directory. /// Get the profile home directory.
pub fn home(&self) -> &Path { pub fn home(&self) -> &Path {
self.paths.home() self.home.path()
} }
/// Get the path to the radicle node socket. /// Get the path to the radicle node socket.
pub fn socket(&self) -> PathBuf { pub fn socket(&self) -> PathBuf {
self.paths.socket() self.home.socket()
} }
/// Get `Paths` of profile /// Get `Paths` of profile
pub fn paths(&self) -> &Paths { pub fn paths(&self) -> &Home {
&self.paths &self.home
} }
} }
/// Get the path to the radicle home folder. /// Get the path to the radicle home folder.
pub fn home() -> Result<PathBuf, io::Error> { pub fn home() -> Result<Home, io::Error> {
if let Some(home) = env::var_os(env::RAD_HOME) { 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") { } else if let Some(home) = env::var_os("HOME") {
Ok(PathBuf::from(home).join(".radicle")) Ok(Home::new(PathBuf::from(home).join(".radicle")))
} else { } else {
Err(io::Error::new( Err(io::Error::new(
io::ErrorKind::NotFound, io::ErrorKind::NotFound,
@ -160,12 +157,19 @@ pub fn home() -> Result<PathBuf, io::Error> {
} }
} }
/// Radicle home.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Paths { pub struct Home {
home: PathBuf, path: PathBuf,
} }
impl Paths { impl From<PathBuf> for Home {
fn from(path: PathBuf) -> Self {
Self { path }
}
}
impl Home {
pub fn init(home: impl Into<PathBuf>) -> Result<Self, io::Error> { pub fn init(home: impl Into<PathBuf>) -> Result<Self, io::Error> {
let paths = Self::new(home); let paths = Self::new(home);
fs::create_dir_all(paths.node()).ok(); fs::create_dir_all(paths.node()).ok();
@ -174,23 +178,23 @@ impl Paths {
} }
pub fn new(home: impl Into<PathBuf>) -> Self { pub fn new(home: impl Into<PathBuf>) -> Self {
Self { home: home.into() } Self { path: home.into() }
} }
pub fn home(&self) -> &Path { pub fn path(&self) -> &Path {
self.home.as_path() self.path.as_path()
} }
pub fn storage(&self) -> PathBuf { pub fn storage(&self) -> PathBuf {
self.home.join("storage") self.path.join("storage")
} }
pub fn keys(&self) -> PathBuf { pub fn keys(&self) -> PathBuf {
self.home.join("keys") self.path.join("keys")
} }
pub fn node(&self) -> PathBuf { pub fn node(&self) -> PathBuf {
self.home.join("node") self.path.join("node")
} }
pub fn socket(&self) -> PathBuf { pub fn socket(&self) -> PathBuf {

View File

@ -10,7 +10,7 @@ pub mod setup {
use crate::crypto::test::signer::MockSigner; use crate::crypto::test::signer::MockSigner;
use crate::prelude::*; use crate::prelude::*;
use crate::{ use crate::{
profile::Paths, profile::Home,
test::{fixtures, storage::git::Repository}, test::{fixtures, storage::git::Repository},
Storage, Storage,
}; };
@ -19,7 +19,7 @@ pub mod setup {
let mut rng = fastrand::Rng::new(); let mut rng = fastrand::Rng::new();
let signer = MockSigner::new(&mut rng); let signer = MockSigner::new(&mut rng);
let home = tmp.path().join("home"); 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 storage = Storage::open(paths.storage()).unwrap();
let (id, _, _, _) = fixtures::project(tmp.path().join("copy"), &storage, &signer).unwrap(); let (id, _, _, _) = fixtures::project(tmp.path().join("copy"), &storage, &signer).unwrap();
let project = storage.repository(id).unwrap(); let project = storage.repository(id).unwrap();