Skip to content

Commit

Permalink
~ refine RencData lifetime declare
Browse files Browse the repository at this point in the history
  • Loading branch information
oluceps committed Nov 29, 2024
1 parent 6a68a85 commit 54f58fe
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 5 additions & 7 deletions src/cmd/renc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
42 changes: 32 additions & 10 deletions src/util/secmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self> {
let c: DashMap<&Secret, Result<SecBuf<AgeEnc>>> = material
.inner_ref()
.iter()
.flat_map(|x| x.secrets.values())
.map(|i| {
(
i,
SecPathBuf::<InStore>::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::<InStore>::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(),
))
}
}

Expand Down Expand Up @@ -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)| {
Expand Down Expand Up @@ -329,6 +343,14 @@ impl<'a> From<&'a profile::Secret> for SecPathBuf<InStore> {
}
}
}
impl<'a> From<&'a PathBuf> for SecPathBuf<InStore> {
fn from(value: &'a PathBuf) -> Self {
Self {
path: value.to_owned(),
_marker: PhantomData,
}
}
}
impl<'a> From<HashMap<(&'a Secret, HostInfo<'a>), SecPathBuf<InStore>>> for RencData<'a, InStore> {
fn from(value: HashMap<(&'a Secret, HostInfo<'a>), SecPathBuf<InStore>>) -> Self {
Self(value)
Expand Down

0 comments on commit 54f58fe

Please sign in to comment.