coliru

A minimal, flexible, dotfile installer
git clone https://git.ashermorgan.net/coliru/
Log | Files | Refs | README

commit 15790b34bd6957e972a179b953d72eb8e7758c3e
parent 27456e1a2ae9cfb55a3e728249c8be45bb3f72e8
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Thu, 20 Jun 2024 12:49:19 -0700

Add base_dir field to Manifest struct

Diffstat:
Msrc/core.rs | 6++++++
Msrc/manifest.rs | 33+++++++++++++++++++++------------
2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/src/core.rs b/src/core.rs @@ -1,9 +1,15 @@ +use std::env::set_current_dir; use super::manifest; use super::tags; use super::utils::copy_file; /// Execute the steps in a coliru manifest according to a set of tag rules pub fn execute_manifest(manifest: manifest::Manifest, rules: Vec<String>) { + if let Err(why) = set_current_dir(manifest.base_dir) { + eprintln!("Error: {}", why); + return; + } + for (i, step) in manifest.steps.iter().enumerate() { if tags::tags_match(&rules, &step.tags) { println!("Step {}:", i+1); diff --git a/src/manifest.rs b/src/manifest.rs @@ -1,34 +1,42 @@ -use serde::{Serialize, Deserialize}; +use serde::Deserialize; use serde_yaml; use std::fs::read_to_string; -use std::path::Path; +use std::path::{Path, PathBuf}; -#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Deserialize)] pub struct CopyOptions { pub src: String, pub dst: String, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Deserialize)] pub struct Step { pub copy: Vec<CopyOptions>, pub tags: Vec<String>, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Deserialize)] +struct RawManifest { + steps: Vec<Step>, +} + +#[derive(Debug, PartialEq)] pub struct Manifest { pub steps: Vec<Step>, + pub base_dir: PathBuf, } /// Parse a coliru YAML manifest file pub fn parse_manifest_file(path: &Path) -> Result<Manifest, String> { - match read_to_string(path) { - Ok(raw) => match serde_yaml::from_str::<Manifest>(&raw) { - Ok(result) => Ok(result), - Err(why) => Err(why.to_string()), - } - Err(why) => Err(why.to_string()), - } + let raw_str = read_to_string(path).map_err(|why| why.to_string())?; + let raw_manifest = serde_yaml::from_str::<RawManifest>(&raw_str) + .map_err(|why| why.to_string())?; + let base_dir = path.parent().or_else(|| Some(&Path::new("."))).unwrap(); + + Ok(Manifest { + steps: raw_manifest.steps, + base_dir: base_dir.to_path_buf(), + }) } #[cfg(test)] @@ -77,6 +85,7 @@ mod tests { tags: vec![String::from("b"), String::from("c")], } ], + base_dir: PathBuf::from("examples"), }; let actual = parse_manifest_file(Path::new("examples/2.yml")); assert_eq!(actual, Ok(expected));