Allow for custom Containerfile adding

This commit is contained in:
Gerald Pinder 2023-10-01 16:38:18 -04:00
parent bd6fabd0de
commit 69effba45b
3 changed files with 30 additions and 10 deletions

View file

@ -1,9 +1,13 @@
use std::{fs, path::PathBuf};
use std::{
collections::HashMap,
fs::{self, read_to_string},
path::PathBuf,
};
use anyhow::Result;
use clap::{Parser, Subcommand};
use recipe::Recipe;
use tera::{Context, Tera};
use tera::{from_value, Context, Function, Tera};
pub const DEFAULT_CONTAINERFILE: &'static str =
include_str!("../templates/starting_point.template");
@ -37,6 +41,22 @@ pub enum CommandArgs {
},
}
fn print_containerfile() -> impl Function {
Box::new(
|args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
match args.get("containerfile") {
Some(v) => match from_value::<String>(v.clone()) {
Ok(containerfile) => {
Ok(read_to_string(format!("containerfiles/{containerfile}"))?.into())
}
Err(_) => Err("Arg containerfile wasn't a string".into()),
},
None => Err("Needs the argument 'containerfile'".into()),
}
},
)
}
pub fn setup_tera(recipe: String) -> Result<(Tera, Context)> {
let recipe_de =
serde_yaml::from_str::<Recipe>(fs::read_to_string(PathBuf::from(&recipe))?.as_str())?
@ -47,6 +67,7 @@ pub fn setup_tera(recipe: String) -> Result<(Tera, Context)> {
let mut tera = Tera::default();
tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?;
tera.register_function("print_containerfile", print_containerfile());
Ok((tera, context))
}