Remove repo content parsing to submodule

main
trivernis 8 months ago
parent e1718162ee
commit efd1d54c2c
Signed by: Trivernis
GPG Key ID: 7E6D18B61C8D2F4B

@ -1,70 +1,38 @@
use chksum::sha2_256::chksum;
use dialoguer::Confirm;
use globset::{Glob, GlobSet, GlobSetBuilder};
use miette::{bail, Context, IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
use std::{ use std::{
env,
fs::{self, File}, fs::{self, File},
io::Write, io::Write,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Command, process::Command,
rc::Rc, rc::Rc,
}; };
use tempfile::NamedTempFile;
use crate::{ use crate::templating;
config::{read_config, SiloConfig},
templating, use super::{ApplyContext, ParseContext};
}; use chksum::sha2_256::chksum;
use dialoguer::Confirm;
use globset::{Glob, GlobSet, GlobSetBuilder};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use miette::{Context, IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};
use tempfile::NamedTempFile;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SiloRepo { pub struct Contents {
pub root: DirEntry, pub root: DirEntry,
pub config: SiloConfig,
} }
impl SiloRepo { impl Contents {
pub fn open(path: &Path) -> Result<Self> { pub fn parse(pctx: ParseContext, path: PathBuf) -> Result<Self> {
if !path.try_exists().into_diagnostic()? { let root = DirEntry::parse(Rc::new(pctx), path.to_owned())?;
bail!("The repository {path:?} does not exist"); Ok(Self { root })
}
let config = read_config(path)?;
Ok(Self {
root: DirEntry::parse(
Rc::new(ParseContext::new(GlobSet::empty(), config.clone())),
path.to_owned(),
)?,
config,
})
}
pub fn apply(&self) -> Result<()> {
let cwd = dirs::home_dir().unwrap_or(env::current_dir().into_diagnostic()?);
let ctx = ApplyContext {
config: self.config.clone(),
};
self.root.apply(&ctx, &cwd)
} }
}
pub struct ParseContext {
ignored: GlobSet,
config: SiloConfig,
}
impl ParseContext { pub fn apply(&self, actx: &ApplyContext, cwd: &Path) -> Result<()> {
pub fn new(ignored: GlobSet, config: SiloConfig) -> Self { self.root.apply(actx, cwd)
Self { ignored, config }
} }
} }
pub struct ApplyContext {
config: SiloConfig,
}
lazy_static! { lazy_static! {
static ref IGNORED_PATHS: GlobSet = GlobSetBuilder::new() static ref IGNORED_PATHS: GlobSet = GlobSetBuilder::new()
.add(Glob::new("**/.git").unwrap()) .add(Glob::new("**/.git").unwrap())

@ -0,0 +1,54 @@
mod contents;
use globset::GlobSet;
use miette::{bail, IntoDiagnostic, Result};
use std::{env, path::Path};
use crate::config::{read_config, SiloConfig};
use self::contents::Contents;
#[derive(Clone, Debug)]
pub struct SiloRepo {
pub config: SiloConfig,
contents: Contents,
}
impl SiloRepo {
pub fn open(path: &Path) -> Result<Self> {
if !path.try_exists().into_diagnostic()? {
bail!("The repository {path:?} does not exist");
}
let config = read_config(path)?;
let pctx = ParseContext::new(GlobSet::empty(), config.clone());
Ok(Self {
contents: Contents::parse(pctx, path.to_owned())?,
config,
})
}
pub fn apply(&self) -> Result<()> {
let cwd = dirs::home_dir().unwrap_or(env::current_dir().into_diagnostic()?);
let ctx = ApplyContext {
config: self.config.clone(),
};
self.contents.apply(&ctx, &cwd)
}
}
pub struct ParseContext {
ignored: GlobSet,
config: SiloConfig,
}
impl ParseContext {
pub fn new(ignored: GlobSet, config: SiloConfig) -> Self {
Self { ignored, config }
}
}
pub struct ApplyContext {
config: SiloConfig,
}
Loading…
Cancel
Save