feat: with redone output

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-06-17 13:33:10 +02:00
parent 04e8baeefc
commit dc0fa589a5
6 changed files with 82 additions and 46 deletions

View File

@@ -25,7 +25,9 @@ pub struct CuddleContext {
pub node_type: CuddleTreeType,
}
pub fn extract_cuddle(config: CuddleConfig) -> anyhow::Result<Arc<Mutex<Vec<CuddleContext>>>> {
pub fn extract_cuddle(
config: CuddleConfig,
) -> anyhow::Result<Option<Arc<Mutex<Vec<CuddleContext>>>>> {
let mut curr_dir = current_dir()?;
curr_dir.push(".cuddle/");
let fetch_policy = config.get_fetch_policy()?;
@@ -39,6 +41,10 @@ pub fn extract_cuddle(config: CuddleConfig) -> anyhow::Result<Arc<Mutex<Vec<Cudd
// Load main cuddle file.
let cuddle_yaml = find_root_cuddle()?;
if let None = cuddle_yaml {
return Ok(None);
}
let cuddle_yaml = cuddle_yaml.unwrap();
log::trace!(cuddle_yaml=log::as_debug!(cuddle_yaml); "Find root cuddle");
let cuddle_plan = serde_yaml::from_str::<CuddlePlan>(cuddle_yaml.as_str())?;
@@ -74,7 +80,7 @@ pub fn extract_cuddle(config: CuddleConfig) -> anyhow::Result<Arc<Mutex<Vec<Cudd
}
}
Ok(context)
Ok(Some(context))
}
fn create_cuddle_local() -> anyhow::Result<PathBuf> {
@@ -131,9 +137,15 @@ fn pull_parent_cuddle_into_local(
Ok(())
}
fn recurse_parent(path: PathBuf, context: Arc<Mutex<Vec<CuddleContext>>>) -> anyhow::Result<()> {
fn recurse_parent(
path: PathBuf,
context: Arc<Mutex<Vec<CuddleContext>>>,
) -> anyhow::Result<Option<()>> {
let cuddle_contents = find_cuddle(path.clone())?;
let cuddle_plan = serde_yaml::from_str::<CuddlePlan>(&cuddle_contents)?;
if let None = cuddle_contents {
return Ok(None);
}
let cuddle_plan = serde_yaml::from_str::<CuddlePlan>(&cuddle_contents.unwrap())?;
let ctx = context.clone();
if let Ok(mut ctxs) = ctx.lock() {
@@ -154,7 +166,7 @@ fn recurse_parent(path: PathBuf, context: Arc<Mutex<Vec<CuddleContext>>>) -> any
}
CuddleBase::Bool(false) => {
log::debug!("plan is root: finishing up");
return Ok(());
return Ok(Some(()));
}
CuddleBase::String(parent_plan) => {
let destination_path = create_cuddle(path.clone())?;
@@ -169,24 +181,22 @@ fn recurse_parent(path: PathBuf, context: Arc<Mutex<Vec<CuddleContext>>>) -> any
}
}
fn find_root_cuddle() -> anyhow::Result<String> {
fn find_root_cuddle() -> anyhow::Result<Option<String>> {
// TODO: Make recursive towards root
let current_dir = env::current_dir()?;
find_cuddle(current_dir)
}
fn find_cuddle(path: PathBuf) -> anyhow::Result<String> {
fn find_cuddle(path: PathBuf) -> anyhow::Result<Option<String>> {
for entry in std::fs::read_dir(path)? {
let entry = entry?;
let path = entry.path();
let metadata = std::fs::metadata(&path)?;
if metadata.is_file() && path.file_name().unwrap() == OsStr::new("cuddle.yaml") {
return Ok(std::fs::read_to_string(path)?);
return Ok(Some(std::fs::read_to_string(path)?));
}
}
Err(anyhow::anyhow!(
"Could not find 'cuddle.yaml' in the current directory"
))
Ok(None)
}