From 54f58fea4331a1f9f6eafd067208fd1d76481e40 Mon Sep 17 00:00:00 2001 From: oluceps Date: Fri, 29 Nov 2024 22:59:16 +0800 Subject: [PATCH] ~ refine RencData lifetime declare --- src/cmd/check.rs | 2 +- src/cmd/deploy.rs | 2 +- src/cmd/renc.rs | 12 +++++------- src/util/secmap.rs | 42 ++++++++++++++++++++++++++++++++---------- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/cmd/check.rs b/src/cmd/check.rs index 13cb3b3..ecf8788 100644 --- a/src/cmd/check.rs +++ b/src/cmd/check.rs @@ -12,7 +12,7 @@ impl CompleteProfile<'_> { .first() .with_context(|| eyre::eyre!("deploy must only one host"))?; - let ctx = RencCtx::create(self); + let ctx = RencCtx::create(self)?; let inst = RencBuilder::create(self) .build_instore() diff --git a/src/cmd/deploy.rs b/src/cmd/deploy.rs index 1d020e5..4375605 100644 --- a/src/cmd/deploy.rs +++ b/src/cmd/deploy.rs @@ -155,7 +155,7 @@ impl Profile { let templates = self.templates.iter().filter(|i| if_early(i.0)); let complete = CompleteProfile::from_iter(iter::once(self)); - let ctx = RencCtx::create(&complete); + let ctx = RencCtx::create(&complete)?; let plain_map = RencBuilder::create(&complete) .build_instore() diff --git a/src/cmd/renc.rs b/src/cmd/renc.rs index f77ed16..a92782f 100644 --- a/src/cmd/renc.rs +++ b/src/cmd/renc.rs @@ -48,18 +48,16 @@ impl<'a> CompleteProfile<'a> { bail!("`flake.nix` not found here, make sure run in flake toplevel."); }; - let ctx = RencCtx::create(&self); - let mut raw_instance = RencBuilder::create(&self).build_inrepo(ctx, cache_path.clone()); - raw_instance.clean_outdated(cache_path.clone())?; - raw_instance.retain_noexist(); + let ctx = RencCtx::create(&self)?; + let mut materia = RencBuilder::create(&self).build_inrepo(&ctx, cache_path.clone()); + materia.clean_outdated(cache_path)?; + materia.retain_noexist(); let ParsedIdentity { identity, recipient: _, } = RawIdentity::from(identity).try_into()?; - let ctx = RencCtx::create(&self); - - raw_instance.build_instance().makeup(&ctx, identity) + materia.build_instance().makeup(&ctx, identity) } } diff --git a/src/util/secmap.rs b/src/util/secmap.rs index 9802add..2108e54 100644 --- a/src/util/secmap.rs +++ b/src/util/secmap.rs @@ -13,7 +13,7 @@ use crate::{ }; use age::Identity; use dashmap::{DashMap, Map}; -use eyre::Context; +use eyre::{Context, bail}; use eyre::{Result, eyre}; use log::debug; use std::marker::PhantomData; @@ -104,23 +104,37 @@ impl<'a, B> RencCtx<'a, B> { } impl<'a> RencCtx<'a, AgeEnc> { - pub fn create(material: &'a CompleteProfile) -> Self { - let c = material + pub fn create(material: &'a CompleteProfile) -> Result { + let c: DashMap<&Secret, Result>> = material .inner_ref() .iter() .flat_map(|x| x.secrets.values()) .map(|i| { ( i, - SecPathBuf::::from(i) - .read_buffer() - .map(SecBuf::new) - .wrap_err_with(|| eyre!("reading secret file {} failed", i.file)) - .expect("read store must success"), + PathBuf::from(i.file.clone()) + .canonicalize() + .wrap_err_with(|| eyre!("secret not found: {}", i.file)) + .and_then(|i| { + SecPathBuf::::from(&i) + .read_buffer() + .map(SecBuf::new) + }), ) }) .collect(); - Self(c) + + for ref r in c.iter() { + if r.is_err() { + bail!("{}", r.as_ref().unwrap_err()) + } + } + + Ok(Self( + c.into_iter() + .map(|(k, v)| (k, v.expect("handled"))) + .collect(), + )) } } @@ -175,7 +189,7 @@ impl<'a> RencBuilder<'a> { pub fn build_inrepo( self, - ctx: RencCtx<'a, AgeEnc>, + ctx: &RencCtx<'a, AgeEnc>, cache_dir: PathBuf, ) -> RencData<'a, InRepo> { RencData::<'_, InRepo>(self.0.iter().fold(HashMap::new(), |mut acc, ((x, _), z)| { @@ -329,6 +343,14 @@ impl<'a> From<&'a profile::Secret> for SecPathBuf { } } } +impl<'a> From<&'a PathBuf> for SecPathBuf { + fn from(value: &'a PathBuf) -> Self { + Self { + path: value.to_owned(), + _marker: PhantomData, + } + } +} impl<'a> From), SecPathBuf>> for RencData<'a, InStore> { fn from(value: HashMap<(&'a Secret, HostInfo<'a>), SecPathBuf>) -> Self { Self(value)