radicle: add remove to WriteStorage

Add the ability to remove a repository, given its RID.

This is useful in the case of attempting to clone a repository, and if
that process fails -- for example, if the repository is private --
then it's necessary to clean up the initially created repository.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2023-10-12 13:45:43 +01:00 committed by cloudhead
parent f1f32d8574
commit 87187d47e5
No known key found for this signature in database
3 changed files with 23 additions and 0 deletions

View File

@ -342,6 +342,8 @@ pub trait WriteStorage: ReadStorage {
fn repository_mut(&self, rid: Id) -> Result<Self::RepositoryMut, Error>; fn repository_mut(&self, rid: Id) -> Result<Self::RepositoryMut, Error>;
/// Create a read-write repository. /// Create a read-write repository.
fn create(&self, rid: Id) -> Result<Self::RepositoryMut, Error>; fn create(&self, rid: Id) -> Result<Self::RepositoryMut, Error>;
/// Delete a repository.
fn remove(&self, rid: Id) -> Result<(), Error>;
} }
/// Allows read-only access to a repository. /// Allows read-only access to a repository.
@ -575,6 +577,10 @@ where
fn create(&self, rid: Id) -> Result<Self::RepositoryMut, Error> { fn create(&self, rid: Id) -> Result<Self::RepositoryMut, Error> {
self.deref().create(rid) self.deref().create(rid)
} }
fn remove(&self, rid: Id) -> Result<(), Error> {
self.deref().remove(rid)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -127,6 +127,10 @@ impl WriteStorage for Storage {
fn create(&self, rid: Id) -> Result<Self::RepositoryMut, Error> { fn create(&self, rid: Id) -> Result<Self::RepositoryMut, Error> {
Repository::create(paths::repository(self, &rid), rid) Repository::create(paths::repository(self, &rid), rid)
} }
fn remove(&self, rid: Id) -> Result<(), Error> {
self.repository(rid)?.remove()
}
} }
impl Storage { impl Storage {
@ -299,6 +303,15 @@ impl Repository {
Ok(Self { id, backend }) Ok(Self { id, backend })
} }
/// Remove an existing repository
pub fn remove(&self) -> Result<(), Error> {
let path = self.backend.path();
if path.exists() {
fs::remove_dir_all(path)?;
}
Ok(())
}
/// Create the repository's identity branch. /// Create the repository's identity branch.
pub fn init<G: Signer, S: WriteStorage>( pub fn init<G: Signer, S: WriteStorage>(
doc: &Doc<Verified>, doc: &Doc<Verified>,

View File

@ -99,6 +99,10 @@ impl WriteStorage for MockStorage {
fn create(&self, _rid: Id) -> Result<Self::RepositoryMut, Error> { fn create(&self, _rid: Id) -> Result<Self::RepositoryMut, Error> {
todo!() todo!()
} }
fn remove(&self, _rid: Id) -> Result<(), Error> {
todo!()
}
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]