From e59df2f36465ec086d1be5e7876ce84a45debef5 Mon Sep 17 00:00:00 2001 From: Kamal Abbas <83987394+0x177@users.noreply.github.com> Date: Thu, 9 May 2024 17:24:03 +0300 Subject: [PATCH] add rm_repo.rs (#16) * added basic remove repo function * cleaner error reporting * semver submodule to dependency * merge bug * refactroed to use macro --------- Co-authored-by: Ishaan Subramanya --- .gitignore | 2 +- Cargo.toml | 2 +- paxy/src/actions/repository/mod.rs | 7 +++ paxy/src/actions/repository/rm_repo.rs | 71 ++++++++++++++++++++++++++ paxy/src/data/config.rs | 2 +- paxy/src/data/mod.rs | 2 +- 6 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 paxy/src/actions/repository/rm_repo.rs diff --git a/.gitignore b/.gitignore index fe8042d..4a45cf4 100644 --- a/.gitignore +++ b/.gitignore @@ -22,7 +22,7 @@ Cargo.lock !.vscode/*.code-snippets .history/ *.vsix -.vscode/ +/.vscode/ # Packaging /packaging/**/pkg diff --git a/Cargo.toml b/Cargo.toml index 83bae38..1fd9e5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,4 +74,4 @@ console = "0.15" # GUI relm4 = "0.8" -relm4-components = "0.8" \ No newline at end of file +relm4-components = "0.8" diff --git a/paxy/src/actions/repository/mod.rs b/paxy/src/actions/repository/mod.rs index 7b2228e..1a2231f 100644 --- a/paxy/src/actions/repository/mod.rs +++ b/paxy/src/actions/repository/mod.rs @@ -24,6 +24,10 @@ pub enum Error { #[non_exhaustive] #[snafu(display("Could not downgrade:\n {source}"))] CouldNotDowngrade { source: downgrade::Error }, + + #[non_exhaustive] + #[snafu(display("Could not remove:\n {source}"))] + CouldNotRemove { source: rm_repo::Error } } // region: IMPORTS @@ -42,6 +46,7 @@ pub mod list; pub mod search; pub mod uninstall; pub mod update; +pub mod rm_repo; // endregion: MODULES @@ -60,5 +65,7 @@ pub use search::*; pub use uninstall::*; #[allow(unused_imports)] pub use update::*; +#[allow(unused_imports)] +pub use rm_repo::*; // endregion: RE-EXPORTS diff --git a/paxy/src/actions/repository/rm_repo.rs b/paxy/src/actions/repository/rm_repo.rs new file mode 100644 index 0000000..96fa649 --- /dev/null +++ b/paxy/src/actions/repository/rm_repo.rs @@ -0,0 +1,71 @@ +//region: IMPORTS +use bson::Document; +use crate::data::config::{Config,load_conf}; +use std::{ + path::PathBuf, + io::Write, +}; +use snafu::{Snafu,ensure,OptionExt,ResultExt}; +//endregion: IMPORTS + +#[derive(Debug, Snafu)] +pub enum Error { + #[snafu(display("failed to serialize repo data into byte stream {source}"))] + FailedToSerialize {source: bson::ser::Error}, + + #[snafu(display("failed to write to repo data file {source}"))] + FailedToWriteData {source: std::io::Error}, + + #[snafu(display("repo not found"))] + FailedToFindRepo {}, +} + +#[allow(dead_code)] +fn delete_repo(repo_name: &str) -> Result<(),Error> { + let mut config = load_conf(); + let mut readable_data = config.repositories; + + readable_data.get(repo_name).context(FailedToFindRepoSnafu{})?; + readable_data.remove(repo_name); + let mut buf = vec![]; + let rbd_result = readable_data.to_writer(&mut buf); + + rbd_result.context(FailedToSerializeSnafu{})?; + + let mut repos_file_path: PathBuf = home!(); + repos_file_path.push(".paxy"); + repos_file_path.push("repos.bson"); + let mut file = std::fs::OpenOptions::new().write(true).truncate(true).open(repos_file_path).unwrap(); + let ftw_result = file.write_all(&buf); + + ftw_result.context(FailedToWriteDataSnafu{})?; + + config.repositories = readable_data; + + let mut config_toml_path: PathBuf = home!(); + + config_toml_path.push(".paxy"); + config_toml_path.push("config.toml"); + + let mut config_toml_file = std::fs::OpenOptions::new().write(true).truncate(true).open(config_toml_path).unwrap(); + config_toml_file.write_all( + toml::to_string(&config) + .unwrap() + .as_bytes(), + ) + .expect("Permission error"); + + Ok(()) +} + +//region: TESTS +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn delete_repo_test() { + Config::default(); + assert_eq!(delete_repo("paxy-pkgs").is_ok(),true); + } +} +//endregion: TESTS diff --git a/paxy/src/data/config.rs b/paxy/src/data/config.rs index 94a6912..db6b54e 100644 --- a/paxy/src/data/config.rs +++ b/paxy/src/data/config.rs @@ -79,7 +79,7 @@ pub enum InstallType { } #[allow(dead_code)] -fn load_conf() -> Config { +pub fn load_conf() -> Config { let mut conf_path: PathBuf = home!(); conf_path.push(".paxy"); conf_path.push("config.toml"); diff --git a/paxy/src/data/mod.rs b/paxy/src/data/mod.rs index 68a6538..2b894b4 100644 --- a/paxy/src/data/mod.rs +++ b/paxy/src/data/mod.rs @@ -35,4 +35,4 @@ use snafu::Snafu; // pub use some_module::*; // endregion: RE-EXPORTS -mod config; +pub mod config;