diff options
| author | Mel <einebeere@gmail.com> | 2021-10-02 21:41:28 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2021-10-02 21:41:28 +0200 |
| commit | dd01bff07e029a12c98cf5aedfdce4d19f4f707c (patch) | |
| tree | cd94b863380b4aa04218cca6e35bc9207829478f /src | |
| parent | cf01169e467f4ac36d2d8b9ce13e7bbc81d46aa5 (diff) | |
| download | ka-dd01bff07e029a12c98cf5aedfdce4d19f4f707c.tar.zst ka-dd01bff07e029a12c98cf5aedfdce4d19f4f707c.zip | |
Re-add Ka repository directory creation to Create action.
Diffstat (limited to 'src')
| -rw-r--r-- | src/actions/create.rs | 12 | ||||
| -rw-r--r-- | 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<Self::File>; fn open_writable_file(&self, path: &Path) -> Result<Self::File>; + fn create_directory(&self, path: &Path) -> Result<()>; fn read_directory(&self, path: &Path) -> Result<Vec<Self::Entry>>; + fn delete_directory(&self, path: &Path) -> Result<()>; fn write_to_file(&self, file: &mut Self::File, buffer: Vec<u8>) -> Result<()>; fn read_from_file(&self, file: &mut Self::File) -> Result<Vec<u8>>; @@ -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<Vec<Self::Entry>> { 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<u8>) -> 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<Vec<Self::Entry>> { 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<u8>) -> Result<()> { self.add_call( &file.path, @@ -274,7 +296,9 @@ pub mod mock { DeleteFile, OpenReadableFile, OpenWritableFile, + CreateDirectory, ReadDirectory { entries: Vec<EntryMock> }, + DeleteDirectory, ReadFile { read_content: Vec<u8> }, WriteToFile, PathExists(bool), @@ -290,7 +314,9 @@ pub mod mock { FileDeleted, ReadableFileOpened, WritableFileOpened, + DirectoryCreated, DirectoryRead, + DirectoryDeleted, FileRead, FileWritten { written_content: Vec<u8> }, DoesPathExist, |
