From cefbc6523f6bc96fb67292aaf2bc78ff5d1d7e59 Mon Sep 17 00:00:00 2001 From: innocentzero Date: Sun, 1 Sep 2024 02:00:52 +0530 Subject: [PATCH] add toml parsing for group files Adds TOML parsing for group files. Some of the backend trait types for various backends need to be modified for TOML compatibility. Signed-off-by: innocentzero --- src/backend/apt.rs | 4 +++- src/backend/dnf.rs | 5 +++-- src/backend/rustup.rs | 5 ++++- src/config.rs | 6 ++---- src/groups.rs | 42 +++++++++++++++++++++++++++++++++--------- src/main.rs | 8 +++++--- src/packages.rs | 3 ++- 7 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/backend/apt.rs b/src/backend/apt.rs index 7705935..21fd063 100644 --- a/src/backend/apt.rs +++ b/src/backend/apt.rs @@ -1,6 +1,7 @@ use std::collections::BTreeMap; use anyhow::Result; +use serde::{Deserialize, Serialize}; use crate::cmd::{command_found, run_args, run_args_for_stdout}; use crate::prelude::*; @@ -8,11 +9,12 @@ use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, derive_more::Display)] pub struct Apt; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct AptQueryInfo { pub explicit: bool, } +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct AptMakeImplicit; impl Backend for Apt { diff --git a/src/backend/dnf.rs b/src/backend/dnf.rs index a0cb2ab..05bc7bf 100644 --- a/src/backend/dnf.rs +++ b/src/backend/dnf.rs @@ -1,6 +1,7 @@ use std::collections::BTreeMap; use anyhow::Result; +use serde::{Deserialize, Serialize}; use crate::cmd::{command_found, run_args, run_args_for_stdout}; use crate::prelude::*; @@ -8,12 +9,12 @@ use crate::prelude::*; #[derive(Debug, Copy, Clone, Default, derive_more::Display)] pub struct Dnf; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct DnfQueryInfo { pub user: bool, } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct DnfInstallOptions { repo: Option, } diff --git a/src/backend/rustup.rs b/src/backend/rustup.rs index 58b9fa7..63cc7bc 100644 --- a/src/backend/rustup.rs +++ b/src/backend/rustup.rs @@ -3,12 +3,15 @@ use crate::cmd::run_args; use crate::cmd::run_args_for_stdout; use crate::prelude::*; use anyhow::{anyhow, bail, Error, Result}; +use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; #[derive(Debug, Copy, Clone, Default, derive_more::Display)] pub struct Rustup; -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] +#[derive( + Debug, Clone, PartialEq, Eq, PartialOrd, Ord, derive_more::Display, Serialize, Deserialize, +)] pub enum RustupPackageId { Toolchain(String), /// Toolchain, Component diff --git a/src/config.rs b/src/config.rs index 244424b..7bb6721 100644 --- a/src/config.rs +++ b/src/config.rs @@ -46,10 +46,8 @@ impl Config { /// /// # Errors /// - If a config file cannot be found. - pub fn load() -> Result { - let config_file = dirs::config_dir() - .context("getting the config directory")? - .join("pacdef/config.toml"); + pub fn load(pacdef_dir: &Path) -> Result { + let config_file = pacdef_dir.join("config.toml"); let content = match std::fs::read_to_string(config_file) { Ok(content) => content, diff --git a/src/groups.rs b/src/groups.rs index e12d5dc..66c8c9e 100644 --- a/src/groups.rs +++ b/src/groups.rs @@ -1,13 +1,17 @@ use crate::prelude::*; -use anyhow::Result; +use anyhow::{anyhow, Context, Result}; +use path_absolutize::Absolutize; +use walkdir::WalkDir; use std::{ collections::BTreeMap, + fs::{create_dir, read_to_string}, ops::{Deref, DerefMut}, + path::Path, }; /// A type representing a users group files with all their packages -#[derive(Default)] +#[derive(Debug, Default)] pub struct Groups(BTreeMap); impl Groups { @@ -22,17 +26,37 @@ impl Groups { packages } - /// Return a new, empty Group - pub fn new() -> Self { - Self(BTreeMap::new()) - } - /// Loads Groups from a users pacdef config folder. /// /// # Errors /// - If the Group config file cannot be found. - pub fn load() -> Result { - todo!() + pub fn load(group_dir: &Path) -> Result { + let mut groups = Self::default(); + + let group_dir = group_dir.join("groups/"); + if !group_dir.is_dir() { + //other directories were already created with the config file + create_dir(&group_dir).context("group directory doesn't exist, creating one")?; + } + + let mut files = vec![]; + for file in WalkDir::new(&group_dir).follow_links(true) { + let path = file?.path().absolutize_from(&group_dir)?.to_path_buf(); + files.push(path); + } + for group_file in files.iter().filter(|path| path.is_file()) { + let group_name = group_file + .strip_prefix(&group_dir)? + .to_str() + .ok_or(anyhow!("Will not fail on Linux"))? + .to_string(); + println!("group_name: {group_name}"); + let file_contents = read_to_string(group_file).context("Reading group file")?; + + let packages: PackagesInstall = toml::from_str(&file_contents)?; + groups.insert(group_name, packages); + } + Ok(groups) } } diff --git a/src/main.rs b/src/main.rs index 9f23c27..7dab48d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,9 +24,11 @@ fn main() -> Result<()> { let main_arguments = MainArguments::parse(); - let config = Config::load().context("loading config file")?; - - let groups = Groups::load().context("failed to load groups")?; + let pacdef_dir = dirs::config_dir() + .map(|path| path.join("pacdef/")) + .context("getting the pacdef config directory")?; + let config = Config::load(&pacdef_dir).context("loading config file")?; + let groups = Groups::load(&pacdef_dir).context("failed to load groups")?; if groups.is_empty() { log::warn!("no group files found"); diff --git a/src/packages.rs b/src/packages.rs index 053d603..803ddaf 100644 --- a/src/packages.rs +++ b/src/packages.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use serde::{Deserialize, Serialize}; use std::{ collections::{BTreeMap, BTreeSet}, fmt::Display, @@ -20,7 +21,7 @@ macro_rules! generate_packages_ids { macro_rules! generate_packages_install { ($($name:ident: $backend:ident),*) => { - #[derive(Debug, Clone, Default)] + #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct PackagesInstall { $( pub $name: BTreeMap<<$backend as Backend>::PackageId, <$backend as Backend>::InstallOptions>,