From bd04489dc3149d30a699db5732bf3bf9a61e0755 Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Thu, 5 Oct 2023 23:05:40 -0400 Subject: [PATCH] Clean up the code a bit --- src/bin/ublue.rs | 13 ++++++++---- src/lib.rs | 51 +++++++++++++++++++++++++----------------------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/bin/ublue.rs b/src/bin/ublue.rs index 38195f6..fbd1e41 100644 --- a/src/bin/ublue.rs +++ b/src/bin/ublue.rs @@ -8,11 +8,16 @@ fn main() -> Result<()> { match args.command { CommandArgs::Template { recipe, - containerfile: _, + containerfile, + output, } => { - let (tera, context) = setup_tera(recipe)?; - let output = tera.render("Containerfile", &context)?; - println!("{output}"); + let (tera, context) = setup_tera(recipe, containerfile)?; + let output_str = tera.render("Containerfile", &context)?; + if let Some(output) = output { + std::fs::write(output, output_str)?; + } else { + println!("{output_str}"); + } } CommandArgs::Build { containerfile: _ } => { println!("Not yet implemented!"); diff --git a/src/lib.rs b/src/lib.rs index a2ab78b..2f6ca00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ use std::{ use anyhow::Result; use clap::{Parser, Subcommand}; use recipe::Recipe; -use tera::{from_value, Context, Function, Tera}; +use tera::{from_value, Context, Tera}; pub const DEFAULT_CONTAINERFILE: &'static str = include_str!("../templates/starting_point.template"); @@ -31,7 +31,11 @@ pub enum CommandArgs { /// Optional Containerfile to use as a template #[arg(short, long)] - containerfile: Option, + containerfile: Option, + + /// File to output to instead of STDOUT + #[arg(short, long)] + output: Option, }, /// Build an image from a Containerfile @@ -41,8 +45,23 @@ pub enum CommandArgs { }, } -fn print_containerfile() -> impl Function { - Box::new( +pub fn setup_tera(recipe: String, containerfile: Option) -> Result<(Tera, Context)> { + let recipe_de = + serde_yaml::from_str::(fs::read_to_string(PathBuf::from(&recipe))?.as_str())? + .process_repos(); + + let mut context = Context::from_serialize(recipe_de)?; + context.insert("recipe", &recipe); + + let mut tera = Tera::default(); + match containerfile { + Some(containerfile) => { + tera.add_raw_template("Containerfile", &read_to_string(containerfile)?)? + } + None => tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?, + } + tera.register_function( + "print_containerfile", |args: &HashMap| -> tera::Result { match args.get("containerfile") { Some(v) => match from_value::(v.clone()) { @@ -54,11 +73,9 @@ fn print_containerfile() -> impl Function { None => Err("Needs the argument 'containerfile'".into()), } }, - ) -} - -fn print_autorun_scripts() -> impl Function { - Box::new( + ); + tera.register_function( + "print_autorun_scripts", |args: &HashMap| -> tera::Result { match args.get("mode") { Some(v) => match from_value::(v.clone()) { @@ -86,21 +103,7 @@ fn print_autorun_scripts() -> impl Function { None => Err("Need arg 'mode' set with 'pre' or 'post'".into()), } }, - ) -} - -pub fn setup_tera(recipe: String) -> Result<(Tera, Context)> { - let recipe_de = - serde_yaml::from_str::(fs::read_to_string(PathBuf::from(&recipe))?.as_str())? - .process_repos(); - - let mut context = Context::from_serialize(recipe_de)?; - context.insert("recipe", &recipe); - - let mut tera = Tera::default(); - tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?; - tera.register_function("print_containerfile", print_containerfile()); - tera.register_function("print_autorun_scripts", print_autorun_scripts()); + ); Ok((tera, context)) }