From dd01bff07e029a12c98cf5aedfdce4d19f4f707c Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 2 Oct 2021 21:41:28 +0200 Subject: Re-add Ka repository directory creation to Create action. --- src/actions/create.rs | 12 ++++++------ src/filesystem.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/actions/create.rs b/src/actions/create.rs index c12d416..56d946b 100644 --- a/src/actions/create.rs +++ b/src/actions/create.rs @@ -5,13 +5,13 @@ use super::ActionOptions; pub fn create(command_options: ActionOptions, fs: &impl Fs) -> Result<()> { let locations = Locations::from(&command_options); - // FIXME: Re-add old repository deletion. - // if locations.ka_path.exists() { - // fs::remove_dir_all(locations.ka_path.as_path())?; - // } - // fs::create_dir(&locations.ka_path)?; - // fs::create_dir(&locations.ka_files_path)?; + if locations.ka_path.exists() { + fs.delete_directory(&locations.ka_path)?; + } + + fs.create_directory(&locations.ka_path)?; + fs.create_directory(&locations.ka_files_path)?; let mut index_file = fs.create_file(&locations.get_repository_index_path())?; let empty_history = RepositoryHistory::default(); diff --git a/src/filesystem.rs b/src/filesystem.rs index 74d7787..a19968b 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -14,7 +14,9 @@ pub trait Fs { fn open_readable_file(&self, path: &Path) -> Result; fn open_writable_file(&self, path: &Path) -> Result; + fn create_directory(&self, path: &Path) -> Result<()>; fn read_directory(&self, path: &Path) -> Result>; + fn delete_directory(&self, path: &Path) -> Result<()>; fn write_to_file(&self, file: &mut Self::File, buffer: Vec) -> Result<()>; fn read_from_file(&self, file: &mut Self::File) -> Result>; @@ -71,11 +73,21 @@ impl Fs for FsImpl { }) } + fn create_directory(&self, path: &Path) -> Result<()> { + fs::create_dir_all(path) + .with_context(|| format!("Failed creating directory '{}'.", path.display())) + } + fn read_directory(&self, path: &Path) -> Result> { let result: io::Result<_> = fs::read_dir(path)?.collect(); result.with_context(|| format!("Failed reading directory {}", path.display())) } + fn delete_directory(&self, path: &Path) -> Result<()> { + fs::remove_dir_all(path) + .with_context(|| format!("Failed deleting directory '{}'.", path.display())) + } + fn write_to_file(&self, file: &mut Self::File, buffer: Vec) -> Result<()> { file.rewind()?; file.set_len(0)?; @@ -197,6 +209,11 @@ pub mod mock { }) } + fn create_directory(&self, path: &Path) -> Result<()> { + self.add_call(path, ReceivedCallVariant::DirectoryCreated); + Ok(()) + } + fn read_directory(&self, path: &Path) -> Result> { self.add_call(path, ReceivedCallVariant::DirectoryRead); @@ -207,6 +224,11 @@ pub mod mock { } } + fn delete_directory(&self, path: &Path) -> Result<()> { + self.add_call(path, ReceivedCallVariant::DirectoryDeleted); + Ok(()) + } + fn write_to_file(&self, file: &mut Self::File, buffer: Vec) -> Result<()> { self.add_call( &file.path, @@ -274,7 +296,9 @@ pub mod mock { DeleteFile, OpenReadableFile, OpenWritableFile, + CreateDirectory, ReadDirectory { entries: Vec }, + DeleteDirectory, ReadFile { read_content: Vec }, WriteToFile, PathExists(bool), @@ -290,7 +314,9 @@ pub mod mock { FileDeleted, ReadableFileOpened, WritableFileOpened, + DirectoryCreated, DirectoryRead, + DirectoryDeleted, FileRead, FileWritten { written_content: Vec }, DoesPathExist, -- cgit 1.4.1