radicle: Move some functions to `Home` type

Certain functions in `Profile` did not need to be in there and could be
moved in the more basic `Home` type.
This commit is contained in:
cloudhead 2023-11-03 17:06:33 +01:00
parent d15534df97
commit a70664368e
No known key found for this signature in database
5 changed files with 72 additions and 20 deletions

View File

@ -106,7 +106,7 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
term::print(profile.did()); term::print(profile.did());
} }
Show::Home => { Show::Home => {
term::print(profile.home().display()); term::print(profile.home().path().display());
} }
Show::Config => { Show::Config => {
term::print(profile.home.config().display()); term::print(profile.home.config().display());
@ -171,7 +171,7 @@ fn all(profile: &Profile) -> anyhow::Result<()> {
let home = profile.home(); let home = profile.home();
table.push([ table.push([
term::format::style("Home").into(), term::format::style("Home").into(),
term::format::tertiary(home.display()).into(), term::format::tertiary(home.path().display()).into(),
]); ]);
let config_path = profile.home.config(); let config_path = profile.home.config();

View File

@ -60,7 +60,7 @@ pub async fn run(options: Options) -> anyhow::Result<()> {
let profile = Profile::load()?; let profile = Profile::load()?;
let request_id = RequestId::new(); let request_id = RequestId::new();
tracing::info!("using radicle home at {}", profile.home().display()); tracing::info!("using radicle home at {}", profile.home().path().display());
let app = let app =
router(options, profile)? router(options, profile)?

View File

@ -329,9 +329,9 @@ impl Node<MockSigner> {
let home = Home::new(home).unwrap(); let home = Home::new(home).unwrap();
let signer = MockSigner::default(); let signer = MockSigner::default();
let storage = Storage::open(home.storage()).unwrap(); let storage = Storage::open(home.storage()).unwrap();
let addresses = Book::memory().unwrap(); let addresses = home.addresses_mut().unwrap();
let tracking = tracking::Config::<tracking::Write>::memory().unwrap(); let tracking = home.tracking_mut().unwrap();
let routing = routing::Table::memory().unwrap(); let routing = home.routing_mut().unwrap();
Self { Self {
id: *signer.public_key(), id: *signer.public_key(),

View File

@ -7,7 +7,7 @@ fn main() -> anyhow::Result<()> {
"fingerprint: {}", "fingerprint: {}",
radicle::crypto::ssh::fmt::fingerprint(profile.id()) radicle::crypto::ssh::fmt::fingerprint(profile.id())
); );
println!("home: {}", profile.home().display()); println!("home: {}", profile.home().path().display());
Ok(()) Ok(())
} }

View File

@ -259,30 +259,34 @@ impl Profile {
Ok(addresses) Ok(addresses)
} }
/// Get radicle home.
pub fn home(&self) -> &Home {
&self.home
}
/// Return a multi-source store for aliases. /// Return a multi-source store for aliases.
pub fn aliases(&self) -> Aliases { pub fn aliases(&self) -> Aliases {
let tracking = self.tracking().ok(); let tracking = self.home.tracking().ok();
let addresses = self.addresses().ok(); let addresses = self.home.addresses().ok();
Aliases { Aliases {
tracking, tracking,
addresses, addresses,
} }
} }
}
/// Return the path to the keys folder. impl std::ops::Deref for Profile {
pub fn keys(&self) -> PathBuf { type Target = Home;
self.home.keys()
fn deref(&self) -> &Self::Target {
&self.home
} }
}
/// Get the profile home directory. impl std::ops::DerefMut for Profile {
pub fn home(&self) -> &Path { fn deref_mut(&mut self) -> &mut Self::Target {
self.home.path() &mut self.home
}
/// Get the path to the radicle node socket.
pub fn socket(&self) -> PathBuf {
self.home.socket()
} }
} }
@ -387,6 +391,54 @@ impl Home {
.map(PathBuf::from) .map(PathBuf::from)
.unwrap_or_else(|| self.node().join(node::DEFAULT_SOCKET_NAME)) .unwrap_or_else(|| self.node().join(node::DEFAULT_SOCKET_NAME))
} }
/// Return a read-only handle to the tracking configuration of the node.
pub fn tracking(&self) -> Result<tracking::store::ConfigReader, tracking::store::Error> {
let path = self.node().join(node::TRACKING_DB_FILE);
let config = tracking::store::Config::reader(path)?;
Ok(config)
}
/// Return a read-write handle to the tracking configuration of the node.
pub fn tracking_mut(&self) -> Result<tracking::store::ConfigWriter, tracking::store::Error> {
let path = self.node().join(node::TRACKING_DB_FILE);
let config = tracking::store::Config::open(path)?;
Ok(config)
}
/// Return a read-only handle to the routing database of the node.
pub fn routing(&self) -> Result<routing::Table, routing::Error> {
let path = self.node().join(node::ROUTING_DB_FILE);
let router = routing::Table::reader(path)?;
Ok(router)
}
/// Return a read-write handle to the routing database of the node.
pub fn routing_mut(&self) -> Result<routing::Table, routing::Error> {
let path = self.node().join(node::ROUTING_DB_FILE);
let router = routing::Table::open(path)?;
Ok(router)
}
/// Return a handle to a read-only addresses database of the node.
pub fn addresses(&self) -> Result<address::Book, address::Error> {
let path = self.node().join(node::ADDRESS_DB_FILE);
let addresses = address::Book::reader(path)?;
Ok(addresses)
}
/// Return a handle to the addresses database of the node.
pub fn addresses_mut(&self) -> Result<address::Book, address::Error> {
let path = self.node().join(node::ADDRESS_DB_FILE);
let addresses = address::Book::open(path)?;
Ok(addresses)
}
} }
#[cfg(test)] #[cfg(test)]