From 0b7fcbbbd43f611dc462a01c7f63ec011817f399 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Tue, 6 Jun 2023 12:16:54 +0200 Subject: [PATCH] feat(export): core logic --- crates/pacdef_core/src/args/datastructure.rs | 1 + crates/pacdef_core/src/core.rs | 29 ++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/crates/pacdef_core/src/args/datastructure.rs b/crates/pacdef_core/src/args/datastructure.rs index e6e0dd1..45cf893 100644 --- a/crates/pacdef_core/src/args/datastructure.rs +++ b/crates/pacdef_core/src/args/datastructure.rs @@ -8,6 +8,7 @@ pub enum Arguments { #[derive(Debug)] pub enum GroupAction { Edit(Groups), + // TODO: optional output dir Export(Groups), Import(Groups), List, diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index b481719..042808d 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -1,7 +1,8 @@ use std::collections::{HashMap, HashSet}; -use std::fs::{remove_file, File}; +use std::env::current_dir; +use std::fs::{remove_file, rename, File}; use std::os::unix::fs::symlink; -use std::path::Path; +use std::path::{Path, PathBuf}; use anyhow::{bail, ensure, Context, Result}; use const_format::formatcp; @@ -399,12 +400,28 @@ impl Pacdef { } fn export_groups(&self, groups: &[String]) -> Result<()> { - let groups = get_group_file_paths_matching_args(groups, &self.groups) - .context("resolving group files")?; + let groups: Vec<_> = groups + .iter() + .map(|group_name| { + self.groups + .iter() + .find(|group| group.name == *group_name) + .unwrap() + }) + .collect(); - std::fs::rename(from, to) + let output_dir = current_dir()?; - todo!() + for group in &groups { + let mut exported_path = output_dir.clone(); + exported_path.push(PathBuf::from(&group.name)); + // TODO: check if output exists + // TODO: check if this was successful, resort to the other method otherwise + rename(&group.path, &exported_path)?; + symlink(&exported_path, &group.path)?; + } + + Ok(()) } }